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;
  }
相关推荐
凯子坚持 c3 分钟前
Qt常用控件指南(8)
开发语言·数据库·qt
冠希陈、8 分钟前
PHP 判断是否是移动端,更新鸿蒙系统
android·开发语言·php
HDO清风28 分钟前
CASIA-HWDB2.x 数据集DGRL文件解析(python)
开发语言·人工智能·pytorch·python·目标检测·计算机视觉·restful
2201_7569890929 分钟前
C++中的事件驱动编程
开发语言·c++·算法
多米Domi01140 分钟前
0x3f 第48天 面向实习的八股背诵第五天 + 堆一题 背了JUC的题,java.util.Concurrency
开发语言·数据结构·python·算法·leetcode·面试
2301_8223776541 分钟前
模板元编程调试方法
开发语言·c++·算法
csbysj202044 分钟前
Python 循环嵌套
开发语言
测试_AI_一辰1 小时前
Agent & RAG 测试工程05:把 RAG 的检索过程跑清楚:chunk 是什么、怎么来的、怎么被命中的
开发语言·人工智能·功能测试·自动化·ai编程
Coding茶水间1 小时前
基于深度学习的输电电力设备检测系统演示与介绍(YOLOv12/v11/v8/v5模型+Pyqt5界面+训练代码+数据集)
开发语言·人工智能·深度学习·yolo·目标检测·机器学习
清风~徐~来1 小时前
【视频点播系统】BRpc 介绍及使用
开发语言