踏踏实实学编程 基于C#操作Word文档中的Bookmark
2012-03-30 16:56
537 查看
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
}
}
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
}
}
相关文章推荐
- 基于C#操作Word文档中的Bookmark
- C#中操作Word(5)—— 利用BookMark对象写Word文档
- 基于WebForm的操作Word文档实例
- C#操作Word(一)——C#中向Word文档中插入Windows控件
- 用C#编程合并多个WORD文档
- 使用 C# 编程对 RTF 文档进行操作 [转载]
- C# Aspose.Word 操作word文档【三】
- C#操作Word文档(Office 2007)
- C#操作Word (2)-- 打开&关闭Word文档
- 黄聪:C# web word文档操作,任意指定位置插入图片,表格简单操作
- C# 中使用Word文档对图像进行操作
- C# 操作word文档
- C#操作Word文档(加密、解密、对应书签插入分页符)
- C#操作Word文档
- 用C#编程合并多个WORD文档
- 编程技巧:如何在C#中读取Word文档
- C# 操作word总结(一)——建立文档和添加页眉页脚
- C#编程实现动态生成Word文档
- C#操作Word文档(Office 2007)
- C#操作Word总结(二)——设置文档格式并添加文本内容、超链接、添加图片