H
hua025
Unregistered / Unconfirmed
GUEST, unregistred user!
[?]1、要求在TOleContainer的界面上显示Excel,并且在用户点击了从数据库导出数据后,Excel能够把数据在Form的界面内显示,并且保存到文件中去;而当用户在Excel的工作表(可以是从用户准备好的现有的一个xls文件或者是新创建的一个空白文文件)编辑数据后,点击导入到数据库中去时,程序把数据导入到数据库中。
2、下面是我已经完成的一个程序,使用的是TExcelApplication(不是OLEContainer),缺点是Excel显示独立于程序的界面,而这是不允许的。不知道是因为我没有彻底掌握TExcelApplication的用法还是这种方法只能达到这样的效果?
3、请大侠指教哪些书有关于Delphi操作Excel、嵌入VBA的介绍?谢谢
------------------------------------------------------------------------------
unit MsgParam;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, ADODB, StdCtrls,Excel2000, OleCtnrs;
type
TForm1 = class(TForm)
BtnExportMsg: TButton;
ADOConnection1: TADOConnection;
ParamADOQuery: TADOQuery;
CloADOQuery: TADOQuery;
MsgParamSaveDialog: TSaveDialog;
BtnImportMsg: TButton;
MsgParamOpenDialog: TOpenDialog;
SelectStructComboBox: TComboBox;
Label1: TLabel;
OleContainer1: TOleContainer;
BtnCloseFile: TButton;
BtnExportParam: TButton;
procedure BtnExportMsgClick(Sender: TObject);
procedure BtnImportMsgClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure SelectStructComboBoxChange(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure BtnCloseFileClick(Sender: TObject);
procedure BtnExportParamClick(Sender: TObject);
private
{ Private declarations }
IsFileOPen:boolean;
StructName:string;
ExcelApplication1: TExcelApplication;
procedure ExportMsg(StructName:string;ExportFileName:string);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.ExportMsg(StructName:string;ExportFileName:string);
var
ExcelWorksheet1: TExcelWorksheet;
ExcelWorkbook1: TExcelWorkbook;
i, j: integer;
begin
if StructName='' then
begin
exit;
end;
if ExportFileName='' then
begin
exit;
end;
ExcelWorksheet1 := TExcelWorksheet.Create(Application);
ExcelWorkbook1 := TExcelWorkbook.Create(Application);
try
ExcelApplication1.Workbooks.Add(EmptyParam, 0);
ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]);
ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _worksheet);
ExcelApplication1.Visible[0]:=True;//设置显示Excel
//打开数据库并显示已有的消息参数取值(即表格的第一行)
ParamADOQuery.Active:=false;
ParamADOQuery.SQL.Clear;
ParamADOQuery.SQL.Add('Select ParamName from MSGParamsOrder where StructName='''+StructName+''' order by ParamOrder');
ParamADOQuery.Active:=true;
ParamADOQuery.ExecSQL();
ParamADOQuery.First;
ExcelWorksheet1.Cells.item[1,1] :='MsgName';
ExcelWorksheet1.Cells.item[1,1].font.size := '10';
for i := 1 to ParamADOQuery.RecordCount do
begin
ExcelWorksheet1.Cells.item[1, i+1] := ParamADOQuery.Fields[0].Asstring;
ExcelWorksheet1.Cells.item[1, i+1].font.size := '10';
ParamADOQuery.Next;
end;
ParamADOQuery.First;
//下面首先填写位于表格第一列的消息名称
CloADOQuery.Active:=false;
CloADOQuery.SQL.Clear;
CloADOQuery.SQL.Add('Select MessageName from Messages where StructName='''+StructName+'''');
CloADOQuery.Active:=true;
CloADOQuery.ExecSQL();
CloADOQuery.First;
for i := 1 to CloADOQuery.RecordCount do
begin
ExcelWorksheet1.Cells.item[i+1, 1] := CloADOQuery.Fields[0].Asstring;
ExcelWorksheet1.Cells.item[i+1, 1].font.size := '10';
CloADOQuery.Next;
end;
CloADOQuery.First;
//以参数为外循环,消息名为内循环填写消息参数表。
for i:=1 to ParamADOQuery.RecordCount do
begin//外循环,每个参数为一列,按列填写。
CloADOQuery.Active:=false;
CloADOQuery.SQL.Clear;
CloADOQuery.SQL.Add('Select Content from MSGParamsTable where StructName='''+StructName+''' and ParamName= '''+ParamADOQuery.Fields[0].Asstring+'''order by MessageName ');//
//showmessage(CloADOQuery.SQL.Text);
CloADOQuery.Active:=true;
CloADOQuery.ExecSQL();
CloADOQuery.First;
for j:=1 to CloADOQuery.RecordCount do
begin
ExcelWorksheet1.Cells.item[j+1, i+1] := CloADOQuery.Fields[0].Asstring;
ExcelWorksheet1.Cells.item[j+1, i+1].font.size := '10';
CloADOQuery.Next;
end;
ParamADOQuery.Next;//移动游标到下一个参数,即开始写新的一列
end;
ExcelWorksheet1.Columns.AutoFit;
try
ExcelWorksheet1.SaveAs(ExportFileName);//注意,此处没有加保护,当用户取消保存时将出错
ShowMessage('该结构的消息已经成功备份到'+ExportFileName);
except
ShowMessage('备份失败');
end;
finally
ExcelWorksheet1.Free;
ExcelApplication1.Workbooks.Close(0);
ExcelWorkbook1.Free;
end;
end;
procedure TForm1.BtnExportMsgClick(Sender: TObject);
var
ExportFileName:string;
begin
//MsgParamSaveDialog.FileName:=StructName;
MsgParamSaveDialog.FileName:='';
MsgParamSaveDialog.Execute;
ExportFileName:=MsgParamSaveDialog.FileName;
if ExportFileName<>'' then
begin
ExportMsg(StructName,ExportFileName);
end
else
begin
ShowMessage('您已经放弃备份!');
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
i:integer;
begin
try//打开Excel
ExcelApplication1 := TExcelApplication.Create(Application);
ExcelApplication1.Connect;
except
Application.Messagebox('Excel.exe Not Found!', 'MsgPool', MB_ICONERROR + mb_Ok);
Abort;
end;
ParamADOQuery.Active:=false;
ParamADOQuery.SQL.Clear;
ParamADOQuery.SQL.Add('select StructName from Structs');
//ShowMessage(ParamADOQuery.SQL.Text);
ParamADOQuery.Active:=true;
for i := 1 to ParamADOQuery.RecordCount do
begin
SelectStructComboBox.AddItem(ParamADOQuery.Fields[0].Asstring,nil);
ParamADOQuery.Next;
end;
BtnImportMsg.Enabled:=false;
BtnExportMsg.Enabled:=false;
end;
procedure TForm1.SelectStructComboBoxChange(Sender: TObject);
begin
StructName:=SelectStructComboBox.Text;
BtnImportMsg.Enabled:=true;
BtnExportMsg.Enabled:=true;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
ExcelApplication1.Disconnect;
ExcelApplication1.Quit;
//ExcelApplication1.Free;
end;
2、下面是我已经完成的一个程序,使用的是TExcelApplication(不是OLEContainer),缺点是Excel显示独立于程序的界面,而这是不允许的。不知道是因为我没有彻底掌握TExcelApplication的用法还是这种方法只能达到这样的效果?
3、请大侠指教哪些书有关于Delphi操作Excel、嵌入VBA的介绍?谢谢
------------------------------------------------------------------------------
unit MsgParam;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, ADODB, StdCtrls,Excel2000, OleCtnrs;
type
TForm1 = class(TForm)
BtnExportMsg: TButton;
ADOConnection1: TADOConnection;
ParamADOQuery: TADOQuery;
CloADOQuery: TADOQuery;
MsgParamSaveDialog: TSaveDialog;
BtnImportMsg: TButton;
MsgParamOpenDialog: TOpenDialog;
SelectStructComboBox: TComboBox;
Label1: TLabel;
OleContainer1: TOleContainer;
BtnCloseFile: TButton;
BtnExportParam: TButton;
procedure BtnExportMsgClick(Sender: TObject);
procedure BtnImportMsgClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure SelectStructComboBoxChange(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure BtnCloseFileClick(Sender: TObject);
procedure BtnExportParamClick(Sender: TObject);
private
{ Private declarations }
IsFileOPen:boolean;
StructName:string;
ExcelApplication1: TExcelApplication;
procedure ExportMsg(StructName:string;ExportFileName:string);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.ExportMsg(StructName:string;ExportFileName:string);
var
ExcelWorksheet1: TExcelWorksheet;
ExcelWorkbook1: TExcelWorkbook;
i, j: integer;
begin
if StructName='' then
begin
exit;
end;
if ExportFileName='' then
begin
exit;
end;
ExcelWorksheet1 := TExcelWorksheet.Create(Application);
ExcelWorkbook1 := TExcelWorkbook.Create(Application);
try
ExcelApplication1.Workbooks.Add(EmptyParam, 0);
ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]);
ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _worksheet);
ExcelApplication1.Visible[0]:=True;//设置显示Excel
//打开数据库并显示已有的消息参数取值(即表格的第一行)
ParamADOQuery.Active:=false;
ParamADOQuery.SQL.Clear;
ParamADOQuery.SQL.Add('Select ParamName from MSGParamsOrder where StructName='''+StructName+''' order by ParamOrder');
ParamADOQuery.Active:=true;
ParamADOQuery.ExecSQL();
ParamADOQuery.First;
ExcelWorksheet1.Cells.item[1,1] :='MsgName';
ExcelWorksheet1.Cells.item[1,1].font.size := '10';
for i := 1 to ParamADOQuery.RecordCount do
begin
ExcelWorksheet1.Cells.item[1, i+1] := ParamADOQuery.Fields[0].Asstring;
ExcelWorksheet1.Cells.item[1, i+1].font.size := '10';
ParamADOQuery.Next;
end;
ParamADOQuery.First;
//下面首先填写位于表格第一列的消息名称
CloADOQuery.Active:=false;
CloADOQuery.SQL.Clear;
CloADOQuery.SQL.Add('Select MessageName from Messages where StructName='''+StructName+'''');
CloADOQuery.Active:=true;
CloADOQuery.ExecSQL();
CloADOQuery.First;
for i := 1 to CloADOQuery.RecordCount do
begin
ExcelWorksheet1.Cells.item[i+1, 1] := CloADOQuery.Fields[0].Asstring;
ExcelWorksheet1.Cells.item[i+1, 1].font.size := '10';
CloADOQuery.Next;
end;
CloADOQuery.First;
//以参数为外循环,消息名为内循环填写消息参数表。
for i:=1 to ParamADOQuery.RecordCount do
begin//外循环,每个参数为一列,按列填写。
CloADOQuery.Active:=false;
CloADOQuery.SQL.Clear;
CloADOQuery.SQL.Add('Select Content from MSGParamsTable where StructName='''+StructName+''' and ParamName= '''+ParamADOQuery.Fields[0].Asstring+'''order by MessageName ');//
//showmessage(CloADOQuery.SQL.Text);
CloADOQuery.Active:=true;
CloADOQuery.ExecSQL();
CloADOQuery.First;
for j:=1 to CloADOQuery.RecordCount do
begin
ExcelWorksheet1.Cells.item[j+1, i+1] := CloADOQuery.Fields[0].Asstring;
ExcelWorksheet1.Cells.item[j+1, i+1].font.size := '10';
CloADOQuery.Next;
end;
ParamADOQuery.Next;//移动游标到下一个参数,即开始写新的一列
end;
ExcelWorksheet1.Columns.AutoFit;
try
ExcelWorksheet1.SaveAs(ExportFileName);//注意,此处没有加保护,当用户取消保存时将出错
ShowMessage('该结构的消息已经成功备份到'+ExportFileName);
except
ShowMessage('备份失败');
end;
finally
ExcelWorksheet1.Free;
ExcelApplication1.Workbooks.Close(0);
ExcelWorkbook1.Free;
end;
end;
procedure TForm1.BtnExportMsgClick(Sender: TObject);
var
ExportFileName:string;
begin
//MsgParamSaveDialog.FileName:=StructName;
MsgParamSaveDialog.FileName:='';
MsgParamSaveDialog.Execute;
ExportFileName:=MsgParamSaveDialog.FileName;
if ExportFileName<>'' then
begin
ExportMsg(StructName,ExportFileName);
end
else
begin
ShowMessage('您已经放弃备份!');
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
i:integer;
begin
try//打开Excel
ExcelApplication1 := TExcelApplication.Create(Application);
ExcelApplication1.Connect;
except
Application.Messagebox('Excel.exe Not Found!', 'MsgPool', MB_ICONERROR + mb_Ok);
Abort;
end;
ParamADOQuery.Active:=false;
ParamADOQuery.SQL.Clear;
ParamADOQuery.SQL.Add('select StructName from Structs');
//ShowMessage(ParamADOQuery.SQL.Text);
ParamADOQuery.Active:=true;
for i := 1 to ParamADOQuery.RecordCount do
begin
SelectStructComboBox.AddItem(ParamADOQuery.Fields[0].Asstring,nil);
ParamADOQuery.Next;
end;
BtnImportMsg.Enabled:=false;
BtnExportMsg.Enabled:=false;
end;
procedure TForm1.SelectStructComboBoxChange(Sender: TObject);
begin
StructName:=SelectStructComboBox.Text;
BtnImportMsg.Enabled:=true;
BtnExportMsg.Enabled:=true;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
ExcelApplication1.Disconnect;
ExcelApplication1.Quit;
//ExcelApplication1.Free;
end;
代码: