如何在Delphi5中把報表輸出為Excel格式(30分)

  • 主题发起人 主题发起人 chenhaipeng
  • 开始时间 开始时间
procedure TForm3.BitBtn2Click(Sender: TObject);
var f:textfile;
fs,str:string;
i,j,k:integer;
begin
if not query1.IsEmpty then
begin
if savedialog1.execute then
begin
fs:=savedialog1.filename;
assignfile(f,fs);
rewrite(f);
for i:=0 to query1.FieldCount-1 do
begin
begin
str:=query1.Fields.Displaylabel;
for j:=query1.Fields.datasize to 35 do
begin
str:=str+' ';
end;
write(f,str);
end;
end;
writeln(f,'');
query1.first;
for i:=0 to query1.RecordCount-1 do
begin
for j:=0 to query1.FieldCount-1 do
begin
str:=query1.Fields[j].Displaytext;
for k:=query1.Fields[j].datasize to 35 do
begin
str:=str+' ';
end;
write(f,str);
end;
writeln(f,'');
query1.next;
end;
filestr1:=fs;
closefile(f);
end;
end
else
begin
showmessage('empty table');
end;
query1.first;
end;

保存成excel类型的文件
 
kingqc:
多謝你的答案,但是我是想把預覽(preview)后的內容輸出到Excel件,並非把query的內容輸出到輸出.
望你多指點,好用的話,我再加100分.
 
请看下面的代码:

unit UseExcelExample;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComObj, StdCtrls, OleServer, Excel97;

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
ExcelApplication,ExcelWorkSheet,SheetColumn:Variant;
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
ExcelApplication:=CreateOleObject('Excel.Application');
//装了Excel之后注册表中就有Excel.Application项,在HKEY_CLASSES_ROOT下

ExcelApplication.Visible:=true;

ExcelApplication.caption:='Excel 标题';

ExcelApplication.workbooks.add(xlWorksheet);//加入一个WorkBook
//若add后面不加参数,则缺省生成含有3个WorkSheet 的WorkBook
//add后面加参数xlWorksheet,则生成含有1个WorkSheet 的WorkBook (=1好象也有同样的效果)

ExcelApplication.workbooks[1].sheets.add;//加入一个WorkSheet
//加入n个WorkSheet:ExcelApplication.workbooks[1].sheets.add(,,n,);

ExcelWorkSheet:=ExcelApplication.workbooks[1].sheets[1];
ExcelWorkSheet.name:='name';

//改变列属性
SheetColumn:=ExcelWorkSheet.Columns[1];
SheetColumn.ColumnWidth:=20;
SheetColumn.Font.Color:=clBlue;
SheetColumn.Font.Name:='宋体';
SheetColumn.Font.Size:=16;
SheetColumn.Font.Bold:=true;//粗体
SheetColumn.Font.Italic:=true;
SheetColumn.Font.UnderLine:=true;


//单元格赋值
ExcelWorkSheet.cells[1,1]:='s222';//写一个字符串
ExcelWorkSheet.cells[2,1]:=now;//写一个时间值
ExcelWorkSheet.cells[2,3]:=12345;//写一个整数
ExcelWorkSheet.cells[3,3]:=111.432;//写一个浮点数
//最好都转换成字符串,要不然excel可能会自动给你加一些¥之类的符号

ExcelWorkSheet.cells[3,4]:='=Sum(c2,c3)';//写一个公式

//改变单元格属性
ExcelWorkSheet.cells[2,3].Font.Color:=clRed;
ExcelWorkSheet.Range['a1:e6'].Borders.LineStyle:=xlContinuous;//画格
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
//关闭Excel
if not VarIsEmpty(ExcelApplication) then
begin
ExcelApplication.DisplayAlerts:=false;//Excel关闭前不进行保存提示
ExcelApplication.Quit;
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
//访问WorkSheet里的数据
ShowMessage(ExcelWorkSheet.cells.item[2,1]);
end;

end.
 
WORKSHEET 这样能释放干净吗?
不信执行后 看看程序列表中是否还有EXCEL
 
我不知道的是如何訪問我預覽后的數據,因為我在DetailBand里面的BeforePrint事件寫代碼,所以實際preview(預覽)出來的數據,與Query里面的數據不太一樣我該怎樣得到預覽后的數據呢!
 
强烈关注
 
Report Builder 5 有一个叫作ExtraDevice for Report Builder的东东,
配合ReportBuilder 5.x使用,可将报表直接输出到Lotus, Quattro, Excel, Bitmap, JPEG, CSS2, HTML, PDF 和 RTF 等多种格式。
http://www.ocloud.com/download/dlview.php?kind=report有下载
 
q-report profasional 有个FILTER FOR EXCEL的,不过效果一般
 
接受答案了.
 
后退
顶部