近,得坛中一高人指点,喜得此代码.
但苦于导出数据,只有表格与标题?请牛人们来看看调调.谢.//DBGRID导出到WORD文件
procedure OutWord(DBGrid1:TDBGrid);
var
i,j:integer;
begin
WordApplication1:=TWordApplication.Create(application);
WordFont1:=TWordFont.Create(application);
Doc:=TWordDocument.Create(application);
ParaFmt:=TWordParagraphFormat.Create(application);
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:='宋体';
end;
i:=2;
with DBGrid1.DataSource.DataSet do
while not eof do
begin
for j:=1 to DBGrid1.Columns.Count do
if DBGrid1.Columns[j-1].Field.Value<>Null then
if DBGrid1.Columns[j-1].Field.Value<>'' then
if Length(DBGrid1.Columns[j-1].Field.Value)<>0 then
Doc.Tables.Item(1).Cell(i,j).Range.InsertAfter(DBGrid1.Columns[j-1].Field.Value);
Next;
Inc(i);
end;
Doc.pagesetup.TopMargin:=9;
Doc.PageSetup.LeftMargin:=15;
Doc.PageSetup.RightMargin:=15;
Doc.PageSetup.set_gutter(15);
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;//导出到EXCEL文件
procedure OutExcel(Title: String; DBGrid: TDBGrid; Total: Boolean);
var
ExcelApp, WorkBook: Variant;
i, j: Integer;
Row, Col: Integer;
FieldName: string;
DataSet: TDataSet;
S: String;
begin
try
ExcelApp := CreateOleObject('Excel.Application');
WorkBook := CreateOleObject('Excel.Sheet');
except
Application.MessageBox('你的机器里未安装Microsoft Excel.', '', 32);
Exit;
end; Application.ProcessMessages;
WorkBook := ExcelApp.WorkBooks.Add;
Col := 1;
ExcelApp.Cells(2, Col) := Title;
Row := 4;
DataSet := DBGrid.DataSource.DataSet;
for I := 0 to DBGrid.Columns.Count - 1 do
begin
if DBGrid.Columns[I].Visible then
begin
FieldName := DBGrid.Columns[I].Title.Caption;
ExcelApp.Cells(Row, Col) := FieldName;
Col := Col + 1;
end;
end; Row := Row + 1; DataSet.First;
while not DataSet.Eof do
begin
Col := 1;
for J := 0 to DBGrid.Columns.Count - 1 do
begin
FieldName := DBGrid.Columns[J].FieldName;
ExcelApp.Cells(Row, Col) := ' ' + DataSet.FieldByName(FieldName).AsString + ' ';
Col := Col + 1;
end;
Row := Row + 1;
DataSet.Next;
end; if Total then
begin
Col := 1;
for J := 0 to DBGrid.Columns.Count - 1 do
begin
S := Char(64 + ((J+1) mod 26));
if (J+1) > 26 then
begin
S := Char(65+(((J+1)-26) div 26)) + S;
end;
if J = 0 then
begin
ExcelApp.Cells(Row, Col) := '合计';
end
else if DBGrid.Columns[J].Field.DataType in [ftInteger, ftSmallint, ftFloat, ftBCD] then
begin
FieldName := DBGrid.Columns[J].FieldName;
ExcelApp.Cells(Row, Col) := '=SUM('+S+'4:'+S+IntToStr(Row-1)+')';
end;
Col := Col + 1;
end;
end;
ExcelApp.Visible := True;
// WorkBook.SaveAs(SaveDialog1.FileName);
// WorkBook.Close;
// ExcelApp.Quit;
// ExcelApp := Unassigned;
end;
但苦于导出数据,只有表格与标题?请牛人们来看看调调.谢.//DBGRID导出到WORD文件
procedure OutWord(DBGrid1:TDBGrid);
var
i,j:integer;
begin
WordApplication1:=TWordApplication.Create(application);
WordFont1:=TWordFont.Create(application);
Doc:=TWordDocument.Create(application);
ParaFmt:=TWordParagraphFormat.Create(application);
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:='宋体';
end;
i:=2;
with DBGrid1.DataSource.DataSet do
while not eof do
begin
for j:=1 to DBGrid1.Columns.Count do
if DBGrid1.Columns[j-1].Field.Value<>Null then
if DBGrid1.Columns[j-1].Field.Value<>'' then
if Length(DBGrid1.Columns[j-1].Field.Value)<>0 then
Doc.Tables.Item(1).Cell(i,j).Range.InsertAfter(DBGrid1.Columns[j-1].Field.Value);
Next;
Inc(i);
end;
Doc.pagesetup.TopMargin:=9;
Doc.PageSetup.LeftMargin:=15;
Doc.PageSetup.RightMargin:=15;
Doc.PageSetup.set_gutter(15);
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;//导出到EXCEL文件
procedure OutExcel(Title: String; DBGrid: TDBGrid; Total: Boolean);
var
ExcelApp, WorkBook: Variant;
i, j: Integer;
Row, Col: Integer;
FieldName: string;
DataSet: TDataSet;
S: String;
begin
try
ExcelApp := CreateOleObject('Excel.Application');
WorkBook := CreateOleObject('Excel.Sheet');
except
Application.MessageBox('你的机器里未安装Microsoft Excel.', '', 32);
Exit;
end; Application.ProcessMessages;
WorkBook := ExcelApp.WorkBooks.Add;
Col := 1;
ExcelApp.Cells(2, Col) := Title;
Row := 4;
DataSet := DBGrid.DataSource.DataSet;
for I := 0 to DBGrid.Columns.Count - 1 do
begin
if DBGrid.Columns[I].Visible then
begin
FieldName := DBGrid.Columns[I].Title.Caption;
ExcelApp.Cells(Row, Col) := FieldName;
Col := Col + 1;
end;
end; Row := Row + 1; DataSet.First;
while not DataSet.Eof do
begin
Col := 1;
for J := 0 to DBGrid.Columns.Count - 1 do
begin
FieldName := DBGrid.Columns[J].FieldName;
ExcelApp.Cells(Row, Col) := ' ' + DataSet.FieldByName(FieldName).AsString + ' ';
Col := Col + 1;
end;
Row := Row + 1;
DataSet.Next;
end; if Total then
begin
Col := 1;
for J := 0 to DBGrid.Columns.Count - 1 do
begin
S := Char(64 + ((J+1) mod 26));
if (J+1) > 26 then
begin
S := Char(65+(((J+1)-26) div 26)) + S;
end;
if J = 0 then
begin
ExcelApp.Cells(Row, Col) := '合计';
end
else if DBGrid.Columns[J].Field.DataType in [ftInteger, ftSmallint, ftFloat, ftBCD] then
begin
FieldName := DBGrid.Columns[J].FieldName;
ExcelApp.Cells(Row, Col) := '=SUM('+S+'4:'+S+IntToStr(Row-1)+')';
end;
Col := Col + 1;
end;
end;
ExcelApp.Visible := True;
// WorkBook.SaveAs(SaveDialog1.FileName);
// WorkBook.Close;
// ExcelApp.Quit;
// ExcelApp := Unassigned;
end;
时有时无,生成数据不稳定,不知为何?