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;
}