2 回答
TA贡献1780条经验 获得超5个赞
//前提:一个表格中只有一个书签 //原理:获取每个表格的范围range1,获取书签的范围:range2 //如果 range1 > range2,那么range2中的内容,就是range1的内容, //CountBookMaskData函数返回的是结果 |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Word;
using System.Text.RegularExpressions;
namespace Piclesoft.BaseCode
{
public class TableValue
{
public int nTablePos;
public int nRowPos;
public int nColumPos;
public Range rTableRange;
public string strText;
};
public class BookMarkValue
{
public Range rBookMarkRange;
public string strBookMarkName;
public TableValue tableValue = null;
};
public class TextResult
{
public string BookMaskName;
public string Text;
};
public class WordOperate
{
private Microsoft.Office.Interop.Word._Application m_oWordApplic; // a reference to Word application
private Microsoft.Office.Interop.Word._Document m_oDoc; // a reference to the document
private object missing = System.Reflection.Missing.Value;
object wdFormat = (int)Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument;//改变文档格式,不能设为wdFormatDocumentDefault
private object m_objCurDocFullFileName;
private string m_strLastError;
public WordOperate()
{
m_oWordApplic = new Microsoft.Office.Interop.Word.ApplicationClass();
m_oWordApplic.Options.ConfirmConversions = false;
m_oWordApplic.Visible = false;
m_oWordApplic.DisplayAlerts = Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsNone;
m_objCurDocFullFileName = string.Empty;
m_strLastError = string.Empty;
}
private void ResetErrMsg()
{
m_strLastError = string.Empty;
}
#region 打开文档
// Open a file (the file must exists) and activate it
public void Open(string strFileName)
{
ResetErrMsg();
try
{
m_objCurDocFullFileName = strFileName;
object readOnly = true;
object isVisible = true;
m_oDoc = m_oWordApplic.Documents.Open(ref m_objCurDocFullFileName, ref missing, ref readOnly,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);
m_oDoc.Activate();
}
catch (Exception ex)
{
m_strLastError = ex.Message;
}
}
#endregion
#region 是否打开成功
public bool IsOpen()
{
return (m_oDoc != null);
}
#endregion
#region 释放
/// <summary>
/// 释放
/// </summary>
public void Quit()
{
ResetErrMsg();
try
{
object notSave = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
m_oDoc.Close(ref notSave, ref missing, ref missing);
m_oWordApplic.Quit(ref missing, ref missing, ref missing);
if (m_oDoc != null)//再关闭
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(m_oDoc);
m_oDoc = null;
}
if (m_oWordApplic != null)//再关闭
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(m_oWordApplic);
m_oWordApplic = null;
}
GC.Collect();
}
catch (Exception ex)
{
m_strLastError = ex.Message;
}
}
#endregion
#region 获取所有的表格
/// <summary>
/// 获取所有的表格
/// </summary>
/// <param name="tableList"></param>
/// <returns></returns>
public bool GetAllTableList(ref List<TableValue> tableList)
{
tableList = new List<TableValue>();
ResetErrMsg();
try
{
for (int tablePos = 1; tablePos <= m_oDoc.Tables.Count; tablePos++)
{
Microsoft.Office.Interop.Word.Table nowTable = m_oDoc.Tables[tablePos];
for (int rowPos = 1; rowPos <= nowTable.Rows.Count; rowPos++)
{
for (int columPos = 1; columPos <= nowTable.Columns.Count; columPos++)
{
try
{
TableValue tTableValue = new TableValue();
tTableValue.nTablePos = tablePos;
tTableValue.nRowPos = rowPos;
tTableValue.nColumPos = columPos;
string strText = nowTable.Cell(rowPos, columPos).Range.Text;
tTableValue.strText = strText.Substring(0, strText.Length - 2);
tTableValue.rTableRange = nowTable.Cell(rowPos, columPos).Range;
tableList.Add(tTableValue);
}
catch (Exception ex)
{
continue;
}
}
}
}
tableList = tableList.Distinct().ToList();
return true;
}
catch (Exception ex)
{
m_strLastError = ex.Message;
return false;
}
}
#endregion
#region 获取所有的书签
public bool GetAllBookMarkList(ref List<BookMarkValue> bookMarkList)
{
ResetErrMsg();
bookMarkList = new List<BookMarkValue>();
try
{
System.Collections.IEnumerator tEnumerator = m_oWordApplic.ActiveDocument.Bookmarks.GetEnumerator();
while (tEnumerator.MoveNext())
{
Microsoft.Office.Interop.Word.Bookmark bk = (Microsoft.Office.Interop.Word.Bookmark)tEnumerator.Current;
BookMarkValue bkv = new BookMarkValue();
bkv.rBookMarkRange = bk.Range;
bkv.strBookMarkName = bk.Name;
bookMarkList.Add(bkv);
}
bookMarkList = bookMarkList.Distinct().ToList();
return true;
}
catch (Exception ex)
{
m_strLastError = ex.Message;
return false;
}
}
#endregion
#region 计算数据
public bool CountBookMaskData(ref List<TextResult> refList)
{
refList = new List<TextResult>();
List<TableValue> tableList = null;
List<BookMarkValue> bookMarkList = null;
if (!GetAllTableList(ref tableList))
{
return false;
}
if (!GetAllBookMarkList(ref bookMarkList))
{
return false;
}
ResetErrMsg();
try
{
foreach (BookMarkValue objBookMark in bookMarkList)
{
foreach (TableValue objTable in tableList)
{
if (objBookMark.rBookMarkRange.InRange(objTable.rTableRange))
{
TextResult obj = new TextResult();
obj.BookMaskName = objBookMark.strBookMarkName;
obj.Text = objTable.strText;
objBookMark.tableValue = objTable;
refList.Add(obj);
break;
}
}
}
return true;
}
catch (Exception ex)
{
m_strLastError = ex.Message;
return false;
}
}
#endregion
#region 去掉从表格中读取的数据后缀
string RemoveSuffix(string strValue)
{
if (strValue == null)
return strValue;
if (strValue.Length > 1)
strValue = strValue.Substring(0, strValue.Length - 2);
return strValue.Trim() ;
}
#endregion
}
}
TA贡献1900条经验 获得超5个赞
先在已有的word文档中输入一串空格[长度自定,如图1所示],然后再选中这窜空格,在此基础上插入一个书签,
可以看到,书签被分割成了一对中括号,中括号里面的内容就是range的内容,那么,大家可以随心所欲写代码了,
原码如下:
private void button1_Click(object sender, EventArgs e)
{
object missingValue = System.Reflection.Missing.Value;
object myTrue = false;
object fileName = this.txt_file.Text.Trim().ToString();
Word._Application oWord = new Word.ApplicationClass();
Word._Document oDoc;
oDoc = oWord.Documents.Open(ref fileName, ref missingValue,
ref myTrue, ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue);
MessageBox.Show(oDoc.Bookmarks.Count.ToString());
//写入word文档
object tmp = "msr_01";
Word.Range tmpRng = oWord.ActiveDocument.Bookmarks.get_Item(ref tmp).Range;
tmpRng.Text = "鲁能工程有限公司221";
//由于附值以后书签自动消除,为了以后便于修改,需要把书签再自动生成一个
object oRng = tmpRng;
oDoc.Bookmarks.Add(tmp.ToString(), ref oRng);
object bSaveChange = true;
oDoc.Close(ref bSaveChange, ref missingValue, ref missingValue);
}
注意:
1、[这是往word文档里面写入数据,如果要取得数据可以用range.text来取得]
2、需要的using指令集是:using Word = Microsoft.Office.Interop.Word;
希望帮到你
- 2 回答
- 0 关注
- 267 浏览
添加回答
举报