C#读取Word示例代码.doc

上传人:m**** 文档编号:548031768 上传时间:2023-08-08 格式:DOC 页数:6 大小:72KB
返回 下载 相关 举报
C#读取Word示例代码.doc_第1页
第1页 / 共6页
C#读取Word示例代码.doc_第2页
第2页 / 共6页
C#读取Word示例代码.doc_第3页
第3页 / 共6页
C#读取Word示例代码.doc_第4页
第4页 / 共6页
C#读取Word示例代码.doc_第5页
第5页 / 共6页
点击查看更多>>
资源描述

《C#读取Word示例代码.doc》由会员分享,可在线阅读,更多相关《C#读取Word示例代码.doc(6页珍藏版)》请在金锄头文库上搜索。

1、C#读取Word文档示例代码在Visual Studio 2005中新建控制台应用程序,命名为GetWordContent添加如下引用:Microsoft.Office.Interop.WordSystem.DrawingSystem.Windows.Forms详细代码如下:using System;using System.Collections.Generic;using System.Text;using System.IO;namespace GetWordContent class Program STAThread static void Main(string args) /根据

2、Y需要a输?入?正y确的?OA文?档路径? string fileName = E:ylq_2010GetWordContentGetWordContentbinDebugOA文?档.doc; bool isOpen = OfficeDocIO.OpenWord(fileName); Console.WriteLine(打开aWord文?档0是?否?成功|:o1, Path.GetFileName(fileName), isOpen); Console.WriteLine(); Console.WriteLine(获?取?分?段?落?文?本?内容Y,?段?落?索引y从1开a始?:o); int

3、 paragraph = OfficeDocIO.getParCount(); Console.WriteLine(共2有D0段?, paragraph);/段?落?索引y从1开a始??? for (int i = 1; i = paragraph; i+) Console.WriteLine(段?落?0:1, i, OfficeDocIO.ReadPar(i); Console.WriteLine(); Console.WriteLine(直接获?取?Word的?全?部?文?本?内容Y:o); Console.WriteLine(OfficeDocIO.GetDocContent(); Of

4、ficeDocIO.CloseWord(); Console.ReadLine(); / / 操作Word的?接口帮?助类 / public class OfficeDocIO private static Microsoft.Office.Interop.Word._Application appWord = null; private static Microsoft.Office.Interop.Word._Document docWord = null; / / 打开a一?个?Word文?档,?相对?的?方?法是?CloseWord() / / 文?件t名? / public stat

5、ic bool OpenWord(string file) appWord = new Microsoft.Office.Interop.Word.Application(); object path = file; object missing = System.Reflection.Missing.Value; object readOnly = true; try docWord = appWord.Documents.Open(ref path, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref

6、missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); return true; catch (Exception e) Console.WriteLine(e.Message); appWord = null; return false; / / 读取?word文?档的?第i段? / / / public static object ReadPar(int i) try string temp =

7、 docWord.Paragraphsi.Range.Text; return temp; catch (Exception e) Console.WriteLine(Error: + e.ToString(); return null; / / 返回?word文?档一?共2几?段? / / public static int getParCount() return docWord.Paragraphs.Count; / / 获?取?文?档的?文?本?内容Y / / public static object GetDocContent() return docWord.Content.Tex

8、t; / / 提取?word文?档中D表格?的?内容Y / / 表格?索引y / 行D号? / 列D号? / 内容Y类型 1为a文?本? 2为a图?片? / 提取?出?来的?内容Y public static object GetWordTableContent(int tableid, int rowIndex, int colIndex, int type) if (docWord != null & docWord.Tables.Count tableid - 1) object content = null; try switch (type) case 1: if (docWord.Tablestableid.Rows.Count (rowIndex - 1) & docWord.Tablestableid.Columns.Count (colIndex - 1) content = docWord.Tablestableid.Cell(rowIndex, colIndex).Range.Text.ToString(); break; case 2: if (docWord.Tablestableid.Rows.Count (rowIndex - 1) &

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 生活休闲 > 社会民生

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号