如何用C#读取Word表格中每个单元格内的每个字的属性? 如何用C#读取Word表格中每个单元格内的每个字的属性?在线等待……不胜感激!!比如说对于Word文档的范围可以这样确定Word.Range rng = doc.Content; 对与单元格的这个范围应该怎么定义呢? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 1//将读取Word表格封装与方法中。2public string ReadWord(string fileName, int rowIndex, int colIndex)3{4 ApplicationClass cls = null;5 Document doc = null;67 Table table = null;8 object missing = Missing.Value;910 object path = fileName;11 cls = new ApplicationClass();1213 try14 {15 doc = cls.Documents.Open16 (ref path, ref missing, ref missing, ref missing,17 ref missing, ref missing, ref missing, ref missing,18 ref missing, ref missing, ref missing, ref missing,19 ref missing, ref missing, ref missing, ref missing);20 table = doc.Tables[1];21 string text = table.Cell(rowIndex, colIndex).Range.Text.ToString();22 text = text.Substring(0, text.Length - 2); //去除尾部的23 return text;24 }25 catch (Exception ex)26 {2728 return ex.Message;29 }30 finally31 {32 if (doc != null)33 doc.Close(ref missing, ref missing, ref missing);34 cls.Quit(ref missing, ref missing, ref missing);35 }36}这个方法用于读取Word表格中某个单元格的数据。其中的参数分别为文件名(包括路径),行号,列号。class WordTableRead2{3 private string fileName;4 private ApplicationClass cls = null;5 private Document doc = null;6 private Table table = null;7 private object missing = Missing.Value;8 //Word是否处于打开状态9 private bool openState;101112 /**//// <summary>13 /// 自定义构造方法14 /// </summary>15 /// <param name="fileName">包含路径的文件名</param>16 public WordTableRead(string fileName)17 {18 this.fileName = fileName;19 }20 21 /**//// <summary>22 /// 打开Word文档23 /// </summary>24 public void Open()25 {26 object path = fileName;27 cls = new ApplicationClass();28 try29 {30 doc = cls.Documents.Open31 (ref path, ref missing, ref missing, ref missing,32 ref missing, ref missing, ref missing, ref missing,33 ref missing, ref missing, ref missing, ref missing,34 ref missing, ref missing, ref missing, ref missing);35 openState = true;36 }37 catch38 {39 openState = false;40 }41 }42 43 /**//// <summary>44 /// 返回指定单元格中的数据45 /// </summary>46 /// <param name="tableIndex">表格号</param>47 /// <param name="rowIndex">行号</param>48 /// <param name="colIndex">列号</param>49 /// <returns>单元格中的数据</returns>50 public string ReadWord(int tableIndex, int rowIndex, int colIndex)51 {52 //Give the value to the tow Int32 params.5354 try55 {56 if (openState == true)57 {58 table = doc.Tables[tableIndex];59 string text = table.Cell(rowIndex, colIndex).Range.Text.ToString();60 text = text.Substring(0, text.Length - 2); //去除尾部的61 return text;62 }63 else64 {65 return "";66 }67 }68 catch69 {70 return "Error";71 }72 }7374 /**//// <summary>75 /// 关闭Word文档76 /// </summary>77 public void Close()78 {79 if (openState == true)80 {81 if (doc != null)82 doc.Close(ref missing, ref missing, ref missing);83 cls.Quit(ref missing, ref missing, ref missing);84 }85 }86} 学习中遇到的问题,求助,关于c#窗口 关于region!!! 急~在线等~关于水晶报表的问题 请问如何绘制线型?比如铁路线。谢谢 在服务器端怎么用for循环加入多个imagebutton控件并加上相应的onclick处理函数 如何用asp.net实现这个? 有关StreamReader的问题? 就10分了 谢谢 求防QQ即时通讯软件实例子或开发原来 SQL Update语句,至少有一个参数未指定值 在数据库中有一个库比如 aa aa中有数据表,如何删除整个aa库 在线等 解决马上给分 怎么会有一个closing的事件 关于下载过程中占用内存大的问题
2public string ReadWord(string fileName, int rowIndex, int colIndex)
3{
4 ApplicationClass cls = null;
5 Document doc = null;
6
7 Table table = null;
8 object missing = Missing.Value;
9
10 object path = fileName;
11 cls = new ApplicationClass();
12
13 try
14 {
15 doc = cls.Documents.Open
16 (ref path, ref missing, ref missing, ref missing,
17 ref missing, ref missing, ref missing, ref missing,
18 ref missing, ref missing, ref missing, ref missing,
19 ref missing, ref missing, ref missing, ref missing);
20 table = doc.Tables[1];
21 string text = table.Cell(rowIndex, colIndex).Range.Text.ToString();
22 text = text.Substring(0, text.Length - 2); //去除尾部的
23 return text;
24 }
25 catch (Exception ex)
26 {
27
28 return ex.Message;
29 }
30 finally
31 {
32 if (doc != null)
33 doc.Close(ref missing, ref missing, ref missing);
34 cls.Quit(ref missing, ref missing, ref missing);
35 }
36}这个方法用于读取Word表格中某个单元格的数据。其中的参数分别为文件名(包括路径),行号,列号。
class WordTableRead
2{
3 private string fileName;
4 private ApplicationClass cls = null;
5 private Document doc = null;
6 private Table table = null;
7 private object missing = Missing.Value;
8 //Word是否处于打开状态
9 private bool openState;
10
11
12 /**//// <summary>
13 /// 自定义构造方法
14 /// </summary>
15 /// <param name="fileName">包含路径的文件名</param>
16 public WordTableRead(string fileName)
17 {
18 this.fileName = fileName;
19 }
20
21 /**//// <summary>
22 /// 打开Word文档
23 /// </summary>
24 public void Open()
25 {
26 object path = fileName;
27 cls = new ApplicationClass();
28 try
29 {
30 doc = cls.Documents.Open
31 (ref path, ref missing, ref missing, ref missing,
32 ref missing, ref missing, ref missing, ref missing,
33 ref missing, ref missing, ref missing, ref missing,
34 ref missing, ref missing, ref missing, ref missing);
35 openState = true;
36 }
37 catch
38 {
39 openState = false;
40 }
41 }
42
43 /**//// <summary>
44 /// 返回指定单元格中的数据
45 /// </summary>
46 /// <param name="tableIndex">表格号</param>
47 /// <param name="rowIndex">行号</param>
48 /// <param name="colIndex">列号</param>
49 /// <returns>单元格中的数据</returns>
50 public string ReadWord(int tableIndex, int rowIndex, int colIndex)
51 {
52 //Give the value to the tow Int32 params.
53
54 try
55 {
56 if (openState == true)
57 {
58 table = doc.Tables[tableIndex];
59 string text = table.Cell(rowIndex, colIndex).Range.Text.ToString();
60 text = text.Substring(0, text.Length - 2); //去除尾部的
61 return text;
62 }
63 else
64 {
65 return "";
66 }
67 }
68 catch
69 {
70 return "Error";
71 }
72 }
73
74 /**//// <summary>
75 /// 关闭Word文档
76 /// </summary>
77 public void Close()
78 {
79 if (openState == true)
80 {
81 if (doc != null)
82 doc.Close(ref missing, ref missing, ref missing);
83 cls.Quit(ref missing, ref missing, ref missing);
84 }
85 }
86}