这是以前做的一个导到EXCL中去的。你想办法改一下吧。 function TFrmDanEdit.PrintInfo( ):boolean; var bt, et:TDateTime; i,i2,row,column:integer; s:string; begin Try ExcelApplication1.Connect; Except MessageDlg('Excel may not be installed', mtError, [mbOk], 0); Abort; End; ExcelApplication1.Visible[0]:=True; ExcelApplication1.Caption:='Excel Application'; ExcelApplication1.Workbooks.Add(Null,0); ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]); ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _Worksheet); qload1.First; row:=1; While Not(qload1.Eof) do//控制行数 begin column:=1; for i:=0 to dbgrid1.FieldCount-1 do //控制列数 begin s:=dbgrid1.Columns.Items[i].FieldName; if s='smethod' then s:='bowner'; ExcelWorksheet1.Cells.Item[row+1,column].ColumnWidth:=16; //这里ROW+1是让在EXCL表格中在最上面空出一行来 ExcelWorksheet1.Cells.Item[row+1,column]:=qload1.fieldbyname(s).AsString; column:=column+1; end; qload1.Next; row:=row+1; end;
for i2:=1 to dbgrid1.FieldCount do //取出字段名放到第一条上去i2控制列名 begin excelworksheet1.Cells.Item[1,i2]:= dbgrid1.Columns.Items[i2-1].Title.Caption;//dbgrid1.Columns.Items[i2-1].FieldName;//取出字段名 end; //取出字段名别名 ExcelApplication1.Disconnect; //关掉与EXCEL的连接
function TFrmDanEdit.PrintInfo( ):boolean;
var bt, et:TDateTime;
i,i2,row,column:integer;
s:string;
begin Try
ExcelApplication1.Connect;
Except
MessageDlg('Excel may not be installed',
mtError, [mbOk], 0);
Abort;
End;
ExcelApplication1.Visible[0]:=True;
ExcelApplication1.Caption:='Excel Application';
ExcelApplication1.Workbooks.Add(Null,0);
ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]);
ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _Worksheet);
qload1.First;
row:=1;
While Not(qload1.Eof) do//控制行数
begin
column:=1; for i:=0 to dbgrid1.FieldCount-1 do //控制列数
begin s:=dbgrid1.Columns.Items[i].FieldName;
if s='smethod' then
s:='bowner'; ExcelWorksheet1.Cells.Item[row+1,column].ColumnWidth:=16;
//这里ROW+1是让在EXCL表格中在最上面空出一行来
ExcelWorksheet1.Cells.Item[row+1,column]:=qload1.fieldbyname(s).AsString; column:=column+1;
end;
qload1.Next;
row:=row+1;
end;
for i2:=1 to dbgrid1.FieldCount do //取出字段名放到第一条上去i2控制列名
begin
excelworksheet1.Cells.Item[1,i2]:= dbgrid1.Columns.Items[i2-1].Title.Caption;//dbgrid1.Columns.Items[i2-1].FieldName;//取出字段名
end; //取出字段名别名
ExcelApplication1.Disconnect; //关掉与EXCEL的连接
Excel 有方法.
但如何
將DBGrid中的文本导到TXT文件?
將DBGrid中的文本导到Word文件?
超级猛料里有===============================================
这个控件是Kingron编写的,版权所有,Kingron保留所有权利。请保留控件的版权信息,尊重作者的辛勤劳动。这个控件支持Delphi5和Delphi6,在Word2000下面测试通过。不可未经授权,擅自散发!安装:把附件保存,然后用Delphi打开这个GridToWord.Pas文件,选择Delphi菜单--〉Component-->Install Component,然后选择Install即可。安装之后,在控件面板的Samples页面上面,会有一个GridToWord控件,把它放到窗体上面,然后设置它的Grid属性即可,此后,你就可以调用她的ExportToWord方法。就这么简单,熟悉之后,你可以试着设置一些复杂的属性,其他的自己摸索吧,如果不希望控件设置表格的属性,只希望有一个干净的Word表格,请设置控件的TableFormat的Style为tfDefault即可。这样你会得到一个完全没有任何格式的表格。控件功能:
能够现实进度条;提供事件支持(OnProgress);能够自动设置表格格式(支持Word所支持的40多个预定义“自动套用格式”,并且允许设置自动套用格式的应用范围);支持StringGrid和DBGrid;能够设置字体、对齐方式等常见格式;支持表头、支持标题和表格主体,并且可以分别设置格式;预计新的版本提供更强大的功能:
允许设置每一个单元格的格式!支持更多的事件,并且允许在事件中对导出进行更多更复杂的控制!
begin
WordFont1.connectTo(WordApplication1.Selection.Font);
WordApplication1.Selection.TypeText('湖南省长沙市邦蝶软件开发有限公司路线统计表');
WordApplication1.Selection.TypeParagraph;
WordApplication1.Selection.TypeText('湖南长沙至湖北武汉');
WordApplication1.Selection.TypeParagraph;
WordApplication1.Selection.TypeText('使用部门:'+'软件开发部'+#13);
//
ParaFmt.ConnectTo(Doc.Paragraphs.Item(1).format);
wordfont1.ConnectTo(Doc.Paragraphs.Item(1).range.font);
parafmt.Alignment:=wdAlignparagraphCenter;
parafmt.SpaceAfter:=2;
parafmt.Borders.Item(3).linestyle:=wdlinestyleSingle;
//parafmt.Borders.Item(3).linestyle:=wdlinestylebuoble;
wordfont1.Size:=18;
wordfont1.Name:='楷体_GB2312';
wordfont1.Bold:=integer(true);
wordfont1.Colorindex:=0;
//
ParaFmt.ConnectTo(Doc.Paragraphs.Item(2).format);
wordfont1.ConnectTo(Doc.Paragraphs.Item(2).range.font);
parafmt.Alignment:=wdAlignparagraphCenter; //中间
parafmt.SpaceAfter:=0; //行间隔大小
wordfont1.Size:=14; //字体大小
wordfont1.Name:='楷体_GB2312'; //字体
wordfont1.Bold:=integer(true);//字体粗细
//
ParaFmt.ConnectTo(Doc.Paragraphs.Item(3).format);
wordfont1.ConnectTo(Doc.Paragraphs.Item(3).range.font);
parafmt.Alignment:=wdAlignparagraphjustify; //zuo
parafmt.SpaceAfter:=0; //行间隔大小
wordfont1.Size:=11; //字体大小
wordfont1.Name:='楷体_GB2312'; //字体
wordfont1.Bold:=integer(true);//字体粗细
end;procedure TForm1.Button1Click(Sender: TObject);
var
i,j:integer;
begin
button1.Enabled:=false;
wordapplication1.Connect;
WordApplication1.Options.CheckSpellingAsYouType := False;
WordApplication1.Options.CheckGrammarAsYouType := False;
doc.ConnectTo(wordapplication1.Documents.Addold(emptyparam,emptyparam));
wordapplication1.visible:=true;
WordApplication1.Caption := 'Delphi automation';
entertext;
WordApplication1.Selection.SetRange(100,100);
doc.Tables.Addold(WordApplication1.Selection.Range,DBGrid1.DataSource.DataSet.RecordCount+1,DBGrid1.Columns.Count);
doc.Tables.Item(1).columns.item(1).width:=100;
doc.Tables.Item(1).rows.item(1).shading.BackgroundPatternColor:=wdColorGray125;
// doc.Tables.Item(1).rows.item(1).ParagraphFormat.Alignment :=wdAlignParagraphCenter;
//doc.Tables.Item(1).Cell(1,1).VerticalAlignment:= wdCellAlignVerticalCenter;
for i:=1 to DBGrid1.Columns.Count do begin
doc.Tables.Item(1).Cell(1,i).Range.InsertAfter(DBGrid1.Columns[i-1].Title.Caption);
doc.Tables.Item(1).cell(1,i).Range.Bold:=integer(true);
wordfont1.ConnectTo(doc.Tables.item(1).cell(1,i).Range.Font);
wordfont1.Size:=10.5; //字体大小
wordfont1.Name:='楷体_GB2312'; //字体
end;
i:=2;
with DBGrid1.DataSource.DataSet do
while not eof do
begin
for j:=1 to DBGrid1.Columns.Count do
doc.Tables.Item(1).Cell(i,j).Range.InsertAfter(DBGrid1.Columns[j-1].Field.Value);
Next;
Inc(i);
end;
doc.pagesetup.TopMargin:=30;
doc.PageSetup.set_gutter(10);
doc.PageSetup.set_gutterontop(true);
// doc.Save;
//wordapplication1.Disconnect;
//wordapplication1.PrintPreview;
// wordapplication1.Quit;
//button1.Enabled:=true;
//close;
// doc.ActiveWindow.activepane.view.seekview:=wdseekcurrentpageheader;
{ doc.Application.Selection.ParagraphFormat.Alignment:=wdAlignparagraphCenter;
doc.application.Selection.TypeText('这是我第一次做叶眉');
doc.ActiveWindow.activepane.view.seekview:=wdseekmaindocument;}
end;
end.
thanks
樓上,看來我們國內的程序員還是拿來主議的多,真正研究技術的沒幾個.... Red00x() ,支持你一下,也給小弟一份,[email protected]