c#
using (var document = DocX.Create(file))
{
document.InsertParagraph().Append("故障报告").FontSize(18).Bold().Alignment = Xceed.Document.NET.Alignment.center;
var table = document.InsertTable(1, 2);
table.SetBorder(TableBorderType.Top, new Xceed.Document.NET.Border() { Color = Color.Transparent });
table.SetBorder(TableBorderType.Bottom, new Xceed.Document.NET.Border() { Color = Color.Transparent });
table.SetBorder(TableBorderType.Left, new Xceed.Document.NET.Border() { Color = Color.Transparent });
table.SetBorder(TableBorderType.Right, new Xceed.Document.NET.Border() { Color = Color.Transparent });
table.SetBorder(TableBorderType.InsideH, new Xceed.Document.NET.Border() { Color = Color.Transparent });
table.SetBorder(TableBorderType.InsideV, new Xceed.Document.NET.Border() { Color = Color.Transparent });
table.Rows[0].Cells[0].Paragraphs[0].Append("型号:" + uavtype.SelectedItem).Alignment=Alignment.left;
table.Rows[0].Cells[1].Paragraphs[0].Append("故障名称:" + faultname.Text).Alignment=Alignment.right;
MemoryStream memoryStream = new MemoryStream();
edit.CaptureImage().Save(memoryStream);
document.InsertParagraph().AppendPicture(document.AddImage(memoryStream).CreatePicture(300, document.PageWidth - document.MarginLeft - document.MarginRight));
var group = edit._groups.Where(o => !string.IsNullOrEmpty(o.Text)).ToList();
table = document.InsertTable(group.Count+2, 3);
table.Alignment = Xceed.Document.NET.Alignment.center;
table.SetBorder(TableBorderType.Top, new Xceed.Document.NET.Border() { Color = Color.PowderBlue });
table.SetBorder(TableBorderType.Bottom, new Xceed.Document.NET.Border() { Color = Color.PowderBlue });
table.SetBorder(TableBorderType.Left, new Xceed.Document.NET.Border() { Color = Color.PowderBlue });
table.SetBorder(TableBorderType.Right, new Xceed.Document.NET.Border() { Color = Color.PowderBlue });
table.SetBorder(TableBorderType.InsideH, new Xceed.Document.NET.Border() { Color = Color.PowderBlue });
table.SetBorder(TableBorderType.InsideV, new Xceed.Document.NET.Border() { Color = Color.PowderBlue });
table.Rows[0].MergeCells(0,2);
table.Rows[0].Cells[0].FillColor = Color.PowderBlue;
table.Rows[0].Cells[0].Paragraphs[0].Append("节点规则");
table.Rows[1].Cells[0].Paragraphs[0].Append("节点");
table.Rows[1].Cells[1].Paragraphs[0].Append("规则名称");
table.Rows[1].Cells[2].Paragraphs[0].Append("规则内容");
for (int i=0; i< group.Count; i++)
{
table.Rows[i+2].Cells[0].Paragraphs[0].Append(group[i].Text);
table.Rows[i+2].Cells[1].Paragraphs[0].Append(group[i].Name);
table.Rows[i+2].Cells[2].Paragraphs[0].Append(group[i].Rule);
}
document.Save();
}
c++
CApplication word_app;
if (!word_app.CreateDispatch(_T("Word.Application"), NULL))
{
LOG_ERROR("该机器没有安装 MS WORD");
return -1;
}
word_app.put_Visible(FALSE);
COleVariant vTrue((short)TRUE), vFalse((short)FALSE), VOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
CDocuments docs;
CDocument0 doc;
docs = word_app.get_Documents();
doc = docs.Add(&CComVariant(_T("")), &CComVariant(false), &CComVariant(0), &CComVariant());
CPageSetup pagesetup = doc.get_PageSetup();
pagesetup.put_LeftMargin(50);
pagesetup.put_RightMargin(50);
try {
CSelection sel = word_app.get_Selection();
CParagraphs wordParagraphs = sel.get_ParagraphFormat();
CString strShow = L"故障报告";
CFont0 font = sel.get_Font();
font.put_Size(16);
font.put_Bold(1);
font.put_Name(L"宋体");
wordParagraphs.put_Alignment(1);
sel.TypeText(strShow);
sel.TypeParagraph();
CTables0 tables = doc.get_Tables();
CTable0 table = tables.Add(sel.get_Range(), 1, 2, vTrue, VOpt);
CBorders borders = table.get_Borders();
borders.put_InsideColor(RGB(255, 255, 255));
borders.put_OutsideColor(RGB(255, 255, 255));
auto input_cell = [](CCell cell, CString strShow, int align) {
CRange range = cell.get_Range();
CParagraphs oParagraphs = range.get_Paragraphs();
CFont0 font = range.get_Font();
font.put_Size(10);
font.put_Bold(0);
font.put_Name(L"宋体");
oParagraphs.put_Alignment(align);
range.InsertAfter(strShow);
};
CCell cell = table.Cell(1, 1);
input_cell(cell, L"型号:" + record.uavtype, 0);
cell = table.Cell(1, 2);
input_cell(cell, L"故障名称:" + record.faultname, 2);
sel.GoTo(COleVariant((short)3), COleVariant((short)-1),VOpt,VOpt);
CnlineShapes shapes = sel.get_InlineShapes();
CnlineShape shape = shapes.AddPicture(image.c_str(), vFalse, vTrue, VOpt);
shape.put_Height(300);
shape.put_Width(pagesetup.get_PageWidth()-100);
vector<tuple<wstring, wstring, wstring, wstring>> item;
recrusvie_tree(record.tree["tree"], item);
tables = doc.get_Tables();
table = tables.Add(sel.get_Range(), item.size()+5, 4, vTrue, VOpt);
borders = table.get_Borders();
borders.put_InsideColor(RGB(198, 217, 241));
borders.put_OutsideColor(RGB(198, 217, 241));
cell = table.Cell(1, 1);
cell.Merge(table.Cell(1, 2));
cell.Merge(table.Cell(1, 2));
cell.Merge(table.Cell(1, 2));
CShading shading = cell.get_Shading();
shading.put_BackgroundPatternColor(RGB(198, 217, 241));
input_cell(cell, L"故障诊断结果建议", 0);
cell = table.Cell(2, 1);
cell.Merge(table.Cell(2, 2));
input_cell(cell, L"诊断结果", 0);
cell = table.Cell(2, 2);
cell.Merge(table.Cell(2, 3));
input_cell(cell, record.result,0);
cell = table.Cell(3, 1);
cell.Merge(table.Cell(3, 2));
input_cell(cell, L"专家建议", 0);
cell = table.Cell(3, 2);
cell.Merge(table.Cell(3, 3));
input_cell(cell, record.advise,0);
cell = table.Cell(4, 1);
cell.Merge(table.Cell(4, 2));
cell.Merge(table.Cell(4, 2));
cell.Merge(table.Cell(4, 2));
shading = cell.get_Shading();
shading.put_BackgroundPatternColor(RGB(198, 217, 241));
input_cell(cell, L"故障各节点规则", 0);
CString header[4] = { L"节点",L"规则名称",L"规则内容",L"状态"};
for (int j = 0; j < item.size()+1; j++) {
for (int i = 0; i < 4; i++) {
cell = table.Cell(j+5, i + 1);
strShow = j==0?header[i]:(i == 0?get<0>(item[j-1]).c_str() :
i == 1 ? get<1>(item[j-1]).c_str() : i == 2 ? get<2>(item[j-1]).c_str() : get<3>(item[j-1]).c_str());
input_cell(cell, strShow,1);
}
}
sel.GoTo(COleVariant((short)3), COleVariant((short)-1), VOpt, VOpt);
sel.TypeParagraph();
for (int i = 0; i < record.faultcase.size(); i++) {
table = tables.Add(sel.get_Range(), 7, 2, vTrue, VOpt);
borders = table.get_Borders();
borders.put_InsideColor(RGB(198, 217, 241));
borders.put_OutsideColor(RGB(198, 217, 241));
cell = table.Cell(1, 1);
cell.Merge(table.Cell(1, 2));
shading = cell.get_Shading();
shading.put_BackgroundPatternColor(RGB(198, 217, 241));
input_cell(cell, L"故障名称:"+ get<0>(record.faultcase[i])).c_str(), 0);
cell = table.Cell(2, 1);
input_cell(cell, L"案例主题", 0);
cell = table.Cell(2, 2);
input_cell(cell, get<0>(record.faultcase[i]).c_str(), 0);
cell = table.Cell(3, 1);
input_cell(cell, L"故障描述", 0);
cell = table.Cell(3, 2);
input_cell(cell, get<1>(record.faultcase[i]).c_str(), 0);
cell = table.Cell(4, 1);
input_cell(cell, L"案例内容", 0);
cell = table.Cell(4, 2);
input_cell(cell, get<2>(record.faultcase[i]).c_str(), 0);
cell = table.Cell(5, 1);
input_cell(cell, L"分析原因", 0);
cell = table.Cell(5, 2);
input_cell(cell, get<3>(record.faultcase[i]).c_str(), 0);
cell = table.Cell(6, 1);
input_cell(cell, L"问题定位", 0);
cell = table.Cell(6, 2);
input_cell(cell, get<4>(record.faultcase[i]).c_str(), 0);
cell = table.Cell(7, 1);
input_cell(cell, L"处理建议", 0);
cell = table.Cell(7, 2);
input_cell(cell, get<5>(record.faultcase[i]).c_str(), 0);
}
doc.SaveAs(COleVariant(filename), VOpt, VOpt, VOpt, VOpt, VOpt, VOpt, VOpt, VOpt,
VOpt, VOpt, VOpt, VOpt, VOpt, VOpt, VOpt);
}