寒
寒与凉
Unregistered / Unconfirmed
GUEST, unregistred user!
把一个DATASET导出成EXCEL文件如下: 有两个问题
一:如何把EXCEL中的任意一列设为文本或者其他方式而不是用常规显示?
二:怎么能把导出EXCEL存入任意的位置?
procedure TForm1.BitBtn1Click(Sender: TObject);
var
ExcelApplication1: TExcelApplication;
ExcelWorksheet1: TExcelWorksheet;
ExcelWorkbook1: TExcelWorkbook;
i, j: integer;
filename: string;
begin
filename := concat(extractfilepath(application.exename), Name, '.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);
query1.First;
for j := 0 to query1.Fields.Count - 1 do
begin
//设置启始行的位置和字体
ExcelWorksheet1.Cells.item[1, j + 1] := query1.Fields[j].DisplayLabel;
ExcelWorksheet1.Cells.item[1, j + 1].font.size := '10';
end;
for i := 2 to query1.RecordCount + 1 do
begin
for j := 0 to query1.Fields.Count - 1 do
begin
ExcelWorksheet1.Cells.item[i, j + 1] :=
query1.Fields[j].Asstring;
ExcelWorksheet1.Cells.item[i, j + 1].font.size := '10';
end;
query1.Next;
end;
ExcelWorksheet1.Columns.AutoFit;
ExcelWorksheet1.SaveAs(filename);
Application.Messagebox(pchar('数据成功导出' + filename), '系统消息',
mb_Ok);
finally
ExcelApplication1.Disconnect;
ExcelApplication1.Quit;
ExcelApplication1.Free;
ExcelWorksheet1.Free;
ExcelWorkbook1.Free;
end;
end;
一:如何把EXCEL中的任意一列设为文本或者其他方式而不是用常规显示?
二:怎么能把导出EXCEL存入任意的位置?
procedure TForm1.BitBtn1Click(Sender: TObject);
var
ExcelApplication1: TExcelApplication;
ExcelWorksheet1: TExcelWorksheet;
ExcelWorkbook1: TExcelWorkbook;
i, j: integer;
filename: string;
begin
filename := concat(extractfilepath(application.exename), Name, '.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);
query1.First;
for j := 0 to query1.Fields.Count - 1 do
begin
//设置启始行的位置和字体
ExcelWorksheet1.Cells.item[1, j + 1] := query1.Fields[j].DisplayLabel;
ExcelWorksheet1.Cells.item[1, j + 1].font.size := '10';
end;
for i := 2 to query1.RecordCount + 1 do
begin
for j := 0 to query1.Fields.Count - 1 do
begin
ExcelWorksheet1.Cells.item[i, j + 1] :=
query1.Fields[j].Asstring;
ExcelWorksheet1.Cells.item[i, j + 1].font.size := '10';
end;
query1.Next;
end;
ExcelWorksheet1.Columns.AutoFit;
ExcelWorksheet1.SaveAs(filename);
Application.Messagebox(pchar('数据成功导出' + filename), '系统消息',
mb_Ok);
finally
ExcelApplication1.Disconnect;
ExcelApplication1.Quit;
ExcelApplication1.Free;
ExcelWorksheet1.Free;
ExcelWorkbook1.Free;
end;
end;