C#读取Word表格数据
2010-11-24 15:27
513 查看
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); //去除尾部的mark
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
}
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); //去除尾部的mark
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
}
相关文章推荐
- C#读取Word表格中的数据
- C#读取Word表格中的数据
- C#读取Word表格中的数据 (转)
- C#读取Word表格中的数据 (转)
- [原]C# 读取Word 表格数据(单元格纵合并)
- 【20090313-01】C#读取Word表格中的数据 (转)
- C# 读取Word 表格数据(单元格纵合并)
- C#调用NPOI组件读取excel表格数据转为datatable写入word表格中并向word中插入图片/文字/书签 获得书签列表
- 使用C#读取Word表格数据
- C#读取Word表格中数据的完整代码
- C#调用NPOI组件读取excel表格数据转为datatable写入word表格中并向word中插入图片/文字/书签 获得书签列表
- C#读取Word表格中的数据 (转)
- 用C#动态生成Word文档并将数据填入Word表格中
- 读取word文档中表格数据到sqlserver数据库
- POI3.8读取word文档的表格数据!
- 读取word文档中的表格数据到access数据库
- C#实现Word中表格信息读取
- C# 生成数据字典(Word 表格)
- [C#]用C#动态生成Word文档并将数据填入Word表格中
- C#实现Word中表格信息读取