栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

解决引用Microsoft.Office.Core 和Microsoft.Office.Interop.Word

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

解决引用Microsoft.Office.Core 和Microsoft.Office.Interop.Word

 

1. 解决引用Microsoft.Office.Core --> Interop.Microsoft.Office.Core.dll

直接右击引用-->添加引用-->在COM页面的下拉框中,找到(OFFICE2003)Microsoft Word 11.0 Object Library /(OFFICE2007)Microsoft Word 12.0 Object Library,按“确定”即可。

 

2. 解决引用Microsoft.Office.Interop.Word --> Microsoft.Office.Interop.Word.dll

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Reflection;
using Word = Microsoft.Office.Interop.Word;



namespace MSWord
{

    /// 
    /// 用于生成word文件
    /// 
    public class FileOperator
    {

        public static  void CreateDoc()
        {
            // Object path = Environment.CurrentDirectory + "\test.doc";
            //Object path = @"C:UsersmaynDesktop测试专用testtemp.dotx";

            Object path = @"C:UsersmaynDesktop测试专用test.doc";

            var wordApp = new Word.Application
            {
                Visible = true
            };
            object unite = Word.WdUnits.wdStory;
            Word.Document wordDoc = wordApp.Documents.Add();
            wordDoc.PageSetup.PaperSize = Word.WdPaperSize.wdPaperA4;
            wordApp.Selection.EndKey(ref unite);//将光标移到文本末尾
            wordDoc.Paragraphs.Last.Range.Font.Size = 18;
            wordDoc.Paragraphs.Last.Range.Text = "质量报告n";
            wordDoc.Paragraphs.Last.Range.Font.Bold = 1;
            wordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
            wordApp.Selection.EndKey(ref unite);//将光标移到文本末尾


            wordApp.Selection.EndKey(ref unite);//将光标移到文本末尾
            wordDoc.Paragraphs.Last.Range.Font.Size = 12;
            wordDoc.Paragraphs.Last.Range.Text = "工程名称:n";
            wordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;

            wordApp.Selection.EndKey(ref unite);//将光标移到文本末尾
            wordDoc.Paragraphs.Last.Range.Font.Size = 10;
            wordDoc.Paragraphs.Last.Range.Text = "井深度/米:";
            wordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;

            wordDoc.Paragraphs.Last.Range.InsertAfter("                          ");//加入空格

            wordApp.Selection.EndKey(ref unite);//将光标移到文本末尾
            wordDoc.Paragraphs.Last.Range.Font.Size = 10;
            wordDoc.Paragraphs.Last.Range.InsertAfter("倾斜度/百分比:n");
            wordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;

            wordApp.Selection.EndKey(ref unite);//将光标移到文本末尾
            wordDoc.Paragraphs.Last.Range.Font.Size = 10;
            wordDoc.Paragraphs.Last.Range.Text = "泥厚深度/米:";
            wordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;

            wordDoc.Paragraphs.Last.Range.InsertAfter("                          ");//加入空格

            wordApp.Selection.EndKey(ref unite);//将光标移到文本末尾
            wordDoc.Paragraphs.Last.Range.Font.Size = 10;
            wordDoc.Paragraphs.Last.Range.InsertAfter("孔径/米:n");
            wordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;

            wordDoc.Shapes.AddLine(80, 100, 520, 100);//在指定位置画横线

            //WdSaveFormat为Word 2003文档的保存格式
            object format = Word.WdSaveFormat.wdFormatDocument;// office 2007就是wdFormatDocumentDefault
                                                               //将wordDoc文档对象的内容保存为DOCX文档
            wordDoc.SaveAs(ref path, ref format);
            wordDoc.Close();
            //关闭wordApp组件对象
            wordApp.Quit();
        }




    }
}

 

 

生成word文件样式如下:

 

 

word模版进行数据填充
模版样式如下:插入书签

 

 

在模版中定义相关位置的书签,书签与填充值在代码中保存为Map对象,是key-value关系。
代码如下:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.IO;
using Word = Microsoft.Office.Interop.Word;


namespace MSWord
{


    /// 
    /// 根据word模版来生成报告
    /// 
    public class AccordingTemplentPdf
    {


        public void CreateReportPdf()
        {
            //string path = @"E:testtemp.dotx";
            //SetValue(path, @"E:testTemplete2019111.doc");
            string path = @"C:UsersmaynDesktop测试专用testtemp.docx";
            SetValue(path, @"C:UsersmaynDesktop测试专用testTemplete2019111.doc");
        }
        /// 
        /// 给书签内容填充数据,并调用生成word方法生成word报告
        /// 
        /// 模板文件
        /// 新文件
        public bool SetValue(string urlstring, string fileName)
        {
            bool result = false;
            try
            {
                string project = "测试工程1";
                string value1 = "11.11";
                string value2 = "22.22";
                string value3 = "33.33";
                string value4 = "44.44";
                #region 声明参数
                Dictionary DList = new Dictionary
                {
                    { "project", project },
                    { "value1", value1 },
                    { "value2", value2 },
                    { "value3", value3 },
                    { "value4", value4 }
                };
                #endregion
                if (CreateWord(urlstring, fileName, DList))
                {
                    result = true;
                }
            }
            catch
            {
                return false;
            }
            return result;
        }
        /// 
        /// 生成word
        /// 
        /// 
        /// 
        /// 
        /// 
        public static bool CreateWord(string templateFile, string fileName, Dictionary myDictionary)
        {
            try
            {
                //生成word程序对象
                Word.Application app = new Word.Application();
                //模板文件
                string TemplateFile = templateFile;
                //模板文件拷贝到新文件
                File.Copy(TemplateFile, fileName);
                //生成documnet对象
                Word._Document doc = new Word.Document();
                object Obj_FileName = fileName;
                object Visible = false;
                object ReadOnly = false;
                object missing = System.Reflection.Missing.Value;
                //打开文件
                doc = app.Documents.Open(ref Obj_FileName, ref missing, ref ReadOnly, ref missing,
                    ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref Visible,
                    ref missing, ref missing, ref missing,
                    ref missing);
                doc.Activate();
                #region 声明参数
                if (myDictionary.Count > 0)
                {
                    object what = Word.WdGoToItem.wdGoToBookmark;
                    object WordMarkName;
                    foreach (var item in myDictionary)
                    {
                        WordMarkName = item.Key;
                        doc.ActiveWindow.Selection.GoTo(ref what, ref missing, ref missing, ref WordMarkName);//光标转到书签的位置
                        doc.ActiveWindow.Selection.TypeText(item.Value);//插入的内容,插入位置是word模板中书签定位的位置
                        //doc.ActiveWindow.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;//设置当前定位书签位置插入内容的格式
                    }
                }
                #endregion
                //输出完毕后关闭doc对象
                object IsSave = true;
                doc.Close(ref IsSave, ref missing, ref missing);
                return true;
            }
            catch
            {

                return false;
            }
        }




        


    }
}

 

 

 

 

 

 

 

 

 

 

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/923159.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号