关于读取和写入word2003的书签内容
来源:互联网 发布:牌匾设计软件下载 编辑:程序博客网 时间:2024/05/18 02:33
原文出处 http://www.blue1000.com/bkhtml/2008-06/57215.htm
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HTMLControls;
using Microsoft.office;
using Microsoft.office.Core;
using Microsoft.office.Interop;
using Microsoft.office.Interop.word;
打开word2003文档,项目中的文档模板实际是doc的文档,dot的很不好用.
做doc文档模板的时候不要用空格留空白,使用段落缩进的方式.标签的添加等文档格式完全编辑好再添加.
对于要预留空白的内容,通过插入1个只有1个单元格(无边框)的方式来进行.这样保证单元格以后内容位置固定,不会因为插入了文字内容而移动位置,这队固定格式公文很有好处.
打开word模板和word文件#region 打开word模板和word文件
public static void OpenwordDot()
{
}
private void OpenwordDoc(string FileName, ref Microsoft.office.Interop.Word.Document wDoc, ref Microsoft.Office.Interop.Word.Application WApp)
{
if (FileName == "") return;
Microsoft.office.Interop.word.Document thisDocument = null;
Microsoft.office.Interop.word.FormFields formFields = null;
Microsoft.office.Interop.word.Application thisApplication = new Microsoft.Office.Interop.Word.ApplicationClass();
thisApplication.Visible = true;
thisApplication.Caption = "";
thisApplication.Options.CheckSpellingAsYouType = false;
thisApplication.Options.CheckGrammarAsYouType = false;
Object filename = FileName;
Object ConfirmConversions = false;
Object ReadOnly = true;
Object AddToRecentFiles = false;
Object PasswordDocument = System.Type.Missing;
Object PasswordTemplate = System.Type.Missing;
Object Revert = System.Type.Missing;
Object WritePasswordDocument = System.Type.Missing;
Object WritePasswordTemplate = System.Type.Missing;
Object Format = System.Type.Missing;
Object Encoding = System.Type.Missing;
Object Visible = System.Type.Missing;
Object OpenAndRepair = System.Type.Missing;
Object DocumentDirection = System.Type.Missing;
Object NoEncodingDialog = System.Type.Missing;
Object XMLTransform = System.Type.Missing;
try
{
Microsoft.office.Interop.word.Document wordDoc =
thisApplication.Documents.Open(ref filename, ref ConfirmConversions,
ref ReadOnly, ref AddToRecentFiles, ref PasswordDocument, ref PasswordTemplate,
ref Revert, ref WritePasswordDocument, ref WritePasswordTemplate, ref Format,
ref Encoding, ref Visible, ref OpenAndRepair, ref DocumentDirection,
ref NoEncodingDialog, ref XMLTransform);
thisDocument = wordDoc;
wDoc = wordDoc;
WApp = thisApplication;
formFields = wordDoc.FormFields;
}
catch (Exception ex)
{
}
}
#endregion
读取文档中标签数据处理方法
1 获取文档中的标签列表.把文档对象的书签列表读到IEnumerator中.
2 通过枚举推进的方式读取每个书签.
3 关闭文档
//获取标签数据
public string GetDocumentBookmarkData(string FileName)
{
Microsoft.office.Interop.word.Document wDoc = null;
Microsoft.office.Interop.word.Application wApp = null;
this.OpenwordDoc(FileName,ref wDoc, ref wApp);
object oEndOfDoc = "//endofdoc";
object missing = System.Reflection.Missing.Value;
string str="";
System.Collections.IEnumerator enu=wApp.ActiveDocument.Bookmarks.GetEnumerator();
while(enu.MoveNext())
{
Microsoft.office.Interop.word.Bookmark bk = (Microsoft.Office.Interop.Word.Bookmark)enu.Current;
str += "{"+bk.Name.ToString() + ":"+bk.Range.Text+"}";
}
object o = null;
wDoc.Close(ref missing, ref missing, ref missing);
wApp.Quit(ref missing, ref missing, ref missing);
if (wDoc!=null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(wDoc);
wDoc = null;
}
if (wApp != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(wApp);
wApp = null;
}
GC.Collect();
return str;
}
往书签中写入数据
1 把书签的名字通过枚举的方式读出来,写到数组里(图/表格等特殊数据书签要处理掉)
2 读取数据库数据表内容写入书签初.注意技巧.
a 检查文档书签集合中存在书签
b 获取文档书签,并选择他,写入数据到selection
c 移动书签的end到合适位置,否则读书签数据永远只读到书签定义处的字符.
d 对于图/表格等的插入需要特殊处理.
e 扫尾 另存.不要覆盖原来模板哦
//设定标签的数据
public string SetDocumentBookmarkData(string FileName,System.Data.DataTable dt)
{
//打开文档
Microsoft.office.Interop.word.Document wDoc = null;
Microsoft.office.Interop.word.Application wApp = null;
this.OpenwordDoc(FileName, ref wDoc, ref wApp);
object oEndOfDoc = "//endofdoc";
object missing = System.Reflection.Missing.Value;
//设定标签数据
System.Collections.IEnumerator enu = wApp.ActiveDocument.Bookmarks.GetEnumerator();
string[] strbook=new string[dt.Columns.Count-1];
int i=0;
Microsoft.office.Interop.word.Bookmark bk=null;
while (enu.MoveNext())
{
bk = (Microsoft.office.Interop.word.Bookmark)enu.Current;
if (bk.Name.ToString().Trim()!="Table")
{
strbook[i] = bk.Name.ToString();
i++;
}
}
object tempobject=null;
int length = 0;
for (i = 0; i < strbook.Length;i++ )
{
tempobject = strbook[i].ToString();
if (wApp.ActiveDocument.Bookmarks.Exists(strbook[i].ToString()))
{
wApp.ActiveDocument.Bookmarks.get_Item(ref tempobject).Select();
wApp.Selection.Text = dt.Rows[0][strbook[i]].ToString();
length = dt.Rows[0][strbook[i]].ToString().Length;
wApp.ActiveDocument.Bookmarks.get_Item(ref tempobject).End =wApp.ActiveDocument.Bookmarks.get_Item(ref tempobject).End + length;
}
}
InsertTabletoBookmark("Table",ref wDoc, ref wApp);
//收尾工作
object o = null;
string guid = System.Guid.NewGuid().ToString();
object sFileName = "D:/Test/office/word/" + guid + ".doc";
if (wDoc.SaveFormat == (int)Microsoft.office.Interop.word.WdSaveFormat.wdFormatDocument)
{
wDoc.Application.ActiveDocument.SaveAs(ref sFileName, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing,
ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing);
}
wDoc.Close(ref missing, ref missing, ref missing);
wApp.Quit(ref missing, ref missing, ref missing);
if (wDoc != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(wDoc);
wDoc = null;
}
if (wApp != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(wApp);
wApp = null;
}
GC.Collect();
return guid + ".doc";
}
表格的插入方法
1 表格的插入很简单,图表可参照
2 表格插入后如何获取表格请注意.用书签的Range对象的Tables集合
插入表格数据#region 插入表格数据
private bool InsertTabletoBookmark(string objTable,ref Microsoft.office.Interop.word.Document wDoc, ref Microsoft.Office.Interop.Word.Application WApp)
{
object oEndOfDoc = "//endofdoc";
object missing = System.Reflection.Missing.Value;
object objBookmark = objTable;
WApp.ActiveDocument.Bookmarks.get_Item(ref objBookmark).Select();
Microsoft.office.Interop.word.Table table = wDoc.Tables.Add(WApp.Selection.Range, 3, 4, ref missing, ref missing);
table.Cell(1, 1).Range.Text = "表:" +WApp.ActiveDocument.Bookmarks.get_Item(refobjBookmark).Range.Tables[1].Rows.Count;
return true;
}
结束语
由于生成文档用户修改后往往会丢掉书签,数据就读不到了,所以生成文档还不能提交给用户修改保存,那位有办法的欢迎指教.
- 关于读取和写入word2003的书签内容
- 关于读取和写入word2003的书签内容
- 关于读取和写入word2003的书签内容
- 用java读取word2003和word2007的内容
- 用java读取word2003和word2007的内容
- 用java读取word2003和word2007的内容
- poi对word2003或者2007的图片读取和写入操作
- java按指定编码写入和读取文件内容的
- 读取文本的内容和将文本写入文件中
- Scala文件的读取和写入 从控制台输入内容
- 怎样读取文件内容和写入文件
- 关于OpenCV不能读取视频和写入视频的问题
- JAVA中关于文件的读取和写入操作
- 读取文件内容和向文件中写入内容
- Java读取文件内容和写入内容到文件
- 读取写入文件内容
- 文件的读取和写入
- Execl 的读取和写入
- ASP.NET MVC框架开发系列课程二
- ASP.NET MVC框架开发系列课程一
- 推荐引擎反思
- Scrapy 轻松定制网络爬虫
- 技术与人文的结晶 ——搜索引擎技术漫谈
- 关于读取和写入word2003的书签内容
- 开发宝典,程序员专用工具,编程工具
- 关于HTC Touch Diamond 2的ROM和WM软件开发高手看过来…
- 数据库优化方法(二)
- 粗心弄出了2个ORA- 错误。
- windbg+ollydbg备忘
- 测试一下用Word 2010 发布博客到cnblogs
- 微软 Microsoft® 手机空间 My Phone ——在线备份您的联系人和短信
- 蜘蛛爬虫网络高像素图片抓取工具[ZSpider.NET]