我想直接创建一个Excel文件,然后把数据库的数据写进去,,而且不显示Excel运行的界面,帮帮我!谢谢!
解决方案 »
- 求增大程序自身体积代码
- 哪位好心人提供Delphi6企业版的下载地址
- 请问,怎么样子才能得到COM+中注册的DLL中的位置???
- 请问谁知道TMS组件哪里有下载,要包括TMSGRID最新版本,谢谢!急!!!!!!
- 谁能给我一个这样的例子啊,创建一个线程它用于监听某一端口,当接收到数据时就通知别的线程
- 请教关于 TREEVIEW的用法!!!!!!!!HELP
- 如何使DBGRID中选中的一行显示不同的颜色这样用户看起数据比较方便。
- 急!delphi作的单机版应用程序的 打包问题
- 关于RTL60出错
- Delphi6的一大Bug!!
- 图像的编辑问题,谢谢大家,急用
- 这么多种线程怎么协调工作呢,Semaphore WaitFor等好像都用不上。大侠支招,重谢。
procedure TfrmEaM.BtnOpenExcelClick(Sender: TObject);
var
i : integer;
j : integer;
EParam : OleVariant;
DocuType : OleVariant;
wkbk : _WorkBook;
begin
sg1.RowCount:=1;
opendialog1.InitialDir:=ExtractFileDir(paramstr(0));
//文件打开的初始路径
opendialog1.Execute; Try
ExcelApplication1.Connect;
Except
Showmessage('Excel文件打开失败!');
Exit;
End; ExcelApplication1.Visible[0]:=False;
ExcelApplication1.Caption:='Excel数据文件'; EParam:=EmptyParam;
DocuType:=0;
try
wkBk:=ExcelApplication1.Workbooks.Add(EParam, DocuType); wkBk:=ExcelApplication1.WorkBooks.Open(
opendialog1.FileName,EmptyParam,EmptyParam,
EmptyParam,EmptyParam,EmptyParam,EmptyParam,
EmptyParam,EmptyParam,
EmptyParam,EmptyParam,DocuType,EmptyParam,DocuType);
except
begin
ExcelApplication1.Disconnect;//出现异常情况时关闭
ExcelApplication1.Quit;
showmessage('请选择EXCEL数据表格!');
exit;
end;
end; ExcelWorkbook1.ConnectTo(ExcelApplication1.ActiveWorkbook);//ExcelWorkbook1与Eexcelapplication1建立连接
ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _Worksheet);//Excelworksheet1与Excelworkbook1建立连接 //开始从EXCEL中取数,放到stringgrid中,取完数后关闭EXCEL
for i:=1 to 10 do
for j:=1 to 4 do
begin
if trim(excelworksheet1.cells.item[i+1,1])<>'' then
begin
sg1.rowCount:=i+2;
sg1.Cells[j,i]:=ExcelWorksheet1.Cells.Item[i,j];
end
else
begin
//将第一条数据赋给编辑框
edtID.text:=sg1.Cells[1,1];
edtName.text:=sg1.Cells[2,1];
edtGender.text:=sg1.Cells[3,1];
edtAge.text:=sg1.Cells[4,1];
exit;
end;
end;
ExcelApplication1.Disconnect;
ExcelApplication1.Quit;
end;procedure TfrmEaM.BtnOpenMDBClick(Sender: TObject);
var
i : integer;
j : integer;
begin
//开始从MDB数据表中取数,放到stringgrid中
sg1.rowcount:=1;
adotable1.First;
while not adotable1.eof do
begin
sg1.rowCount:=i+2;
sg1.Cells[1,i]:=adotable1.fieldbyname('ID').Value;
sg1.Cells[2,i]:=adotable1.fieldbyname('Name').Value;
sg1.Cells[3,i]:=adotable1.fieldbyname('Gender').Value;
sg1.Cells[4,i]:=adotable1.fieldbyname('Age').Value; adotable1.Next;
end; //将第一条数据赋给编辑框
edtID.text:=sg1.Cells[1,1];
edtName.text:=sg1.Cells[2,1];
edtGender.text:=sg1.Cells[3,1];
edtAge.text:=sg1.Cells[4,1];
end;procedure TfrmEaM.BtnNextClick(Sender: TObject);
var
i:integer;
begin
i:=sg1.row+1; if i<>sg1.RowCount then
begin
sg1.row:=sg1.row+1; edtID.text:=sg1.Cells[1,i];
edtName.text:=sg1.Cells[2,i];
edtGender.text:=sg1.Cells[3,i];
edtAge.text:=sg1.Cells[4,i]; exit;
end
else
showmessage('已到第一条记录!');
end;
procedure TfrmEaM.BtnForwardClick(Sender: TObject);
var
i:integer;
begin
i:=sg1.row-1; if i<>0 then
begin
sg1.row:=sg1.row-1; edtID.text:=sg1.Cells[1,i];
edtName.text:=sg1.Cells[2,i];
edtGender.text:=sg1.Cells[3,i];
edtAge.text:=sg1.Cells[4,i]; exit;
end
else
showmessage('已到第一条记录!');
end;procedure TfrmEaM.SG1Click(Sender: TObject);
var
i:integer;
begin
i:=sg1.row; edtID.text:=sg1.Cells[1,i];
edtName.text:=sg1.Cells[2,i];
edtGender.text:=sg1.Cells[3,i];
edtAge.text:=sg1.Cells[4,i];
end;procedure TfrmEaM.BtnToMDBClick(Sender: TObject);
var
i : integer;
j : integer;
EParam : OleVariant;
DocuType : OleVariant;
wkbk : _WorkBook;
begin
opendialog1.InitialDir:=ExtractFileDir(paramstr(0));
//文件打开的初始路径
opendialog1.Execute; Try
ExcelApplication1.Connect;
Except
Showmessage('Excel文件打开失败!');
Exit;
End; ExcelApplication1.Visible[0]:=False;
ExcelApplication1.Caption:='Excel数据文件'; EParam:=EmptyParam;
DocuType:=0;
try
wkBk:=ExcelApplication1.Workbooks.Add(EParam, DocuType); wkBk:=ExcelApplication1.WorkBooks.Open(opendialog1.FileName,EmptyParam,EmptyParam,
EmptyParam,EmptyParam,EmptyParam,EmptyParam,
EmptyParam,EmptyParam,
EmptyParam,EmptyParam,DocuType,EmptyParam,DocuType);
except
begin
ExcelApplication1.Disconnect;//出现异常情况时关闭
ExcelApplication1.Quit;
showmessage('请选择EXCEL数据表格!');
exit;
end;
end; ExcelWorkbook1.ConnectTo(ExcelApplication1.ActiveWorkbook);//ExcelWorkbook1与Eexcelapplication1建立连接
ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _Worksheet);//Excelworksheet1与Excelworkbook1建立连接 //开始从EXCEL中取数,放到stringgrid中,取完数后关闭EXCEL
for i:=1 to 10 do
begin
if trim(excelworksheet1.cells.item[i+1,1])<>'' then
begin
AdoTable1.Append;
AdoTable1.fieldbyname('ID').value:=ExcelWorksheet1.Cells.Item[i,1];
AdoTable1.fieldbyname('Name').value:=ExcelWorksheet1.Cells.Item[i,2];
AdoTable1.fieldbyname('Gender').value:=ExcelWorksheet1.Cells.Item[i,3];
AdoTable1.fieldbyname('Age').value:=ExcelWorksheet1.Cells.Item[i,4];
AdoTable1.Post;
end
end; ExcelApplication1.Disconnect;
ExcelApplication1.Quit;
showmessage('数据转换完毕!');
end;procedure TfrmEaM.BtnToExcelClick(Sender: TObject);
var
i : integer;
EParam : OleVariant;
DocuType : OleVariant;
wkbk : _WorkBook;
FileName : OleVariant;
begin
Try
ExcelApplication1.Connect;
Except
Showmessage('Excel文件打开失败!');
Exit;
End; ExcelApplication1.Visible[0]:=False;
ExcelApplication1.Caption:='Excel数据文件'; EParam:=EmptyParam;
DocuType:=0;
try
wkBk:=ExcelApplication1.Workbooks.Add(EParam, DocuType);
except
begin
ExcelApplication1.Disconnect;//出现异常情况时关闭
ExcelApplication1.Quit;
showmessage('创建EXCEL数据表格失败!');
exit;
end;
end; ExcelWorkbook1.ConnectTo(ExcelApplication1.ActiveWorkbook);//ExcelWorkbook1与Eexcelapplication1建立连接
ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _Worksheet);//Excelworksheet1与Excelworkbook1建立连接 AdoTable1.First;
i:=1;
while not AdoTable1.Eof do
begin
ExcelWorksheet1.Cells.Item[i,1]:=AdoTable1.fieldbyname('ID').value;
ExcelWorksheet1.Cells.Item[i,2]:=AdoTable1.fieldbyname('Name').value;
ExcelWorksheet1.Cells.Item[i,3]:=AdoTable1.fieldbyname('Gender').value;
ExcelWorksheet1.Cells.Item[i,4]:=AdoTable1.fieldbyname('Age').value;
AdoTable1.next;
i:=i+1;
end; wkBk:=ExcelApplication1.ActiveWorkbook;
DocuType:=0;
FileName:='d:\mywb.xls';
wkBk.Close(True, FileName, EmptyParam, DocuType); ExcelApplication1.Disconnect;
ExcelApplication1.Quit;
showmessage('数据转换完毕!');
end;
var
eclApp,WorkBook:olevariant;
xlsFileName:string;
i,j:integer;
begin if SaveDialog1.Execute then
xlsFileName:=SaveDialog1.FileName; try
eclApp:=CreateOleObject('Excel.Application');
WorkBook:=CreateOleObject('Excel.Sheet');
except
Application.MessageBox('系统没有安装Microsoft Excel','Microsoft Excel',MB_OK+MB_ICONWarning);
Exit;
end; try
WorkBook:=eclApp.workbooks.Add;
for i:=0 to DBGrid1.Columns.Count-1 do
begin
eclApp.Cells(1,i+1):=DBGrid1.Columns[i].FieldName;
end; DBGrid1.DataSource.DataSet.First;
j:=2;
while not DBGrid1.DataSource.DataSet.Eof do
begin
for i:=0 to DBGrid1.DataSource.DataSet.FieldCount-1 do
begin
eclApp.Cells(j,i+1):=DBGrid1.DataSource.DataSet.Fields[i].Value;
end;
DBGrid1.DataSource.DataSet.Next;
inc(j);
end; WorkBook.SaveAS(xlsFileName);
WorkBook.close;
except
ShowMessage('文件导出失败!');
Exit;
end;
Application.MessageBox('保存成功!','恭喜',MB_OK + MB_ICONINFORMATION);
end;可以自己命名Excel文件名,有DBGrid1导出为Excel文档!
不显示Excel,其实就是不打开Excel,只要不启用打开程序就可以了!