1.关于 Interop.Word类库
今天练习C#操作Word文档,首先要引入类库(Microsoft.Office.Interop.Word ),可是在我机器上无法引用,于是找其原因是没有安装“Office 2003 主 Interop 程序集”.
安装方法(网上介绍的):运行office2003安装程序,选择“添加或删除组件”,->勾选“高级自定义应用程序”,->展开特定于应用程序的节点。要获取 Microsoft Office word 2003 PIA,请展开 Microsoft Office word 节点并选择 .NET Programmability Support(.net可编程支持) 。单击 .NET Programmability Support (.net可编程支持) 旁边的下拉箭头以选择更新选项,并选取 Run from My Computer(从本机运行)。然后点击“更新”按钮。完成!
可是问题来了: 我下载的这个office2003(盗版)安装程序打开后没有.NET Programmability Support (.net可编程支持)这个选项
不能就此罢手,添加引用->COM->找到microosft word 11.0 object Library,选择,确定.
引用:
using Word;
运行程序通过,搞定.
【摘自】 http://tmsoft.lsxy.com/trackback.php?tbID=334&extra=2aa67d 周老师科研站
前提: 导入COM库:Microsoft word 11.0 Object Library.
引用里面就增加了:
创建新Word
object
oMissing
=
System.Reflection.Missing.Value; Word._Application oWord; Word._Document oDoc; oWord
=
new
Word.Application(); oWord.Visible
=
true
; oDoc
=
oWord.Documents.Add(
ref
oMissing,
ref
oMissing,
ref
oMissing,
ref
oMissing);
打开文档:
object
oMissing
=
System.Reflection.Missing.Value; Word._Application oWord; Word._Document oDoc; oWord
=
new
Word.Application(); oWord.Visible
=
true
;
object
fileName
=
@"
E:CCCXCXXTestDoc.doc
"
; oDoc
=
oWord.Documents.Open(
ref
fileName,
ref
oMissing,
ref
oMissing,
ref
oMissing,
ref
oMissing,
ref
oMissing,
ref
oMissing,
ref
oMissing,
ref
oMissing,
ref
oMissing,
ref
oMissing,
ref
oMissing,
ref
oMissing,
ref
oMissing,
ref
oMissing,
ref
oMissing);
导入模板
object
oMissing
=
System.Reflection.Missing.Value; Word._Application oWord; Word._Document oDoc; oWord
=
new
Word.Application(); oWord.Visible
=
true
;
object
fileName
=
@"
E:XXXCCXTest.doc
"
; oDoc
=
oWord.Documents.Add(
ref
fileName,
ref
oMissing,
ref
oMissing,
ref
oMissing);
.添加新表
object
oMissing
=
System.Reflection.Missing.Value; Word._Application oWord; Word._Document oDoc; oWord
=
new
Word.Application(); oWord.Visible
=
true
; oDoc
=
oWord.Documents.Add(
ref
oMissing,
ref
oMissing,
ref
oMissing,
ref
oMissing);
object
start
=
0
;
object
end
=
0
; Word.Range tableLocation
=
oDoc.Range(
ref
start,
ref
end); oDoc.Tables.Add(tableLocation,
3
,
4
,
ref
oMissing,
ref
oMissing);
.表插入行
object
oMissing
=
System.Reflection.Missing.Value; Word._Application oWord; Word._Document oDoc; oWord
=
new
Word.Application(); oWord.Visible
=
true
; oDoc
=
oWord.Documents.Add(
ref
oMissing,
ref
oMissing,
ref
oMissing,
ref
oMissing);
object
start
=
0
;
object
end
=
0
; Word.Range tableLocation
=
oDoc.Range(
ref
start,
ref
end); oDoc.Tables.Add(tableLocation,
3
,
4
,
ref
oMissing,
ref
oMissing); Word.Table newTable
=
oDoc.Tables[
1
];
object
beforeRow
=
newTable.Rows[
1
]; newTable.Rows.Add(
ref
beforeRow);
.单元格合并
object
oMissing
=
System.Reflection.Missing.Value; Word._Application oWord; Word._Document oDoc; oWord
=
new
Word.Application(); oWord.Visible
=
true
; oDoc
=
oWord.Documents.Add(
ref
oMissing,
ref
oMissing,
ref
oMissing,
ref
oMissing);
object
start
=
0
;
object
end
=
0
; Word.Range tableLocation
=
oDoc.Range(
ref
start,
ref
end); oDoc.Tables.Add(tableLocation,
3
,
4
,
ref
oMissing,
ref
oMissing); Word.Table newTable
=
oDoc.Tables[
1
];
object
beforeRow
=
newTable.Rows[
1
]; newTable.Rows.Add(
ref
beforeRow); Word.Cell cell
=
newTable.Cell(
1
,
1
); cell.Merge(newTable.Cell(
1
,
2
));
.单元格分离
object
oMissing
=
System.Reflection.Missing.Value; Word._Application oWord; Word._Document oDoc; oWord
=
new
Word.Application(); oWord.Visible
=
true
; oDoc
=
oWord.Documents.Add(
通过段落控制插入
object
oMissing
=
System.Reflection.Missing.Value;
object
oEndOfDoc
=
"
\endofdoc
"
;
/* endofdoc is a predefined bookmark */
//
Start Word and create a new document.
Word._Application oWord; Word._Document oDoc; oWord
=
new
Word.Application(); oWord.Visible
=
true
; oDoc
=
oWord.Documents.Add(
ref
oMissing,
ref
oMissing,
ref
oMissing,
ref
oMissing);
//
Insert a paragraph at the beginning of the document.
Word.Paragraph oPara1; oPara1
=
oDoc.Content.Paragraphs.Add(
ref
oMissing); oPara1.Range.Text
=
"
Heading 1
"
; oPara1.Range.Font.Bold
=
1
; oPara1.Format.SpaceAfter
=
24
;
//
24 pt spacing after paragraph.
oPara1.Range.InsertParagraphAfter();
C#读取Word表格中的数据
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
10
namespace
DataAccessTest
11
{ 12 public partial class WordTableRead : Form 13 { 14 public WordTableRead() 15 { 16 InitializeComponent(); 17 } 18 19 private void button1_Click( object sender, EventArgs e) 20 { 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 27 object path = @" C:\temp3.doc " ; 28 cls = new ApplicationClass(); 29 30 try 31 { 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 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 { 40 if (ex is System.Runtime.InteropServices.COMException) 41 { 42 MessageBox.Show(((System.Runtime.InteropServices.COMException)(ex)).ErrorCode.ToString()); 43 } 44 } 45 finally 46 { 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 }
C#读取Word表格数据
1 // 将读取Word表格封装与方法中。 2 public 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 ); // 去除尾部的mark 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表格中某个单元格的数据。其中的参数分别为文件名(包括路径),行号,列号。
由于考虑到代码复用,我将代码写成了一个类。此外,通过审视代码可以发现,如果要多次读取同一文件中的不同的单元格数据会造成频繁的打开、关闭Word程序;因此,我将代码进行优化。在我做优化的时候突然想起来ADO.NET的SqlConnection和SqlCommand类。这两个类我常常用做数据库操作,一般用到的方法顺序都是:打开数据库连接,执行数据库查询,关闭数据库连接。我没有使用到两个类,我将这段代码封装于一个类中。使用Open、Close控制Word文档的打开和关闭,使用WordTableRead方法读取表格中的数据。这样对于读取多个单元格中的数据,每次只需要打开、关闭一次Word程序即可,大大的节约了资源的开销和节省了时间,提高的读取效率。此外,对于代码的优化还有可以读取指定表格中数据。下图显示了这个类的结构,代码及相应注释附在图的下方:
1 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 }