unit FLeading_In_Excel;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
cxDataStorage, cxEdit, DB, cxDBData, ComCtrls, ToolWin, ImgList,
cxGridLevel, cxClasses, cxControls, cxGridCustomView, fHHCommonDM,
cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxGrid,
ExtCtrls, kbmMemTable, ComObj, Grids, Excel97, OleServer, ActnList,
kbmMemTableHH;
type
THHLeading_In_Excel = class(TForm)
Panel2: TPanel;
Panel3: TPanel;
ToolBar3: TToolBar;
btnBack: TToolButton;
Label7: TLabel;
BtnChooseExcel: TButton;
BtnLeading_In_Database: TButton;
OpenDialog1: TOpenDialog;
ExcelApplication1: TExcelApplication;
ExcelWorkbook1: TExcelWorkbook;
ExcelWorksheet1: TExcelWorksheet;
Label1: TLabel;
Label2: TLabel;
Panel1: TPanel;
StringGrid1: TStringGrid;
Label3: TLabel;
Label4: TLabel;
ImageList1: TImageList;
ActionList1: TActionList;
act_Back: TAction;
ChooseExcel: TAction;
Leading_In_Database: TAction;
MMTable: TkbmMemTableHH;
procedure StringGrid1Click(Sender: TObject);
procedure ChooseExcelExecute(Sender: TObject);
procedure Leading_In_DatabaseExecute(Sender: TObject);
procedure act_BackExecute(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
HHLeading_In_Excel: THHLeading_In_Excel;
implementation
{$R *.dfm}
procedure THHLeading_In_Excel.StringGrid1Click(Sender: TObject);
var i, j: integer;
begin
i := stringgrid1.Row;
j := stringgrid1.col;
label1.caption := inttostr(i + 1);
label2.caption := inttostr(j);
StringGrid1.Options := StringGrid1.Options + [goEditing]
end;
procedure THHLeading_In_Excel.ChooseExcelExecute(Sender: TObject);
var i, j: integer;
begin
opendialog1.InitialDir := ExtractFileDir(paramstr(0)); //文件的打存放初始路径
opendialog1.Execute;
try
ExcelApplication1.Connect; //EXCEL应用程序
except
MessageDlg('Excel may not be installed', mtError, [mbOk], 0);
Abort;
end;
ExcelApplication1.Visible[0] := True;
ExcelApplication1.Caption := 'Excel Application';
try
excelapplication1.Workbooks.Open(opendialog1.FileName,
null, null, null, null, null, null, null, null, null, null, null, null, 0); //打开指定的EXCEL 文件
except
begin
ExcelApplication1.Disconnect; //出现异常情况时关闭
ExcelApplication1.Quit; showmessage('请选择EXCEL电子表格!');
exit;
end;
end;
ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]); //ExcelWorkbook1与Eexcelapplication1建立连接
ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _Worksheet); //Excelworksheet1与Excelworkbook1建立连接
//开始从EXCEL中取数,放到stringgrid1中,取完数后关闭EXCEL
for i := 0 to 1000 do //最大取值1000
for j := 1 to 100 do
begin
if trim(excelworksheet1.cells.item[i + 1, 1]) <> '' then
begin
stringgrid1.rowCount := i + 1;
stringgrid1.colCount := j + 1;
stringgrid1.Cells[j, i] := ExcelWorksheet1.Cells.Item[i + 1, j];
end
else
begin
label1.caption := inttostr(i - 1);
label2.caption := inttostr(j - 1);
ExcelApplication1.Disconnect;
ExcelApplication1.Quit;
exit;
end;
end;
end;
procedure THHLeading_In_Excel.Leading_In_DatabaseExecute(Sender: TObject);
var
sql:string;
i:integer;
begin
sql := 'select * from card_type'
HHCommonDM.OpenQueryByTable(sql, '会员卡类型',MMTable);
with MMTable do
begin
if not Active then
Active:=True;
Append;
FieldByName('sid').AsString:=StringGrid1.fieldByName('sid').AsString;
FieldByName('EXCHANGDATAPARA1').AsString:=StringGrid1.fieldByName('EXCHANGDATAPARA1').AsString;
FieldByName('EXCHANGDATAPARA2').AsString:=StringGrid1.fieldByName('EXCHANGDATAPARA2').AsString;
Post;
end;
HHCommonDM.ModifyHHTable(HHCommonDM.MMTable,'I');
end;
procedure THHLeading_In_Excel.act_BackExecute(Sender: TObject);
begin
//
close;
end;
end.