本文整理自网络,侵删。
主要是介绍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 xe 取得 app 自己的版本号 (狠跨 4 个平台)
Delphi superobject json操作类的基本用法
更多相关阅读请进入《Delphi》频道 >>