对excel的读写:
unit UMain;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Db, DBTables, StdCtrls, Grids, DBGrids,Excel97,Comctrls,OleCtnrs,ComObj;
type
TForm1 = class(TForm)
DBGrid1: TDBGrid;
Button1: TButton;
Button2: TButton;
DataSource1: TDataSource;
Table1: TTable;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
XlsApp,XlsSheet,XlsWBk : Variant;
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
var
I,J : integer;
begin
if VarIsEmpty(XlsApp) then
XlsApp := CreateOleObject('Excel.Application');
XLsApp.Workbooks.Add;
XlsSheet := XLsApp.Worksheets['Sheet1'];
for I := 0 to Table1.Fields.Count - 1 do
begin
XlsSheet.Cells[3,I + 1] := dbgrid1.Columns[I].Title.Caption;
end;
Table1.first;
for J := 0 to Table1.RecordCount - 1 do
begin
for I := 0 to Table1.Fields.Count - 1 do
begin
XlsSheet.Cells[J + 4,I + 1] := Table1.Fields[I].AsString;
end;
Table1.Next;
end;
XlsApp.Visible := true;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
I,J : integer;
TxtFile : TextFile;
TmpString : String;
begin
try
if VarIsEmpty(XlsApp) then
XlsApp := CreateOleObject('Excel.Application');
XlsSheet := XlsApp.workbooks.open('c:\my documents\book3.xls');
AssignFile(TxtFile,'C:\My Documents\Test.txt');
Rewrite(TxtFile);
try
for I := 3 to 21 do
begin
TmpString := '';
for J := 1 to 5 do
begin
TmpString := TmpString + XlsSheet.ActiveSheet.Cells[I,J].Text + '|';
end;
Writeln(TxtFile,Tmpstring);
end;
finally
CloseFile(TxtFile);
end;
XlsApp.Visible := true;
except
XlsSheet.close;
XlsApp.Application.quit;
XlsApp := Unassigned;
XlsSheet := Unassigned;
end;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if not VarIsEmpty(XlsApp) then
begin
XlsApp.DisplayAlerts := True; // 7Discard unsaved files....
try
XlsApp.Application.Quit;
except
end;
end;
end;
end.
保存DBGRID数据到EXCEL文件中
精选 转载文章标签 保存DBGRID数据到EXCEL文件中 文章分类 办公效率
上一篇:delphi DBGrid的数据导出到Excel文件保存
下一篇:微软下载主页
![](https://ucenter.51cto.com/images/noavatar_middle.gif)
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
java oss文件保存本地Excel java保存文件到数据库
发现环信的根据时间条件拉取历史消息接口已经停用,就做了个通过导出聊天记录接口保存到数据库实体的功能,分享一下. 大致的思路: 1.通过环信的接口,把前一天24小时的数据压缩包下载到本地 2.把下载后的文
java oss文件保存本地Excel 环信聊天记录 java json List