如何把一个grid中显示的数据直接输出到word文件(存储为word或txt文件)?100分(100分)

  • 主题发起人 主题发起人 lmy
  • 开始时间 开始时间
L

lmy

Unregistered / Unconfirmed
GUEST, unregistred user!
如何把一个grid中显示的数据直接输出到word文件(存储为word或txt文件)?100分
 
如果你的grid是StringGrid,可以直接取cell
对于DBGrid,可以把他的数据集中每条记录的每个字段asString一下就可以的了
 
good 有很多例子 查一下
 
procedure Texportexcel.BitBtn1Click(Sender: TObject);
var
row,column,Tempcolumn:integer;
i,j:integer;
begin
inherited;
ExcelWorksheet1.Disconnect;
ExcelWorkbook1.Disconnect;
ExcelApplication1.Disconnect;
Try
{Excelapplication1.Connect;
ExcelWorkbook1.Connect;
ExcelWorksheet1.Connect;}
Except
Messagedlg('沒有安裝Excel!'+char(13)+
'請重新安裝!',mtinformation,[mbok],0);
Abort;
end;
Excelapplication1.Visible[0]:=true;
Excelapplication1.Caption:= '數據導出窗口程序';
Excelapplication1.Workbooks.Add(varNull,0);
Excelworkbook1.ConnectTo(excelapplication1.workbooks[excelapplication1.workbooks.count]);
Excelworksheet1.ConnectTo(excelworkbook1.worksheets[1] as _worksheet);
Excelworksheet1.Cells.Item[1,3]:='TOOLING PROGRESS REPORT';
Excelworksheet1.Cells.Item[2,1]:='Customer:';
Excelworksheet1.Cells.Item[2,3]:=dm.xvalue.fieldbyname('customer').value;
Excelworksheet1.Cells.Item[3,1]:='Cust.No:';
Excelworksheet1.Cells.item[3,3]:=dm.xvalue.fieldbyname('custph').value;
Excelworksheet1.Cells.Item[4,1]:='Description';
Excelworksheet1.Cells.Item[4,3]:=dm.xvalue.fieldbyname('custcpname').value;
Excelworksheet1.Cells.item[5,1]:='RSC No.:';
Excelworksheet1.Cells.Item[5,3]:=dm.xvalue.fieldbyname('rscno').value;
Excelworksheet1.Cells.item[6,1]:='Chinese Description:';
Excelworksheet1.Cells.Item[6,3]:=dm.xvalue.fieldbyname('rsccpname').value;
Excelworksheet1.Cells.Item[7,1]:='Start date:';
Excelworksheet1.Cells.Item[7,3]:=dm.xvalue.fieldbyname('rjdate').value;
Excelworksheet1.Cells.item[8,1]:='Estimate Finish Date:';
Excelworksheet1.Cells.Item[8,3]:=dm.xvalue.fieldbyname('rjoverdate').value;
Excelworksheet1.Cells.item[9,1]:='Actual Finish Date';
Excelworksheet1.cells.Item[10,1]:='total days:';
Excelworksheet1.Cells.Item[10,3]:=dm.xvalue.fieldbyname('totaldays').value;
Excelworksheet1.cells.item[11,1]:='Progress Per Day:';
Excelworksheet1.Cells.Item[11,3]:=dm.xvalue.fieldbyname('perday').value+'%';
Excelworksheet1.Cells.Item[12,1]:='Date/Time';
EXCELWORKSHEET1.Cells.Item[12,2]:='PLAN(%)';
Excelworksheet1.Cells.Item[12,3]:='ACTUAL(%)';
Excelworksheet1.Cells.Item[12,4]:='GAP';
Excelworksheet1.Cells.Item[12,5]:='WORKING DAYS';
row:=13;
dm.evalue.First ;
While Not(dm.evalue.Eof) do
begin
TempColumn := 0 ;
for Column:=1 to DbGrid1.FieldCount do
begin
if DbGrid1.Columns.Items[Column-1].Visible = True Then
begin
TempColumn := TempColumn + 1 ;
ExcelWorksheet1.Cells.Item[row,TempColumn] := DbGrid1.Fields[Column-1].AsString;
end;
end;
dm.evalue.Next;
row:=row + 1;
end;
end;
對你絕對有用,如果有用,給分,我太窮了!
 
下面是我写的一个比较完整的输出到WORD的过程,你可能需要稍微改一下以适合你的需要:
procedure DBGridExportToWord(tempGrid: TObject);
var
WordApplication: TWordApplication;
WordDocument: TWordDocument;
WordFont: TWordFont;
WordParagraphFormat: TWordParagraphFormat;

Template,NewTemplate,ItemIndex:OleVariant;
v1,v2,v3: OleVariant;
coli,colii,rowi,i,j:integer;
ClientDataSet:TClientDataSet;

tReportTitle: string;
tPageWidth,tPageHeight,tTopMargin,tBottomMargin,tLeftMargin,tRightMargin: Double;
tXHLen: Double;
tFX: integer;

procedure setfont;
begin
// WordFont.ConnectTo(WordDocument.Sentences.Get_Last.Font);
// if ChkBoxUnderline.checked then WordFont.Underline := 2;
// if ChkBoxBold.checked then WordFont.Bold := 1;
// if ChkBoxItalic.Checked then WordFont.Italic := 1;
// if ChkBoxEmboss.Checked then WordFont.Emboss := 1;
// if ChkBoxEngrave.checked then WordFont.Engrave := 1;
// if ChkBoxShadow.checked then WordFont.shadow := 1;
// if ChkBoxDoublestrike.checked then WordFont.DoubleStrikeThrough := 1;
// if ChkBoxStrike.checked then WordFont.StrikeThrough := 1;
// WordFont.Size := StrToInt(Size.text);
// if Fonttype.Itemindex >= 0 then
// WordFont.Name := FontType.Items[FontType.Itemindex];
end;
begin
//下面的这段是我编写的一段控制WORD页面参数的语句,你可以删除改成自己的
try
WordSetupForm:=TWordSetupForm.Create(nil);
WordSetupForm.ShowModal;
i:=WordSetupForm.ModalResult;
if i=mrOK then
begin
tReportTitle:=WordSetupForm.edtReportTitle.Text;
tPageWidth:=StrToFloat(WordSetupForm.edtPageWidth.Text);
tPageHeight:=StrToFloat(WordSetupForm.edtPageHeight.Text);
tTopMargin:=StrToFloat(WordSetupForm.edtTopMargin.Text);
tBottomMargin:=StrToFloat(WordSetupForm.edtBottomMargin.Text);
tLeftMargin:=StrToFloat(WordSetupForm.edtLeftMargin.Text);
tRightMargin:=StrToFloat(WordSetupForm.edtRightMargin.Text);
tFX:=WordSetupForm.rgFX.ItemIndex;
tXHLen:=StrToFloat(WordSetupForm.edtXHLen.Text);
end;
finally
WordSetupForm.Free;
end;
if i=mrCancel then exit;

//下面正式进入表格打印...这是实际的代码
try
WordApplication:=TWordApplication.Create(nil);
WordDocument:=TWordDocument.Create(nil);
WordParagraphFormat:=TWordParagraphFormat.Create(nil);
WordFont:=TWordFont.Create(nil);

Template := EmptyParam; //这里若使用模板,则应使用模板的绝对路径名
NewTemplate := True;
ItemIndex := 1;
try
WordApplication.Disconnect;
Wordapplication.Connect;
except
MessageDlg('Word 可能还没有安装!', mtError, [mbOk], 0);
Abort;
end;

Wordapplication.Visible := True;
WordApplication.Caption := tReportTitle;
Template := EmptyParam;
NewTemplate := False;
WordApplication.Documents.Add(Template, NewTemplate);
{Assign WordDocument component}
WordDocument.ConnectTo(WordApplication.Documents.Item(ItemIndex));
{Turn Spell checking of because it takes a long time if enabled and slows down Winword}
WordApplication.Options.CheckSpellingAsYouType := False;
WordApplication.Options.CheckGrammarAsYouType := False;

WordApplication.Selection.ParagraphFormat.Alignment := wdAlignParagraphCenter;
WordApplication.Selection.Font.Bold := wdToggle;
WordApplication.Selection.Font.Size := 22;
WordApplication.Selection.TypeText(tReportTitle);
WordApplication.Selection.TypeParagraph;
WordApplication.Selection.Font.Bold := wdToggle;
WordApplication.Selection.Font.Size := 11;
WordApplication.Selection.ParagraphFormat.Alignment := wdAlignParagraphLeft;

if tFX=0 then WordDocument.PageSetup.Orientation:=wdOrientPortrait
else WordDocument.PageSetup.Orientation:=wdOrientLandscape;
WordDocument.PageSetup.TopMargin:=WordApplication.MillimetersToPoints(tTopMargin);
WordDocument.PageSetup.BottomMargin:=WordApplication.MillimetersToPoints(tBottomMargin);
WordDocument.PageSetup.LeftMargin:=WordApplication.MillimetersToPoints(tLeftMargin);
WordDocument.PageSetup.RightMargin:=WordApplication.MillimetersToPoints(tRightMargin);
WordDocument.PageSetup.PageWidth:=WordApplication.MillimetersToPoints(tPageWidth);
WordDocument.PageSetup.PageHeight:=WordApplication.MillimetersToPoints(tPageHeight);

//设置页脚
if WordDocument.ActiveWindow.View.SplitSpecial<>wdPaneNone then
WordDocument.ActiveWindow.Panes.Item(2).Close;
if (WordDocument.ActiveWindow.ActivePane.View.type_=wdNormalView)
or (WordDocument.ActiveWindow.ActivePane.View.type_=wdOutlineView) then
WordDocument.ActiveWindow.ActivePane.View.type_:=wdPrintPreview;
WordDocument.ActiveWindow.ActivePane.View.SeekView:=wdSeekCurrentPageHeader;
if WordApplication.Selection.HeaderFooter.IsHeader then
WordDocument.ActiveWindow.ActivePane.View.SeekView:= wdSeekCurrentPageFooter
else
WordDocument.ActiveWindow.ActivePane.View.SeekView:= wdSeekCurrentPageHeader;
WordApplication.Selection.ParagraphFormat.Alignment := wdAlignParagraphRight;
WordApplication.Selection.TypeText('第');
v1:=wdFieldPage;v2:=EmptyParam;v3:=EmptyParam;
WordApplication.Selection.Fields.Add(WordApplication.Selection.Range,v1,v2,v3);
WordApplication.Selection.TypeText('/');
v1:=wdFieldNumPages;v2:=EmptyParam;v3:=EmptyParam;
WordApplication.Selection.Fields.Add(WordApplication.Selection.Range,v1,v2,v3);
WordApplication.Selection.TypeText('页');
v1:=wdLine; v2:=EmptyParam;
WordApplication.Selection.HomeKey(v1,v2);
v1:=wdLine; v2:=wdExtend;
WordApplication.Selection.EndKey(v1,v2);
WordApplication.Selection.Borders.Item(wdBorderTop).LineStyle:=WordApplication.Options.DefaultBorderLineStyle;
WordApplication.Selection.Borders.Item(wdBorderTop).LineWidth:=wdLineWidth050pt;
WordApplication.Selection.Borders.Item(wdBorderTop).ColorIndex:=WordApplication.Options.DefaultBorderColorIndex;

WordApplication.ActiveWindow.ActivePane.View.SeekView := wdSeekCurrentPageHeader;
v1:=wdLine; v2:=EmptyParam;
WordApplication.Selection.HomeKey(v1,v2);
v1:=wdLine; v2:=wdExtend;
WordApplication.Selection.EndKey(v1,v2);
WordApplication.Selection.Borders.Item(wdBorderTop).LineStyle := wdLineStyleNone;
WordApplication.Selection.Borders.Item(wdBorderLeft).LineStyle := wdLineStyleNone;
WordApplication.Selection.Borders.Item(wdBorderBottom).LineStyle := wdLineStyleNone;
WordApplication.Selection.Borders.Item(wdBorderRight).LineStyle := wdLineStyleNone;
WordDocument.ActiveWindow.ActivePane.View.SeekView := wdSeekMainDocument;

with WordDocument do
begin
j:=0;
for coli:=0 to (tempGrid as TDBGrid).FieldCount-1 do
if not (tempGrid as TDBGrid).DataSource.DataSet.FieldByName((tempGrid as TDBGrid).Columns[coli].FieldName).Visible then Continue
else inc(j);
//创建表格
tables.Add(WordDocument.Words.Last,(tempGrid as TDBGrid).DataSource.DataSet.RecordCount+1,j+1); //考虑加一个序号

//完成表头
tables.Item(1).Cell(1,1).Width :=WordApplication.MillimetersToPoints(tXHLen); //序号
tables.Item(1).Cell(1,1).Range.Text:='序号';
//下面完成其它的表头部分
j:=2;
for coli:=0 to (tempGrid as TDBGrid).FieldCount-1 do
begin
if not (tempGrid as TDBGrid).DataSource.DataSet.FieldByName((tempGrid as TDBGrid).Columns[coli].FieldName).Visible then Continue;
begin
tables.Item(1).Cell(1, j).Width :=WordApplication.MillimetersToPoints(ScreenPixelToMM((tempGrid as TDBGrid).Columns[coli].Width));
tables.Item(1).Cell(1, j).Range.Text:=(tempGrid as TDBGrid).Columns[coli].FieldName;
end;
inc(j);
end;

//表格居中
WordApplication.Selection.Tables.Item(1).Rows.Alignment := wdAlignRowCenter;
//不允许跨页断行
WordApplication.Selection.Tables.Item(1).Rows.AllowBreakAcrossPages := 0;
//将数据填入表格
for rowi:=0 to (tempGrid as TDBGrid).DataSource.DataSet.RecordCount-1 do
begin
tables.Item(1).Cell(rowi+2,1).Range.Set_Text(IntToStr(rowi+1));
tables.Item(1).Cell(rowi+2,1).Width :=WordApplication.MillimetersToPoints(tXHLen); //序号
i:=1;
for coli:=0 to (tempGrid as TDBGrid).FieldCount-1 do
begin
if not (tempGrid as TDBGrid).DataSource.DataSet.FieldByName((tempGrid as TDBGrid).Columns[coli].FieldName).Visible then Continue;
inc(i);
if i>=j then Break;
tables.Item(1).Cell(rowi+2,i).Range.Set_Text((tempGrid as TDBGrid).DataSource.DataSet.FieldByName((tempGrid as TDBGrid).Columns[coli].FieldName).AsString);
tables.Item(1).Cell(rowi+2,i).Width :=WordApplication.MillimetersToPoints(ScreenPixelToMM((tempGrid as TDBGrid).Columns[coli].Width));
end;
(tempGrid as TDBGrid).DataSource.DataSet.Next;
end;
(tempGrid as TDBGrid).DataSource.DataSet.First;
end;
//表头格式设置
v1:=wdStory; v2:=EmptyParam;
WordApplication.Selection.HomeKey(v1,v2);
v1:=wdLine; v2:=1; v3:=EmptyParam;
WordApplication.Selection.MoveDown(v1,v2,v3);
//在各页顶端以表头形式出现,office 2000 适用
// WordApplication.Selection.Tables.Item(1).Rows.HeadingFormat:= wdToggle;

WordApplication.Selection.SelectRow;
WordApplication.Selection.ParagraphFormat.Alignment := wdAlignParagraphCenter;
//表格线条设置
WordApplication.Selection.Tables.Item(1).Select;
WordApplication.Selection.Tables.Item(1).Borders.Item(wdBorderLeft).LineStyle := wdLineStyleSingle;
WordApplication.Selection.Tables.Item(1).Borders.Item(wdBorderLeft).LineWidth := wdLineWidth100pt;
WordApplication.Selection.Tables.Item(1).Borders.Item(wdBorderLeft).ColorIndex := wdAuto;
WordApplication.Selection.Tables.Item(1).Borders.Item(wdBorderRight).LineStyle := wdLineStyleSingle;
WordApplication.Selection.Tables.Item(1).Borders.Item(wdBorderRight).LineWidth := wdLineWidth100pt;
WordApplication.Selection.Tables.Item(1).Borders.Item(wdBorderRight).ColorIndex := wdAuto;
WordApplication.Selection.Tables.Item(1).Borders.Item(wdBorderTop).LineStyle := wdLineStyleSingle;
WordApplication.Selection.Tables.Item(1).Borders.Item(wdBorderTop).LineWidth := wdLineWidth100pt;
WordApplication.Selection.Tables.Item(1).Borders.Item(wdBorderTop).ColorIndex := wdAuto;
WordApplication.Selection.Tables.Item(1).Borders.Item(wdBorderBottom).LineStyle := wdLineStyleSingle;
WordApplication.Selection.Tables.Item(1).Borders.Item(wdBorderBottom).LineWidth := wdLineWidth100pt;
WordApplication.Selection.Tables.Item(1).Borders.Item(wdBorderBottom).ColorIndex := wdAuto;
WordApplication.Selection.Tables.Item(1).Borders.Item(wdBorderHorizontal).LineStyle := wdLineStyleSingle;
WordApplication.Selection.Tables.Item(1).Borders.Item(wdBorderHorizontal).LineWidth := wdLineWidth050pt;
WordApplication.Selection.Tables.Item(1).Borders.Item(wdBorderHorizontal).ColorIndex := wdAuto;
WordApplication.Selection.Tables.Item(1).Borders.Item(wdBorderVertical).LineStyle := wdLineStyleSingle;
WordApplication.Selection.Tables.Item(1).Borders.Item(wdBorderVertical).LineWidth := wdLineWidth050pt;
WordApplication.Selection.Tables.Item(1).Borders.Item(wdBorderVertical).ColorIndex := wdAuto;
WordApplication.Selection.Tables.Item(1).Borders.Item(wdBorderDiagonalDown).LineStyle := wdLineStyleNone;
WordApplication.Selection.Tables.Item(1).Borders.Item(wdBorderDiagonalUp).LineStyle := wdLineStyleNone;
WordApplication.Selection.Tables.Item(1).Borders.Shadow := False;
v1:=wdStory; v2:=EmptyParam;
WordApplication.Selection.HomeKey(v1,v2);
except
on E: Exception do
begin
ShowMessage(E.Message);
end;
end;
WordApplication.Disconnect;
WordFont.Free;
WordParagraphFormat.Free;
WordDocument.Free;
WordApplication.Free;
end;
 
用ehlib可导出到txt,word 没试过,速度快,感觉爽。
 
后退
顶部