zoukankan      html  css  js  c++  java
  • 基于C#操作Word文档中的Bookmark

    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
    }
    }
  • 相关阅读:
    程序设计思维与实践 Week5 作业 (3/4/数据班)
    程序设计思维与实践 Week6 作业 (3/4/数据班)
    Effective C++笔记(更新中...)
    二叉树的序列化与反序列化
    矩阵乘法的顺序安排问题 Python简单实现
    Python 读写Excel文件 总结
    2019美赛D题 人员疏散模型Python编程
    函数绘图语言 西电编译原理大作业
    洛谷试炼场 动态规划专练
    2019 IEEEXtreme 13.0 Impact Factor 影响因子
  • 原文地址:https://www.cnblogs.com/jin256/p/2355433.html
Copyright © 2011-2022 走看看