解決引用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
{

    /// <summary>
    /// 用於生成word文件
    /// </summary>
    public class FileOperator
    {

        public static  void CreateDoc()
        {
            // Object path = Environment.CurrentDirectory + "\\test.doc";
            //Object path = @"C:\Users\mayn\Desktop\測試專用\testtemp.dotx";

            Object path = @"C:\Users\mayn\Desktop\測試專用\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
{


    /// <summary>
    /// 根據word模版來生成報告
    /// </summary>
    public class AccordingTemplentPdf
    {


        public void CreateReportPdf()
        {
            //string path = @"E:\testtemp.dotx";
            //SetValue(path, @"E:\testTemplete2019111.doc");
            string path = @"C:\Users\mayn\Desktop\測試專用\testtemp.docx";
            SetValue(path, @"C:\Users\mayn\Desktop\測試專用\testTemplete2019111.doc");
        }
        /// <summary>
        /// 給書籤內容填充數據,並調用生成word方法生成word報告
        /// </summary>
        /// <param name="urlstring">模板文件</param>
        /// <param name="fileName">新文件</param>
        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<string, string> DList = new Dictionary<string, string>
                {
                    { "project", project },
                    { "value1", value1 },
                    { "value2", value2 },
                    { "value3", value3 },
                    { "value4", value4 }
                };
                #endregion
                if (CreateWord(urlstring, fileName, DList))
                {
                    result = true;
                }
            }
            catch
            {
                return false;
            }
            return result;
        }
        /// <summary>
        /// 生成word
        /// </summary>
        /// <param name="templateFile"></param>
        /// <param name="fileName"></param>
        /// <param name="myDictionary"></param>
        /// <returns></returns>
        public static bool CreateWord(string templateFile, string fileName, Dictionary<string, string> 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;
            }
        }




        


    }
}

 

 

 

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章