踏踏實實學編程 基於C#操作Word文檔中的Bookmark

來源:互聯網
上載者:User

http://www.cnblogs.com/jin256/archive/2012/02/17/2355433.html

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Office.Interop.Word;
using System.Data;

namespace WebApplication1
{
    public static class FunctionHelp
    {
        #region Parameters
        private static Microsoft.Office.Interop.Word._Document MyDoc;
        private static Microsoft.Office.Interop.Word._Application MyWord;
        private static object Nothing = System.Reflection.Missing.Value;
        #endregion

        #region Share Methods

        private static Microsoft.Office.Interop.Word._Document OpenWord(Microsoft.Office.Interop.Word._Application wordApp,string templatePath)
        {
            object Visible = false;
            object newTemp = false;
            wordApp = new Microsoft.Office.Interop.Word.Application();
            object templateFile = templatePath + "\\New.doc";
            try
            {
                return wordApp.Documents.Open(ref templateFile, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
                                            ref Nothing, ref Nothing, ref Visible, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
            }
            catch
            {
                return null;
            }
        }

        private static bool SaveWord(Microsoft.Office.Interop.Word._Document word,string savePath)
        {
            object Visible = false;
            object filename = savePath + "\\King.doc";
            try
            {
                word.SaveAs(filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
                                ref Nothing, ref Nothing, ref Nothing, ref Visible, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
                CloseWord(MyDoc,MyWord);
                return true;
            }
            catch (Exception ex)
            {
                CloseWord(MyDoc, MyWord);
                return false;
            }
        }

        private static void CloseWord(Microsoft.Office.Interop.Word._Document word,Microsoft.Office.Interop.Word._Application wordApp)
        {
            object missingValue = Type.Missing;
            object doNotSaveChanges = WdSaveOptions.wdDoNotSaveChanges;
            if (word != null)
                word.Close(ref doNotSaveChanges, ref missingValue, ref missingValue);
            if (wordApp != null)
                wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
        }

        #endregion

        #region Static Methods
        /// <summary>
        /// 根據Word文檔內的BookMark添加資料
        /// </summary>
        /// <param name="dtSource"></param>
        /// <param name="templatePath"></param>
        /// <param name="savePath"></param>
        /// <returns>是否儲存成功</returns>
        public static bool ExportWord(DataTable dtSource, string templatePath, string savePath)
        {
            MyDoc = OpenWord(MyWord, templatePath);
            if (MyDoc != null)
            {
                foreach (Microsoft.Office.Interop.Word.Bookmark bm in MyDoc.Bookmarks)
                {
                    bm.Select();
                    switch (bm.Name)
                    {
                        case "書籤名":
                            bm.Range.Text = 根據dtSource給書籤賦值
                            break;
                        ...
                    }
                }
            }
            return SaveWord(MyDoc, savePath);
        }

        #endregion

        #region Public Methods
        #endregion
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.