自动化word导出

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))
    //if (!word_app.CreateDispatch(_T("KWPS.Application"), NULL))
{
    //printf("本机没有安装word产品!");
    LOG_ERROR("该机器没有安装 MS WORD");
    return -1;
}
word_app.put_Visible(FALSE);//设置word可见
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);
}

相关推荐

  1. 自动化word导出

    2024-04-14 07:50:03       36 阅读
  2. Typora导出word

    2024-04-14 07:50:03       55 阅读
  3. word导出链接

    2024-04-14 07:50:03       55 阅读
  4. Easypoi word 模板导出问题

    2024-04-14 07:50:03       57 阅读
  5. poi导出word 详细教程

    2024-04-14 07:50:03       22 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-14 07:50:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-14 07:50:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-14 07:50:03       87 阅读
  4. Python语言-面向对象

    2024-04-14 07:50:03       96 阅读

热门阅读

  1. 每天学习一个Linux命令之w

    2024-04-14 07:50:03       37 阅读
  2. array和vector

    2024-04-14 07:50:03       115 阅读
  3. mybatis-plus实现数据字段加解密

    2024-04-14 07:50:03       36 阅读
  4. HTML VUE

    2024-04-14 07:50:03       36 阅读
  5. 查询电脑用户名和组信息

    2024-04-14 07:50:03       34 阅读
  6. Python数据库编程实战:sqlite3模块详解

    2024-04-14 07:50:03       42 阅读
  7. MYSQL原理学习篇简记(五)

    2024-04-14 07:50:03       40 阅读
  8. C语言执行CMD命令并隐藏CMD窗口

    2024-04-14 07:50:03       41 阅读