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;
  }
相关推荐
远望清一色14 分钟前
基于MATLAB的实现垃圾分类Matlab源码
开发语言·matlab
confiself24 分钟前
大模型系列——LLAMA-O1 复刻代码解读
java·开发语言
XiaoLeisj35 分钟前
【JavaEE初阶 — 多线程】Thread类的方法&线程生命周期
java·开发语言·java-ee
杜杜的man39 分钟前
【go从零单排】go中的结构体struct和method
开发语言·后端·golang
幼儿园老大*40 分钟前
走进 Go 语言基础语法
开发语言·后端·学习·golang·go
半桶水专家40 分钟前
go语言中package详解
开发语言·golang·xcode
llllinuuu41 分钟前
Go语言结构体、方法与接口
开发语言·后端·golang
cookies_s_s42 分钟前
Golang--协程和管道
开发语言·后端·golang
王大锤439143 分钟前
golang通用后台管理系统07(后台与若依前端对接)
开发语言·前端·golang
为什么这亚子44 分钟前
九、Go语言快速入门之map
运维·开发语言·后端·算法·云原生·golang·云计算