C#生成Word文档,实际尝试了一种可行的方法,记录如下:

1. 添加引用

   Microsoft.Office.Interop.Word.dll,这个文件有多种版本,我选了11.0的,链接:https://download.csdn.net/download/zhouyingge1104/10992883

C# 生成Word文档_程序设计

C# 生成Word文档_编程开发_02

2. 组织代码

using Microsoft.Office.Interop.Word;

//...

Microsoft.Office.Interop.Word.Application winword = new Microsoft.Office.Interop.Word.Application();
winword.Visible = false;

object missing = System.Reflection.Missing.Value;

Microsoft.Office.Interop.Word.Document document = winword.Documents.Add(ref missing, ref missing, ref missing, ref missing);

//页边距
document.PageSetup.LeftMargin = 40; //1.41CM
document.PageSetup.RightMargin = 40;
document.PageSetup.TopMargin = 40;
document.PageSetup.BottomMargin = 40;

//页眉
foreach (Microsoft.Office.Interop.Word.Section section in document.Sections)
{
//Get the header range and add the header details.
Microsoft.Office.Interop.Word.Range headerRange = section.Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
headerRange.Fields.Add(headerRange, Microsoft.Office.Interop.Word.WdFieldType.wdFieldPage);
headerRange.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
headerRange.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdBlue;
headerRange.Font.Size = 10;
headerRange.Text = "Header text goes here";
}

//页脚
foreach (Microsoft.Office.Interop.Word.Section wordSection in document.Sections)
{
Microsoft.Office.Interop.Word.Range footerRange = wordSection.Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
footerRange.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdDarkRed;
footerRange.Font.Size = 10;
footerRange.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
footerRange.Text = "Footer text goes here";
}

//添加内容
document.Content.SetRange(0, 0);
document.Content.Text = "检测报告 " + Environment.NewLine;

//添加段落
Microsoft.Office.Interop.Word.Paragraph para1 = document.Content.Paragraphs.Add(ref missing);
para1.Range.Text = "Para 1 text";
para1.Range.InsertParagraphAfter();

Microsoft.Office.Interop.Word.Paragraph para2 = document.Content.Paragraphs.Add(ref missing);
para2.Range.Text = "Para 2 text";
para2.Range.InsertParagraphAfter();

//表格
Table firstTable = document.Tables.Add(para1.Range, 5, 5, ref missing, ref missing);

firstTable.Borders.Enable = 1;
foreach (Row row in firstTable.Rows)
{
foreach (Cell cell in row.Cells)
{
//表头
if (cell.RowIndex == 1)
{
cell.Range.Text = "Column " + cell.ColumnIndex.ToString();
cell.Range.Font.Bold = 1;
cell.Range.Font.Name = "verdana";
cell.Range.Font.Size = 10;
cell.Shading.BackgroundPatternColor = WdColor.wdColorGray25;
cell.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;
cell.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
}
//行
else
{
cell.Range.Text = (cell.RowIndex - 2 + cell.ColumnIndex).ToString();
}

}

//保存
string fName = "c://export.docx";

document.SaveAs(fName);
document.Close(ref missing, ref missing, ref missing);
document = null;

winword.Quit(ref missing, ref missing, ref missing);
winword = null;

//打开文件
System.Diagnostics.Process.Start(fName);

效果:

C# 生成Word文档_C#编程_03

参考:​​https://www.c-sharpcorner.com/UploadFile/muralidharan.d/how-to-create-word-document-using-C-Sharp/​