var
i:integer;
str:String;
StrList:TStringList;//用于存储数据的字符列表
begin
try
with Table1 do//可以是TABLE,也可以是QUERY等等
begin
First;//指针至表头
DisableControls;
while not EOF do
begin
str:=';
for i:=0 to FieldCount-1 do
begin
str:=str+Fields[i].AsString+#19;
StrList.Add(str);
Next;
end;
StrList.SaveToFile('c:\a.xls');//存储路径
EnableControls;
end
finally
StrList.free;//释放内存
end;
end;
i:integer;
str:String;
StrList:TStringList;//用于存储数据的字符列表
begin
try
with Table1 do//可以是TABLE,也可以是QUERY等等
begin
First;//指针至表头
DisableControls;
while not EOF do
begin
str:=';
for i:=0 to FieldCount-1 do
begin
str:=str+Fields[i].AsString+#19;
StrList.Add(str);
Next;
end;
StrList.SaveToFile('c:\a.xls');//存储路径
EnableControls;
end
finally
StrList.free;//释放内存
end;
end;
解决方案 »
- Delphi 中 & 符号有什么作用
- delphi编辑器里,按“'”键,不出来,按两下就出来两个,怎么设置成按一下就出来一个
- 在FOR 循环中,遇到异常时,如何继续往下循环?
- ------------------李维·Borland传奇 一书 下载 快,要删了-----------------------
- 用Quickrep预览怎么使他出来就是zoomto100的状态,而不是zoomtowidth的状态?
- 有关操作不同表时SQL语句不能正常执行的问题?
- fastreport:打印合计数的问题!急(在线!!!)
- 一个菜问题。
- 问一个简单的函数使用
- 如何在DBGrid中实现转行(搞定再给200分)
- 求助解决哲学家就餐问题
- 怎样按段时间备份sql server数据库表中的数据?
以下是将DBGrid内容导入EXECL:
procedure ExportDBGrid(toExcel: Boolean);
var
bm: TBook;
col, row: Integer;
sline: String;
mem: TMemo;
ExcelApp: Variant;
begin
Screen.Cursor := crHourglass;
DBGrid1.DataSource.DataSet.DisableControls;
bm := DBGrid1.DataSource.DataSet.GetBook;
DBGrid1.DataSource.DataSet.First;
// create the Excel object
if toExcel then
begin
ExcelApp := CreateOleObject('Excel.Application');
ExcelApp.WorkBooks.Add(xlWBatWorkSheet);
ExcelApp.WorkBooks[1].WorkSheets[1].Name := 'Grid Data';
end;
// First we send the data to a memo
// works faster than doing it directly to Excel
mem := TMemo.Create(Self);
mem.Visible := false;
mem.Parent := MainForm;
mem.Clear;
sline := '';
// add the info for the column names
for col := 0 to DBGrid1.FieldCount-1 do
sline := sline + DBGrid1.Fields[col].DisplayLabel + #9;
mem.Lines.Add(sline);
// get the data into the memo
for row := 0 to DBGrid1.DataSource.DataSet.RecordCount-1 do
begin
sline := '';
for col := 0 to DBGrid1.FieldCount-1 do
sline := sline + DBGrid1.Fields[col].AsString + #9;
mem.Lines.Add(sline);
DBGrid1.DataSource.DataSet.Next;
end;
// we copy the data to the clipboard
mem.SelectAll;
mem.CopyToClipboard;
// if needed, send it to Excel
// if not, we already have it in the clipboard
if toExcel then
begin
ExcelApp.Workbooks[1].WorkSheets['Grid Data'].Paste;
ExcelApp.Visible := true;
end;
FreeAndNil(mem);
// FreeAndNil(ExcelApp);
DBGrid1.DataSource.DataSet.GotoBook(bm);
DBGrid1.DataSource.DataSet.FreeBook(bm);
DBGrid1.DataSource.DataSet.EnableControls;
Screen.Cursor := crDefault;
end;
可以导出多种格式文件,word,excel,htm...
给你一个例子
procedure TFrmMain.WriteExcel(AdsData: TADODataSet; sName, Title: string);
var
ExcelApplication1: TExcelApplication;
ExcelWorksheet1: TExcelWorksheet;
ExcelWorkbook1: TExcelWorkbook;
i, j: integer;
filename: string;
begin
filename := concat(extractfilepath(application.exename), sName, ’.xls’);
try
ExcelApplication1 := TExcelApplication.Create(Application);
ExcelWorksheet1 := TExcelWorksheet.Create(Application);
ExcelWorkbook1 := TExcelWorkbook.Create(Application);
ExcelApplication1.Connect;
except
Application.Messagebox(’Excel 没有安装!’, ’Hello’, MB_ICONERROR + mb_Ok);
Abort;
end;
try
ExcelApplication1.Workbooks.Add(EmptyParam, 0);
ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]);
ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _worksheet);
AdsData.First;
for j := 0 to AdsData.Fields.Count - 1 do
begin
ExcelWorksheet1.Cells.item[3, j + 1] := AdsData.Fields[j].DisplayLabel;
ExcelWorksheet1.Cells.item[3, j + 1].font.size := ’10’;
end;
for i := 4 to AdsData.RecordCount + 3 do
begin
for j := 0 to AdsData.Fields.Count - 1 do
begin
ExcelWorksheet1.Cells.item[i, j + 1] :=
AdsData.Fields[j].Asstring;
ExcelWorksheet1.Cells.item[i, j + 1].font.size := ’10’;
end;
AdsData.Next;
end;
ExcelWorksheet1.Columns.AutoFit;
ExcelWorksheet1.Cells.item[1, 2] := Title;
ExcelWorksheet1.Cells.Item[1, 2].font.size := ’14’;
ExcelWorksheet1.SaveAs(filename);
Application.Messagebox(pchar(’数据成功导出’ + filename), ’Hello’,
mb_Ok);
finally
ExcelApplication1.Disconnect;
ExcelApplication1.Quit;
ExcelApplication1.Free;
ExcelWorksheet1.Free;
ExcelWorkbook1.Free;
end;
end;