张志峰的博客

水滴石川,积少成多。

导航

delphi 712 Word 2

Posted on 2016-04-13 10:16  ╰★张志峰★╮  阅读(715)  评论(0编辑  收藏  举报

//导出Word
procedure TFrm_Computing.ExportWord(aFileName: string; aFileType: string);
var
wordApp, WordDoc, WrdSelection, wrdtable, wrdtable1, cell: variant;
strAdd: string;
wdPar,wdRange:OleVariant;
iCol, iRow, I, J: Integer;
vNode: TcxTreeListNode;
vcolumn: TcxTreeListColumn;
str: string;
begin

try
wordApp := CreateOleObject('Word.Application');
except
Application.MessageBox('Word没有安装', '提示信息', MB_OK+MB_ICONASTERISK+MB_DEFBUTTON1+MB_APPLMODAL);
Exit;
end;
Self.cht_Edit.CopyToClipboardBitmap;
wordApp.Visible := false;
wordDoc:=WordApp.Documents.Add();
wordDoc.select;
wrdSelection := WordApp.selection;
strAdd:='XXX分析报告';
//strAdd:= Format(strAdd, [FormatDateTime('YYYY', Date), WeekofYear(Date)]);
wrdSelection.ParagraphFormat.Alignment:=wdAlignParagraphCenter;
wrdSelection.Font.bold := true;
wrdSelection.Font.Size := 15;
wrdSelection.TypeText(strAdd);
wordApp.selection.TypeParagraph;//换行
wrdSelection.Font.bold := false;
wrdSelection.Font.Size := 10;
wrdSelection.Font.bold := false;
wrdSelection.ParagraphFormat.Alignment:=wdAlignParagraphLeft;

wdPar:=WordApp.ActiveDocument.Paragraphs.Add;
wdRange:=wdPar.Range;
wdRange := wordApp.ActiveDocument.Content;
//该方法用于将指定区域折叠到起始位置或结束位置。
wdRange.Collapse(wdCollapseEnd);

iRow := RzRichEdit_ReportTop.Lines.Count;
iCol := 1;
wrdtable := wordDoc.Tables.Add(wdRange,iRow, iCol);
for I := 0 to RzRichEdit_ReportTop.Lines.Count -1 do
begin
wrdtable.Cell(I, 1).Range.Text := RzRichEdit_ReportTop.Lines.Strings[I];
end;

 

wordApp.selection.TypeParagraph;//换行

wordApp.selection.movedown(wdLine, 17);


wordApp.selection.paste;
wordApp.selection.TypeParagraph;//换行
wordApp.selection.TypeParagraph;//换行


wdPar:=WordApp.ActiveDocument.Paragraphs.Add;
wdRange:=wdPar.Range;
wdRange := wordApp.ActiveDocument.Content;
wdRange.Collapse(wdCollapseEnd);

iRow := SGrid_PeakResult.Count;
iCol := SGrid_PeakResult.ColumnCount;
wrdtable1 := wordDoc.Tables.Add(wdRange,iRow, iCol);
for I := 0 to SGrid_PeakResult.Count -1 do
begin
vNode := SGrid_PeakResult.Items[I];
for J := 0 to SGrid_PeakResult.ColumnCount -1 do
begin
vcolumn := SGrid_PeakResult.Columns[J];
wrdtable1.Cell(I +1,J + 1).Range.Text:= vNode.Texts[vcolumn.ItemIndex];
end;
end;

wrdtable1.columns.item(2).width := 60;
wrdtable1.rows.item(2).Borders.Item(wdBorderTop).LineStyle:=wdLineStyleSingle; //控制单行是否有线
wrdtable1.rows.item(2).Borders.Item(wdBorderTop).LineWidth:= wdLineWidth150pt; //控制单行的样式的线宽
wrdtable1.Rows.Item(1).Cells.VerticalAlignment:=wdCellAlignVerticalCenter;
wrdtable1.columns.item(3).width := 60;
wrdtable1.columns.item(4).width := 60;
wrdtable1.columns.item(8).width := 60;

 

wrdtable1.Borders.Item(wdBorderTop).LineStyle:=wdLineStyleSingle;
wrdtable1.Borders.Item(wdBorderBottom).LineStyle:=wdLineStyleSingle;
// wrdtable1.Borders.Item(wdBorderLeft).LineStyle:=wdLineStyleSingle;
// wrdtable1.Borders.Item(wdBorderRight).LineStyle:=wdLineStyleSingle;
// wrdtable1.Borders.Item(wdBorderHorizontal).LineStyle:=wdLineStyleSingle;
wrdtable1.Borders.Item(wdBorderTop).LineWidth:= wdLineWidth150pt; //
wrdtable1.Borders.Item(wdBorderBottom).LineWidth:= wdLineWidth150pt; //
wrdtable1.Borders.Item(wdBorderBottom).LineStyle:= wdLineStyleSingle; //

wdPar:=WordApp.ActiveDocument.Paragraphs.Add;
wdRange:=wdPar.Range;
wdRange := wordApp.ActiveDocument.Content;
//该方法用于将指定区域折叠到起始位置或结束位置。
wdRange.Collapse(wdCollapseEnd);

iRow := RzRichEdit_ReportBottom.Lines.Count;
iCol := 1;
wrdtable := wordDoc.Tables.Add(wdRange,iRow, iCol);
for I := 0 to RzRichEdit_ReportBottom.Lines.Count -1 do
begin
wrdtable.Cell(I, 1).Range.Text := RzRichEdit_ReportBottom.Lines.Strings[I];
end;


wordApp.Visible := True;
if AnsiSameText(aFileType, 'doc') then
begin
wordDoc.saveas(aFileName, wdFormatDocument);
end
else if AnsiSameText(aFileType, 'rtf') then
begin
str := aFileName;
str := ChangeFileExt(str , '.rtf');
wordDoc.saveas(str, wdFormatRTF);
end;

end;