Delphi读取Excel文件-统计全部工作表中内容相同单元格出现次数


本文整理自网络,侵删。

 主要是介绍Delphi访问Excel文件的方法,思路也比较简单,打开Excel文件,访问每张Sheet的所有单元格,内容出现过的增加次数1,未出现过添加到链接中并设置记数为1。完整单元文件如下:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, OleServer, ExcelXP, StdCtrls, Buttons, ExtCtrls, Gauges;

type
  TForm1 = class(TForm)
    Excel1: TExcelApplication;
    Book1: TExcelWorkbook;
    Sheet1: TExcelWorksheet;
    Label1: TLabel;
    Panel1: TPanel;
    Label2: TLabel;
    Panel2: TPanel;
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    OD1: TOpenDialog;
    SD1: TSaveDialog;
    Button1: TButton;
    PanExec: TPanel;
    LbSheet: TLabel;
    G1: TGauge;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure SpeedButton2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);

  end;

type
  PKeyName = ^ TKeyName;
  TKeyName = record
    Key: String;
    Count: Integer;
  end;

var
  Form1: TForm1;
  List1: TList;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  List1 := TList.Create;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  List1.Free;
end;

procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
  if OD1.Execute then Panel1.Caption := OD1.FileName;
end;

procedure TForm1.SpeedButton2Click(Sender: TObject);
begin
  if SD1.Execute then Panel2.Caption := SD1.FileName;
end;

procedure AddAKey(strName: String);
var
  i: Integer;
  aKey: PKeyName;
begin
  if strName = '' then Exit;
  for i:= 0 to List1.Count - 1 do
  begin
    aKey := List1.Items[i];
    if aKey.Key = strName then
    begin
      Inc(aKey.Count);
      Exit;
    end;
  end;
  New(aKey);
  aKey.Key := strName;
  aKey.Count := 1;
  List1.Add(aKey);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  i: Integer;
  iCol, iRow, iCols, iRows: Integer;
  aKey: PKeyName;
  L1: TStringList;
  LC_ID: LCID;
begin
  if (Panel1.Caption = '') or (Panel2.Caption = '') then Exit;
  //初始化
  (Sender as TButton).Enabled := False;
  G1.Progress := 0;
  LbSheet.Caption := '..';
  PanExec.Visible := True;
  PanExec.Update;
  //处理
  LC_ID := GetUserDefaultLCID;
  Excel1.Workbooks.Open (Panel1.Caption, //打开Excel文件
    EmptyParam , EmptyParam , EmptyParam , EmptyParam ,  EmptyParam,
    EmptyParam , EmptyParam , EmptyParam , EmptyParam , EmptyParam,
    EmptyParam , EmptyParam , EmptyParam , EmptyParam , LC_ID);
  //Excel1.Visible[0] := True;  //显示Excel界面
  Book1.ConnectTo(Excel1.Workbooks[1]); //连接到Excel文件
  for i:=1 to Book1.Sheets.Count do
  begin
    Sheet1.ConnectTo(Book1.Sheets[i] as _Worksheet); //连接到工作表
    LbSheet.Caption := Sheet1.Name;
    G1.Progress := 0;
    PanExec.Update;
    iRows := Sheet1.UsedRange[LC_ID].Rows.Count;
    iCols := Sheet1.UsedRange[LC_ID].Columns.Count;
    G1.MaxValue := iRows * iCols;
    for iRow := 1 to iRows  do
      for iCol := 1 to iCols  do
      begin
        AddAKey(Sheet1.Cells.Item[iRow, iCol]);
        G1.AddProgress(1);
      end;
  end;
  //结束
  Excel1.Workbooks.Close(LC_ID);  //关闭Excel文件
  L1 := TStringList.Create;
  for i:=0 to List1.Count - 1 do
  begin
    aKey := List1.Items[i];
    L1.Add(Format('%s,%d', [aKey.Key, aKey.Count]));
    Dispose(aKey);
  end;
  List1.Clear;
  L1.SaveToFile(Panel2.Caption); //保存处理结果
  L1.Free;
  PanExec.Visible := False;
  (Sender as TButton).Enabled := True;
end;

end.

相关阅读 >>

Delphi xe5调用外部扫描程序――谷歌 zxing

Delphi用ado操作数据库完整示例

Delphi idhttp中get 图像链接通过memorystream加载 image控件显示

Delphi xe更改ttrayicon系统任务栏图标(无模糊)

Delphi调用百度语音识别服务

Delphi shellexecute执行cmd命令窗口不关闭

Delphi xe5实现datetimetounix/unixtodatetime的一点小改进

Delphi xe3中如何crc验证函数?

Delphi 无法打不开读取文件名有逗号的文件

Delphi wmi 获取网络信息

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...