C# 使用DocX生成word文档

1、引用DocX包3.0.0版本

2、引用包

cs 复制代码
using Xceed.Words.NET;

3、生成word文档

cs 复制代码
  // 保存文档到 Stream
  var stream = new MemoryStream();
  // 创建一个新的文档
  using (var doc = DocX.Create("Document"))
  {
      // 添加页眉
      doc.AddHeaders();
      Header header = doc.Headers.Odd;

      // 在页眉中添加带有下划线格式的段落
      Paragraph headerParagraph = header.InsertParagraph();
      headerParagraph.Append("文档名").Font(new Xceed.Document.NET.Font("宋体")).FontSize(10.5).Alignment = Alignment.left;

      // 添加包含一行空白表格的段落来模拟横线
      //Table lineTable = header.InsertTable(1, 1);
      //lineTable.Rows[0].Height = 1;
      //lineTable.SetBorder(TableBorderType.Top, new Border(BorderStyle.Tcbs_single, BorderSize.one, 10, Color.Black));


      Paragraph p = doc.InsertParagraph();
      // 添加标题
      p.AppendLine("标题V1.1").FontSize(18d).Bold().Spacing(6).Alignment = Alignment.center;

      // 设置行高
      p.LineSpacingAfter = 40;

      // 添加正文内容

      // 添加报表表格
      var reportTable = doc.AddTable(10, 4);
      reportTable.Design = TableDesign.LightListAccent1;

      var defaultRowHeight = 40; // 按照你的需要设置行高的值

      // 设置表格所有行的高度 边框
      foreach (var row in reportTable.Rows)
      {
          row.Height = defaultRowHeight; // 设置行高

          foreach (var reportCell in row.Cells)
          {
              reportCell.VerticalAlignment = VerticalAlignment.Center;
              reportCell.SetBorder(TableCellBorderType.Bottom, new Border(BorderStyle.Tcbs_single, BorderSize.one, 5, Color.Black));
              reportCell.SetBorder(TableCellBorderType.Top, new Border(BorderStyle.Tcbs_single, BorderSize.one, 5, Color.Black));
              reportCell.SetBorder(TableCellBorderType.Left, new Border(BorderStyle.Tcbs_single, BorderSize.one, 5, Color.Black));
              reportCell.SetBorder(TableCellBorderType.Right, new Border(BorderStyle.Tcbs_single, BorderSize.one, 5, Color.Black));
          }
      }

      reportTable.SetBorder(TableBorderType.Bottom, new Border(BorderStyle.Tcbs_single, BorderSize.one, 10, Color.Black));
      reportTable.SetBorder(TableBorderType.Top, new Border(BorderStyle.Tcbs_single, BorderSize.one, 10, Color.Black));
      reportTable.SetBorder(TableBorderType.Left, new Border(BorderStyle.Tcbs_single, BorderSize.one, 10, Color.Black));
      reportTable.SetBorder(TableBorderType.Right, new Border(BorderStyle.Tcbs_single, BorderSize.one, 10, Color.Black));

      reportTable.Rows[0].MergeCells(0, 3);
      reportTable.Rows[8].Height = 100;
      reportTable.Rows[9].Height = 170;

      // 填充表格内容
      reportTable.Rows[0].Cells[0].Paragraphs.First().Append("一、报告概览").Font(new Xceed.Document.NET.Font("宋体")).FontSize(16).Bold().Alignment = Alignment.center;

      reportTable.Rows[1].Cells[0].Paragraphs.First().Append("项目编号");
      //项目编号
      reportTable.Rows[1].Cells[1].Paragraphs.First().Append(input.ProjectNumber);
      reportTable.Rows[1].Cells[2].Paragraphs.First().Append("申请单位");
      //申请单位
      reportTable.Rows[1].Cells[3].Paragraphs.First().Append(input.Department);
      reportTable.MergeCellsInColumn(2, 1, 2);

      var requist = reportTable.Rows[9].Cells[0];
      reportTable.Rows[9].MergeCells(0, 3);
      Paragraph repa = requist.InsertParagraph();
      repa.Append("段落标题:").FontSize(10.5).Bold().Alignment = Alignment.left;
      repa.SpacingAfter(10);
      Paragraph repa1 = requist.InsertParagraph();
      repa1.Append("1、123123123123。").FontSize(10.5).Alignment = Alignment.left;
      repa1.SpacingAfter(10);
      Paragraph repa2 = requist.InsertParagraph();
      repa2.Append("2、4324234322423。").FontSize(10.5).Alignment = Alignment.left;
      repa2.SpacingAfter(10);
      Paragraph repa3 = requist.InsertParagraph();
      repa3.Append("3、4356564557657。").FontSize(10.5).Alignment = Alignment.left;
      repa3.SpacingAfter(10);

      // 将报表概览表格添加到文档
      doc.InsertTable(reportTable);


      // 添加系统自测结果表格
      var testResultTable = doc.AddTable(2, 1);
      testResultTable.Design = TableDesign.LightListAccent1;


      // 设置表格所有行的高度 边框
      foreach (var row in testResultTable.Rows)
      {
          row.Height = defaultRowHeight; // 设置行高
          foreach (var resultCell in row.Cells)
          {
              resultCell.VerticalAlignment = VerticalAlignment.Center;
              resultCell.SetBorder(TableCellBorderType.Bottom, new Border(BorderStyle.Tcbs_single, BorderSize.one, 5, Color.Black));
              resultCell.SetBorder(TableCellBorderType.Top, new Border(BorderStyle.Tcbs_single, BorderSize.one, 5, Color.Black));
              resultCell.SetBorder(TableCellBorderType.Left, new Border(BorderStyle.Tcbs_single, BorderSize.one, 5, Color.Black));
              resultCell.SetBorder(TableCellBorderType.Right, new Border(BorderStyle.Tcbs_single, BorderSize.one, 5, Color.Black));
          }
      }

      testResultTable.SetBorder(TableBorderType.Bottom, new Border(BorderStyle.Tcbs_single, BorderSize.one, 10, Color.Black));
      testResultTable.SetBorder(TableBorderType.Top, new Border(BorderStyle.Tcbs_single, BorderSize.one, 10, Color.Black));
      testResultTable.SetBorder(TableBorderType.Left, new Border(BorderStyle.Tcbs_single, BorderSize.one, 10, Color.Black));
      testResultTable.SetBorder(TableBorderType.Right, new Border(BorderStyle.Tcbs_single, BorderSize.one, 10, Color.Black));

      testResultTable.Rows[0].Height = 40;
      testResultTable.Rows[1].Height = 360;

      testResultTable.Rows[0].Cells[0].Paragraphs.First().Append("二、表单二").Font(new Xceed.Document.NET.Font("宋体")).FontSize(16).Bold().Alignment = Alignment.center;

      Cell resultImgCell = testResultTable.Rows[1].Cells[0];
      resultImgCell.RemoveParagraphAt(0);
      // 创建一个段落
      Paragraph resultParagraph = resultImgCell.InsertParagraph();
      // 插入图片到段落中
      var img1 = doc.AddImage(input.Content.GetStream());
      Picture pic1 = img1.CreatePicture();
      // 设置图片大小
      pic1.Width = doc.PageWidth - doc.MarginLeft - doc.MarginRight; // 设置图片宽度等于页面宽度减去左右页边距
      pic1.Height = ((float)Math.Round((double)pic1.Height * ((double)pic1.Width / (double)pic1.Width))); // 根据宽度比例计算高度
                                                                                                          //pic1.Width = 450;
                                                                                                          //pic1.Height = 100;
                                                                                                          // 将图片添加到段落
      resultParagraph.AppendPicture(pic1);

      // 插入图片到段落中
      var img2 = doc.AddImage(input.Content1.GetStream());
      Picture pic2 = img2.CreatePicture();
      // 设置图片大小
      pic2.Width = doc.PageWidth - doc.MarginLeft - doc.MarginRight; // 设置图片宽度等于页面宽度减去左右页边距
      pic2.Height = ((float)Math.Round((double)pic2.Height * ((double)pic2.Width / (double)pic2.Width))); // 根据宽度比例计算高度
                                                                                                          // 将图片添加到段落
      resultParagraph.AppendPicture(pic2);

      // 将系统自测结果表格添加到文档
      doc.InsertTable(testResultTable);
      doc.InsertParagraph();


      doc.SaveAs(stream);

      // 将 Stream 位置设为起始位置
      stream.Position = 0;

      // 返回生成的 Word 文档的 Stream
      //return stream;
  }
相关推荐
yngsqq24 分钟前
一键打断线(根据相交点打断)——CAD c# 二次开发
windows·microsoft·c#
古希腊掌管学习的神25 分钟前
[LeetCode-Python版]相向双指针——611. 有效三角形的个数
开发语言·python·leetcode
赵钰老师25 分钟前
【R语言遥感技术】“R+遥感”的水环境综合评价方法
开发语言·数据分析·r语言
就爱学编程33 分钟前
重生之我在异世界学编程之C语言小项目:通讯录
c语言·开发语言·数据结构·算法
Oneforlove_twoforjob1 小时前
【Java基础面试题025】什么是Java的Integer缓存池?
java·开发语言·缓存
emoji1111111 小时前
前端对页面数据进行缓存
开发语言·前端·javascript
每天都要学信号1 小时前
Python(第一天)
开发语言·python
TENET信条1 小时前
day53 第十一章:图论part04
开发语言·c#·图论
生信圆桌1 小时前
【生信圆桌x教程系列】如何安装 seurat V5版本R包,最详细安装手册
开发语言·r语言
IT猿手1 小时前
最新高性能多目标优化算法:多目标麋鹿优化算法(MOEHO)求解TP1-TP10及工程应用---盘式制动器设计,提供完整MATLAB代码
开发语言·深度学习·算法·机器学习·matlab·多目标算法