C# 生成Word文档

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

1. 添加引用

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

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

效果:

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

 

  • 3
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小鹰信息技术服务部

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值