C# 获取word文档内的表格内容
答案:3 悬赏:60 手机版
解决时间 2021-02-19 10:34
- 提问者网友:像風在裏
- 2021-02-19 02:37
给定一个word文档,word文档内有一个表格,现在我所需要做的就是获取word文档内那个表格的所有数据,只需要他的纯文本,那些显示的换行符之类的内容统统不要,要怎么写一个方法,获取到这些内容并显示在texetbox内呢?急求高手救援,先谢谢了
最佳答案
- 五星知识达人网友:白昼之月
- 2021-02-19 04:07
取word 的书签值
全部回答
- 1楼网友:長槍戰八方
- 2021-02-19 06:49
争议//前提:一个表格中只有一个书签<br>//原理:获取每个表格的范围range1,获取书签的范围:range2<br>//如果 range1 > range2,那么range2中的内容,就是range1的内容,<br>//countbookmaskdata函数返回的是结果using system;<br>using system.collections.generic;<br>using system.linq;<br>using system.text;<br>using microsoft.office.interop.word;<br>using system.text.regularexpressions;<br>namespace piclesoft.basecode<br>{<br>public class tablevalue<br>{<br>public int ntablepos;<br>public int nrowpos;<br>public int ncolumpos;<br>public range rtablerange;<br>public string strtext;<br>};<br>public class bookmarkvalue<br>{<br>public range rbookmarkrange;<br>public string strbookmarkname;<br>public tablevalue tablevalue = null;<br>};<br>public class textresult<br>{<br>public string bookmaskname;<br>public string text;<br>};<br>public class wordoperate<br>{<br>private microsoft.office.interop.word._application m_owordapplic; // a reference to word application <br>private microsoft.office.interop.word._document m_odoc; // a reference to the document <br>private object missing = system.reflection.missing.value;<br>object wdformat = (int)microsoft.office.interop.word.wdsaveformat.wdformatdocument;//改变文档格式,不能设为wdformatdocumentdefault<br>private object m_objcurdocfullfilename;<br>private string m_strlasterror;<br>public wordoperate()<br>{<br>m_owordapplic = new microsoft.office.interop.word.applicationclass();<br>m_owordapplic.options.confirmconversions = false;<br>m_owordapplic.visible = false;<br>m_owordapplic.displayalerts = microsoft.office.interop.word.wdalertlevel.wdalertsnone;<br>m_objcurdocfullfilename = string.empty;<br>m_strlasterror = string.empty;<br>}<br>private void reseterrmsg()<br>{<br>m_strlasterror = string.empty;<br>}<br>#region 打开文档<br>// open a file (the file must exists) and activate it <br>public void open(string strfilename)<br>{<br>reseterrmsg();<br>try<br>{<br>m_objcurdocfullfilename = strfilename;<br>object readonly = true;<br>object isvisible = true;<br>m_odoc = m_owordapplic.documents.open(ref m_objcurdocfullfilename, ref missing, ref readonly,<br>ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,<br>ref missing, ref missing, ref isvisible, ref missing, ref missing, ref missing, ref missing);<br>m_odoc.activate();<br>}<br>catch (exception ex)<br>{<br>m_strlasterror = ex.message;<br>}<br>}<br>#endregion<br>#region 是否打开成功<br>public bool isopen()<br>{<br>return (m_odoc != null);<br>}<br>#endregion<br>#region 释放<br>/// <summary><br>/// 释放<br>/// </summary><br>public void quit()<br>{<br>reseterrmsg();<br>try<br>{<br>object notsave = microsoft.office.interop.word.wdsaveoptions.wddonotsavechanges;<br>m_odoc.close(ref notsave, ref missing, ref missing);<br>m_owordapplic.quit(ref missing, ref missing, ref missing);<br>if (m_odoc != null)//再关闭<br>{<br>system.runtime.interopservices.marshal.releasecomobject(m_odoc);<br>m_odoc = null;<br>}<br>if (m_owordapplic != null)//再关闭<br>{<br>system.runtime.interopservices.marshal.releasecomobject(m_owordapplic);<br>m_owordapplic = null;<br>}<br>gc.collect();<br>}<br>catch (exception ex)<br>{<br>m_strlasterror = ex.message;<br>}<br>}<br>#endregion<br>#region 获取所有的表格<br>/// <summary><br>/// 获取所有的表格<br>/// </summary><br>/// <param name="tablelist"></param><br>/// <returns></returns><br>public bool getalltablelist(ref list<tablevalue> tablelist)<br>{<br>tablelist = new list<tablevalue>();<br>reseterrmsg();<br>try<br>{<br>for (int tablepos = 1; tablepos <= m_odoc.tables.count; tablepos++)<br>{<br>microsoft.office.interop.word.table nowtable = m_odoc.tables[tablepos];<br>for (int rowpos = 1; rowpos <= nowtable.rows.count; rowpos++)<br>{<br>for (int columpos = 1; columpos <= nowtable.columns.count; columpos++)<br>{<br>try<br>{<br>tablevalue ttablevalue = new tablevalue();<br>ttablevalue.ntablepos = tablepos;<br>ttablevalue.nrowpos = rowpos;<br>ttablevalue.ncolumpos = columpos;<br>string strtext = nowtable.cell(rowpos, columpos).range.text;<br>ttablevalue.strtext = strtext.substring(0, strtext.length - 2);<br>ttablevalue.rtablerange = nowtable.cell(rowpos, columpos).range;<br>tablelist.add(ttablevalue);<br>}<br>catch (exception ex)<br>{<br>continue;<br>}<br>}<br>}<br>}<br>tablelist = tablelist.distinct().tolist();<br>return true;<br>}<br>catch (exception ex)<br>{<br>m_strlasterror = ex.message;<br>return false;<br>}<br>}<br>#endregion<br>#region 获取所有的书签<br>public bool getallbookmarklist(ref list<bookmarkvalue> bookmarklist)<br>{<br>reseterrmsg();<br>bookmarklist = new list<bookmarkvalue>();<br>try<br>{<br>system.collections.ienumerator tenumerator = m_owordapplic.activedocument.bookmarks.getenumerator();<br>while (tenumerator.movenext())<br>{<br>microsoft.office.interop.word.bookmark bk = (microsoft.office.interop.word.bookmark)tenumerator.current;<br>bookmarkvalue bkv = new bookmarkvalue();<br>bkv.rbookmarkrange = bk.range;<br>bkv.strbookmarkname = bk.name;<br>bookmarklist.add(bkv);<br>}<br>bookmarklist = bookmarklist.distinct().tolist();<br>return true;<br>}<br>catch (exception ex)<br>{<br>m_strlasterror = ex.message;<br>return false;<br>}<br>}<br>#endregion<br>#region 计算数据<br>public bool countbookmaskdata(ref list<textresult> reflist)<br>{<br>reflist = new list<textresult>();<br>list<tablevalue> tablelist = null;<br>list<bookmarkvalue> bookmarklist = null;<br>if (!getalltablelist(ref tablelist))<br>{<br>return false;<br>}<br>if (!getallbookmarklist(ref bookmarklist))<br>{<br>return false;<br>}<br>reseterrmsg();<br>try<br>{<br>foreach (bookmarkvalue objbookmark in bookmarklist)<br>{<br>foreach (tablevalue objtable in tablelist)<br>{<br>if (objbookmark.rbookmarkrange.inrange(objtable.rtablerange))<br>{<br>textresult obj = new textresult();<br>obj.bookmaskname = objbookmark.strbookmarkname;<br>obj.text = objtable.strtext;<br>objbookmark.tablevalue = objtable;<br>reflist.add(obj);<br>break;<br>}<br>}<br>}<br>return true;<br>}<br>catch (exception ex)<br>{<br>m_strlasterror = ex.message;<br>return false;<br>}<br>}<br>#endregion<br>#region 去掉从表格中读取的数据后缀<br>string removesuffix(string strvalue)<br>{<br>if (strvalue == null)<br>return strvalue;<br>if (strvalue.length > 1)<br>strvalue = strvalue.substring(0, strvalue.length - 2);<br>return strvalue.trim() ;<br>}<br>#endregion<br>}<br>}
- 2楼网友:话散在刀尖上
- 2021-02-19 05:38
您好,我想向你请教一下,【C# 获取word文档内的内容】的代码该怎么实现呢?(我已设置书签了)
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯