1
using
System;
2
using
System.Collections.Generic;
3
using
System.ComponentModel;
4
using
System.Data;
5
using
System.Drawing;
6
using
System.Text;
7
using
System.Windows.Forms;
8
using
Interop.Word;
9
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
namespace
DataAccessTest
11
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
12
public partial class WordTableRead : Form
13![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
14
public WordTableRead()
15![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
16
InitializeComponent();
17
}
18![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
private void button1_Click(object sender, EventArgs e)
20![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
21
ApplicationClass cls = null;
22
Document doc = null;
23
Interop.Word.Table table = null;
24
object missing = System.Reflection.Missing.Value;
25
int rowIndex = 1, colIndex = 2;
26![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
object path = @"C:"temp3.doc";
28
cls = new ApplicationClass();
29![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
try
31![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
32
doc = cls.Documents.Open(ref path, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
33
table = doc.Tables.Item(1);
34![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
string text = table.Cell(rowIndex, colIndex).Range.Text;
36
this.textBox1.Text = text.Substring(0, text.Length - 1); //去除尾部的mark
37
}
38
catch (Exception ex)
39![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
40
if (ex is System.Runtime.InteropServices.COMException)
41![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
42
MessageBox.Show(((System.Runtime.InteropServices.COMException)(ex)).ErrorCode.ToString());
43
}
44
}
45
finally
46![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
47
if( doc != null ) doc.Close(ref missing, ref missing, ref missing);
48
cls.Quit(ref missing, ref missing, ref missing);
49
}
50
}
51
}
52
}
转载于:https://www.cnblogs.com/hzuIT/articles/1067982.html