Microsoft.Office.Interop.Word通过模板生成文档

这篇博客介绍如何利用C#和Microsoft.Office.Interop.Word.dll,在Visual Studio 2013环境下,通过模板来创建动态的Word文档。
摘要由CSDN通过智能技术生成

本博客实现通过模板动态生成word文档。
- C#
- Visual Studio 2013
- Microsoft.Office.Interop.Word.dll –>需要添加该引用,可以在网上下载

关键代码如下:

        public void ExportWord()
        {           
            //取得模板路径
            string wordTemplatePath = @"C:\XXXXX\模板.doc";
            //导出文件名称
            fileName = "我的word" + System.DateTime.Now.ToString("yyyMMddHHmmss") + ".doc";
            //导出文件路径
            string outPutFilePath = @"C:\XXXXX\" + fileName;
            //创建word对象
            object missing = System.Reflection.Missing.Value;
            object readOnly = false;         
            Word.Application app = new Word.Application();           
            Word.Document doc = null;                     
            File.Copy(wordTemplatePath, outPutFilePath, true);//true 相同文件名时,覆盖
            //打开doc
            object tempword = (object)wordTemplatePath;
            object output = (object)outPutFilePath;
            doc = app.Documents.Open(ref tempword, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
            doc.Activate();

            //插入4行4列表格  
            WdLineStyle OutStyle = WdLineStyle.wdLineStyleThickThinLargeGap;
            WdLineStyle InStyle = WdLineStyle.wdLineStyleSingle;
            AddSimpleTable(app, d
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值