如果你不要求有EXCEL的格式,比如字体等,那么用下面的方法: var i:integer; s:string; begin s:=''; Query1.First; while not Query1.Eof do begin for i:=0 to Query1.FieldCount-1 do begin s:=s+','+Query1.Fields[i].AsString; end; Memo1.Lines.Add(s); i:=0; s:=''; Query1.Next; end; Memo1.Lines.SaveToFile('c:\ff.csv');
EXCEL可以打开
我自己就用了几种
EXCELRPT204
xls121等等
(将数据导入到Excel文件,然后利用Delphi自带的控件调用Excel打开它)
var i,j,k,l,m,n:integer;ColumnRange,qzw,qzw1,QZW2: Variant;begin
excelapplication1.Connect;
excelapplication1.Caption:='123';
excelapplication1.Visible[0]:=true;
excelapplication1.Workbooks.Add(null,0);
excelworkbook1.ConnectTo(excelapplication1.Workbooks[1]);
excelworksheet1.ConnectTo(excelworkbook1.Worksheets[1] as _worksheet);
excelworksheet2.ConnectTo(excelworkbook1.Worksheets[2] as _worksheet);
excelworksheet3.ConnectTo(excelworkbook1.Worksheets[3] as _worksheet);
excelworksheet2.Activate;
excelworksheet3.Name:='zwtlzr3';
excelworksheet2.Name:='zwtlzr2';
excelworksheet1.Name:='zwtlzr1';
excelworksheet2.Columns.Font.Name:='黑体';
//excelworksheet2.Cells.Item[1,1]:='zw';
//excelworksheet2.Cells.Item[1,2]:='zw1';
qzw:=excelworksheet2.rows;
QZW1:=excelworksheet2.Range['A1','e1'];
QZW1.MERGE;
qzw1:=excelworksheet2.Range['A2','a10'];
qzw1.Merge;
qzw1:=excelworksheet2.Range['A2','a1'];
qzw1.Interior.ColorIndex:=3;
qzw.rows[1].rowheight:=30;
qzw.rows[1].font.color:=clblue;
qzw.rows[1].font.bold:=true;
qzw.rows[1].font.underline:=true;
QZW.ROWS[11].PAGEBREAK:=1;
excelworksheet2.PageSetup.PrintTitleRows:='$12:$12';
ColumnRange:= excelworksheet2.Columns;
ColumnRange.Columns[3].ColumnWidth:=30;
ColumnRange.Columns[3].font.name:='隶书';
ColumnRange.Columns[3].font.color:=clblue;
excelworksheet2.PageSetup.PrintGridlines:=true;
excelworksheet2.PageSetup.PaperSize:=XLPAPERA4;
//excelworksheet2.PageSetup.PaperSize:=XLPAPERuser;
excelworksheet2.PageSetup.CenterFooter:='第abc&P页共&N页&d&t&f&a';
excelworksheet2.PageSetup.PrintTitleRows:='$1:$2';
excelworksheet2.PageSetup.Orientation:=XLLANDSCAPE;
//ColumnRange.Columns[1].numberformatlocal:='G/通用格式';
//ColumnRange.Columns[1].numberformatlocal:='@';
excelworksheet2.PageSetup.CenterHeader:= '&"隶书"报表演示'#13+'1213';
table1.Close;
table1.DatabaseName:='e:\zz';
table1.TableName:='qq.dbf';
table1.TableType:=ttfoxpro;
table1.Open;
j:=2;
k:=3;
excelworksheet2.Cells.Item[2,1]:='123';
excelworksheet2.Cells.Item[2,1].Orientation:= xlVertical;
excelworksheet2.Cells.Item[2,1].VerticalAlignment:= xlVAlignCenter;
excelworksheet2.Cells.Item[2,2]:='11';
excelworksheet2.Cells.Item[2,3]:='22';
excelworksheet2.Cells.Item[2,4]:='33';
excelworksheet2.Cells.Item[2,5]:='44';
for i:=1 to 100 do
begin
excelworksheet2.Cells.Item[k,2]:=table1.Fields[0].AsString;
excelworksheet2.Cells.Item[k,3]:=table1.Fields[1].AsString;
excelworksheet2.Cells.Item[k,4].numberformatlocal:='0.00';
excelworksheet2.Cells.Item[k,4]:=table1.Fieldbyname('11').AsFloat;
excelworksheet2.Cells.Item[k,5]:=table1.Fieldbyname('22').AsFloat;
excelworksheet2.Cells.Item[k,5].numberformatlocal:='0.00';
//excelworksheet2.Cells.Item[k,1].numberformatlocal:='0';
if i=10 then excelworksheet2.PageSetup.PrintTitleRows:='$12:$12';k:=k+1;
table1.Next;
end;
excelworksheet2.Cells.Item[k+1,2]:='合 计';
excelworksheet2.Cells.Item[k+1,4]:='=sum(d1:d100)';
excelworksheet2.Cells.Item[k+1,5]:='=sum(e1:e100)';
excelworksheet2.Cells.Item[k+1,4].numberformatlocal:='0.00';
excelworksheet2.Cells.Item[k+1,5].numberformatlocal:='0.00';
excelworksheet2.Cells.Item[1,2].font.name:='黑体';
excelworksheet2.Cells.Item[1,1]:='yyyy';
excelworksheet2.Cells.Item[1,1].HorizontalAlignment := xlCenter;
excelworksheet2.Cells.Item[1,1].font.size:=20;
ColumnRange.Columns[4].numberformatlocal:='#,##0.00';
excelworksheet2.Cells.Item[105,3]:=excelworksheet2.PageSetup.CenterHeader;
excelworksheet2.Cells.Item[105,4]:=Pos(#13,excelworksheet2.PageSetup.CenterHeader);
excelworksheet2.Cells.Item[105,5]:='&p';
excelworksheet2.Protect('123',true,true,true,true);
excelworksheet2.PrintPreview;
excelworkBOOK1.Saved[1]:=TRUE;
QZW2:='12345';
excelworksheet2.SaveAs('e:\txt\123.xls',XLEXCEL9795,'789');
var i:integer;
s:string;
begin
s:='';
Query1.First;
while not Query1.Eof do
begin
for i:=0 to Query1.FieldCount-1 do
begin
s:=s+','+Query1.Fields[i].AsString;
end;
Memo1.Lines.Add(s);
i:=0;
s:='';
Query1.Next;
end;
Memo1.Lines.SaveToFile('c:\ff.csv');