Delphi 712操作word

时间:2023-12-17 08:18:32

//导出Word
procedure TFrm_Computing.ExportWord;
var
wordApp, WordDoc, WrdSelection, wrdtable, wrdtable1, cell: variant;
strAdd: string;
wdPar,wdRange:OleVariant;
iCol, iRow, I, J: Integer;

begin

try
wordApp := CreateOleObject('Word.Application');
except
Application.MessageBox('Word没有安装', '提示信息', MB_OK+MB_ICONASTERISK+MB_DEFBUTTON1+MB_APPLMODAL);
Exit;
end;
Self.Chart_Edit.CopyToClipboardBitmap;
wordApp.Visible := true;
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 := 12;
iCol := 2;
wrdtable := wordDoc.Tables.Add(wdRange,iRow, iCol);
wrdtable.Cell(1, 1).Merge(wrdtable.Cell(1, 2));
wrdtable.Cell(2, 1).Merge(wrdtable.Cell(2, 2));
wrdtable.Cell(1,1).Range.Text:= '打印时间:' + FormatDateTime('yyyy年hh月dd日 hh时MM分ss秒', Now);
wrdtable.Cell(2,1).Range.Text:= '进样时间:' + FormatDateTime('yyyy年hh月dd日 hh时MM分ss秒', Now);
wrdtable.Cell(3,1).Range.Text:= '质检(E)字第()号';
wrdtable.Cell(4,1).Range.Text:= '送样单位:' + 'XXX公司';
wrdtable.Cell(4,2).Range.Text:= '仪器型号:' + 'SC-1001-09A';
wrdtable.Cell(5,1).Range.Text:= '取样日期:' + FormatDateTime('yyyy年hh月dd日 ', Now);
wrdtable.Cell(5,2).Range.Text:= '收样日期:' + FormatDateTime('yyyy年hh月dd日 ', Now);
wrdtable.Cell(6,1).Range.Text:= '样品批号:' + '';
wrdtable.Cell(6,2).Range.Text:= '样品名称:' + '固液';
wrdtable.Cell(7,1).Range.Text:= '样品罐号:' + 'A-1-2';
wrdtable.Cell(8,1).Range.Text:= '仪器文件控制参数:' + '#1111';
wrdtable.Cell(9,1).Range.Text:= '';
wrdtable.Cell(10,1).Range.Text:= 'C:\AAAAAAA\111.bmp';
wrdtable.Cell(11,1).Range.Text:= '';
wrdtable.Cell(12,1).Range.Text:= '';

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

wordApp.selection.movedown(wdLine, 17);

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

wdPar:=WordApp.ActiveDocument.Paragraphs.Add;
wdRange:=wdPar.Range;
wdRange := wordApp.ActiveDocument.Content;
wdRange.Collapse(wdCollapseEnd);
iRow := SGrid_PeakResult.RowCount;
iCol := SGrid_PeakResult.ColCount;
wrdtable1 := wordDoc.Tables.Add(wdRange,iRow, iCol);
for I := 0 to SGrid_PeakResult.RowCount -1 do
begin
for J := 0 to SGrid_PeakResult.ColCount -1 do
begin
//ShowMessage(SGrid_PeakResult.Cells[J, I]);
wrdtable1.Cell(I +1,J + 1).Range.Text:= SGrid_PeakResult.Cells[J, I];
end;
end;

wrdtable1.columns.item(2).width := 80;
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.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; //

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

//选择左边的表格
//Selection.Borders(wdBorderLeft).LineStyle = wdLineStyleNone
//Selection.Borders(wdBorderBottom).LineStyle = wdLineStyleNone
//选择右边的表格
////Selection.Borders(wdBorderRight).LineStyle = wdLineStyleNone

end;