• C# 使用DocX生成word文档


    1、引用DocX包3.0.0版本

    2、引用包

    using Xceed.Words.NET;

    3、生成word文档

    1. // 保存文档到 Stream
    2. var stream = new MemoryStream();
    3. // 创建一个新的文档
    4. using (var doc = DocX.Create("Document"))
    5. {
    6. // 添加页眉
    7. doc.AddHeaders();
    8. Header header = doc.Headers.Odd;
    9. // 在页眉中添加带有下划线格式的段落
    10. Paragraph headerParagraph = header.InsertParagraph();
    11. headerParagraph.Append("文档名").Font(new Xceed.Document.NET.Font("宋体")).FontSize(10.5).Alignment = Alignment.left;
    12. // 添加包含一行空白表格的段落来模拟横线
    13. //Table lineTable = header.InsertTable(1, 1);
    14. //lineTable.Rows[0].Height = 1;
    15. //lineTable.SetBorder(TableBorderType.Top, new Border(BorderStyle.Tcbs_single, BorderSize.one, 10, Color.Black));
    16. Paragraph p = doc.InsertParagraph();
    17. // 添加标题
    18. p.AppendLine("标题V1.1").FontSize(18d).Bold().Spacing(6).Alignment = Alignment.center;
    19. // 设置行高
    20. p.LineSpacingAfter = 40;
    21. // 添加正文内容
    22. // 添加报表表格
    23. var reportTable = doc.AddTable(10, 4);
    24. reportTable.Design = TableDesign.LightListAccent1;
    25. var defaultRowHeight = 40; // 按照你的需要设置行高的值
    26. // 设置表格所有行的高度 边框
    27. foreach (var row in reportTable.Rows)
    28. {
    29. row.Height = defaultRowHeight; // 设置行高
    30. foreach (var reportCell in row.Cells)
    31. {
    32. reportCell.VerticalAlignment = VerticalAlignment.Center;
    33. reportCell.SetBorder(TableCellBorderType.Bottom, new Border(BorderStyle.Tcbs_single, BorderSize.one, 5, Color.Black));
    34. reportCell.SetBorder(TableCellBorderType.Top, new Border(BorderStyle.Tcbs_single, BorderSize.one, 5, Color.Black));
    35. reportCell.SetBorder(TableCellBorderType.Left, new Border(BorderStyle.Tcbs_single, BorderSize.one, 5, Color.Black));
    36. reportCell.SetBorder(TableCellBorderType.Right, new Border(BorderStyle.Tcbs_single, BorderSize.one, 5, Color.Black));
    37. }
    38. }
    39. reportTable.SetBorder(TableBorderType.Bottom, new Border(BorderStyle.Tcbs_single, BorderSize.one, 10, Color.Black));
    40. reportTable.SetBorder(TableBorderType.Top, new Border(BorderStyle.Tcbs_single, BorderSize.one, 10, Color.Black));
    41. reportTable.SetBorder(TableBorderType.Left, new Border(BorderStyle.Tcbs_single, BorderSize.one, 10, Color.Black));
    42. reportTable.SetBorder(TableBorderType.Right, new Border(BorderStyle.Tcbs_single, BorderSize.one, 10, Color.Black));
    43. reportTable.Rows[0].MergeCells(0, 3);
    44. reportTable.Rows[8].Height = 100;
    45. reportTable.Rows[9].Height = 170;
    46. // 填充表格内容
    47. reportTable.Rows[0].Cells[0].Paragraphs.First().Append("一、报告概览").Font(new Xceed.Document.NET.Font("宋体")).FontSize(16).Bold().Alignment = Alignment.center;
    48. reportTable.Rows[1].Cells[0].Paragraphs.First().Append("项目编号");
    49. //项目编号
    50. reportTable.Rows[1].Cells[1].Paragraphs.First().Append(input.ProjectNumber);
    51. reportTable.Rows[1].Cells[2].Paragraphs.First().Append("申请单位");
    52. //申请单位
    53. reportTable.Rows[1].Cells[3].Paragraphs.First().Append(input.Department);
    54. reportTable.MergeCellsInColumn(2, 1, 2);
    55. var requist = reportTable.Rows[9].Cells[0];
    56. reportTable.Rows[9].MergeCells(0, 3);
    57. Paragraph repa = requist.InsertParagraph();
    58. repa.Append("段落标题:").FontSize(10.5).Bold().Alignment = Alignment.left;
    59. repa.SpacingAfter(10);
    60. Paragraph repa1 = requist.InsertParagraph();
    61. repa1.Append("1、123123123123。").FontSize(10.5).Alignment = Alignment.left;
    62. repa1.SpacingAfter(10);
    63. Paragraph repa2 = requist.InsertParagraph();
    64. repa2.Append("2、4324234322423。").FontSize(10.5).Alignment = Alignment.left;
    65. repa2.SpacingAfter(10);
    66. Paragraph repa3 = requist.InsertParagraph();
    67. repa3.Append("3、4356564557657。").FontSize(10.5).Alignment = Alignment.left;
    68. repa3.SpacingAfter(10);
    69. // 将报表概览表格添加到文档
    70. doc.InsertTable(reportTable);
    71. // 添加系统自测结果表格
    72. var testResultTable = doc.AddTable(2, 1);
    73. testResultTable.Design = TableDesign.LightListAccent1;
    74. // 设置表格所有行的高度 边框
    75. foreach (var row in testResultTable.Rows)
    76. {
    77. row.Height = defaultRowHeight; // 设置行高
    78. foreach (var resultCell in row.Cells)
    79. {
    80. resultCell.VerticalAlignment = VerticalAlignment.Center;
    81. resultCell.SetBorder(TableCellBorderType.Bottom, new Border(BorderStyle.Tcbs_single, BorderSize.one, 5, Color.Black));
    82. resultCell.SetBorder(TableCellBorderType.Top, new Border(BorderStyle.Tcbs_single, BorderSize.one, 5, Color.Black));
    83. resultCell.SetBorder(TableCellBorderType.Left, new Border(BorderStyle.Tcbs_single, BorderSize.one, 5, Color.Black));
    84. resultCell.SetBorder(TableCellBorderType.Right, new Border(BorderStyle.Tcbs_single, BorderSize.one, 5, Color.Black));
    85. }
    86. }
    87. testResultTable.SetBorder(TableBorderType.Bottom, new Border(BorderStyle.Tcbs_single, BorderSize.one, 10, Color.Black));
    88. testResultTable.SetBorder(TableBorderType.Top, new Border(BorderStyle.Tcbs_single, BorderSize.one, 10, Color.Black));
    89. testResultTable.SetBorder(TableBorderType.Left, new Border(BorderStyle.Tcbs_single, BorderSize.one, 10, Color.Black));
    90. testResultTable.SetBorder(TableBorderType.Right, new Border(BorderStyle.Tcbs_single, BorderSize.one, 10, Color.Black));
    91. testResultTable.Rows[0].Height = 40;
    92. testResultTable.Rows[1].Height = 360;
    93. testResultTable.Rows[0].Cells[0].Paragraphs.First().Append("二、表单二").Font(new Xceed.Document.NET.Font("宋体")).FontSize(16).Bold().Alignment = Alignment.center;
    94. Cell resultImgCell = testResultTable.Rows[1].Cells[0];
    95. resultImgCell.RemoveParagraphAt(0);
    96. // 创建一个段落
    97. Paragraph resultParagraph = resultImgCell.InsertParagraph();
    98. // 插入图片到段落中
    99. var img1 = doc.AddImage(input.Content.GetStream());
    100. Picture pic1 = img1.CreatePicture();
    101. // 设置图片大小
    102. pic1.Width = doc.PageWidth - doc.MarginLeft - doc.MarginRight; // 设置图片宽度等于页面宽度减去左右页边距
    103. pic1.Height = ((float)Math.Round((double)pic1.Height * ((double)pic1.Width / (double)pic1.Width))); // 根据宽度比例计算高度
    104. //pic1.Width = 450;
    105. //pic1.Height = 100;
    106. // 将图片添加到段落
    107. resultParagraph.AppendPicture(pic1);
    108. // 插入图片到段落中
    109. var img2 = doc.AddImage(input.Content1.GetStream());
    110. Picture pic2 = img2.CreatePicture();
    111. // 设置图片大小
    112. pic2.Width = doc.PageWidth - doc.MarginLeft - doc.MarginRight; // 设置图片宽度等于页面宽度减去左右页边距
    113. pic2.Height = ((float)Math.Round((double)pic2.Height * ((double)pic2.Width / (double)pic2.Width))); // 根据宽度比例计算高度
    114. // 将图片添加到段落
    115. resultParagraph.AppendPicture(pic2);
    116. // 将系统自测结果表格添加到文档
    117. doc.InsertTable(testResultTable);
    118. doc.InsertParagraph();
    119. doc.SaveAs(stream);
    120. // 将 Stream 位置设为起始位置
    121. stream.Position = 0;
    122. // 返回生成的 Word 文档的 Stream
    123. //return stream;
    124. }

  • 相关阅读:
    karmada介绍和分析
    【IP SSL】内网IP SSL证书Nginx部署
    uniapp开发H5,分享链接到微信好友,显示标题和缩略图
    车载激光雷达标定板在无人驾驶中的作用
    新基建助力智能化道路交通领域的转型发展
    关于#python#的问题:二维数组去重保留最后一行满足条件的数据(相关搜索:数组去重)
    【Unity3D】正交视图与透视视图 ( 正交视图概念 | 透视视图概念 | 观察点 | 正交视图作用 | 摄像机广角设定 | 透视畸变效果 )
    RFID技术在仓储物流供应链管理中的应用
    buuctf-misc-wireshark1
    MFC Windows 程序设计[149]之上报控件组全展示
  • 原文地址:https://blog.csdn.net/StevenYanghahha/article/details/136537873