2 回答

TA貢獻(xiàn)1780條經(jīng)驗(yàn) 獲得超5個(gè)贊
//前提:一個(gè)表格中只有一個(gè)書簽 //原理:獲取每個(gè)表格的范圍range1,獲取書簽的范圍:range2 //如果 range1 > range2,那么range2中的內(nèi)容,就是range1的內(nèi)容, //CountBookMaskData函數(shù)返回的是結(jié)果 |
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;//改變文檔格式,不能設(shè)為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)//再關(guān)閉
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(m_oDoc);
m_oDoc = null;
}
if (m_oWordApplic != null)//再關(guān)閉
{
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 計(jì)算數(shù)據(jù)
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 去掉從表格中讀取的數(shù)據(jù)后綴
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貢獻(xiàn)1900條經(jīng)驗(yàn) 獲得超5個(gè)贊
先在已有的word文檔中輸入一串空格[長(zhǎng)度自定,如圖1所示],然后再選中這竄空格,在此基礎(chǔ)上插入一個(gè)書簽,
可以看到,書簽被分割成了一對(duì)中括號(hào),中括號(hào)里面的內(nèi)容就是range的內(nèi)容,那么,大家可以隨心所欲寫代碼了,
原碼如下:
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";
//由于附值以后書簽自動(dòng)消除,為了以后便于修改,需要把書簽再自動(dòng)生成一個(gè)
object oRng = tmpRng;
oDoc.Bookmarks.Add(tmp.ToString(), ref oRng);
object bSaveChange = true;
oDoc.Close(ref bSaveChange, ref missingValue, ref missingValue);
}
注意:
1、[這是往word文檔里面寫入數(shù)據(jù),如果要取得數(shù)據(jù)可以用range.text來取得]
2、需要的using指令集是:using Word = Microsoft.Office.Interop.Word;
希望幫到你
- 2 回答
- 0 關(guān)注
- 298 瀏覽
添加回答
舉報(bào)