2024年3月28日发(作者:)
C# 读取 操做 Word 内容
2009年06月19日 星期五 11:12
导入COM库:Microsoft word 11.0 Object Library.
引用里面就增加了:
创建新Word
object oMissing = ;
Word._Application oWord;
Word._Document oDoc;
oWord = new ation();
e = true;
oDoc = (ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
打开文档:
object oMissing = ;
//object readOnly = true;
Word._Application oWord;
Word._Document oDoc;
oWord = new ation();
e = true;
object fileName = @"E:";
oDoc = (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 = ;
Word._Application oWord;
Word._Document oDoc;
oWord = new ation();
e = true;
object fileName = @"E:";
oDoc = (ref fileName, ref oMissing,
ref oMissing, ref oMissing);
.添加新表
object oMissing = ;
Word._Application oWord;
Word._Document oDoc;
oWord = new ation();
e = true;
oDoc = (ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
object start = 0;
object end = 0;
tableLocation = (ref start, ref end);
(tableLocation, 3, 4, ref oMissing, ref
oMissing);
.表插入行
object oMissing = ;
Word._Application oWord;
Word._Document oDoc;
oWord = new ation();
e = true;
oDoc = (ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
object start = 0;
object end = 0;
tableLocation = (ref start, ref end);
(tableLocation, 3, 4, ref oMissing, ref
oMissing);
newTable = [1];
object beforeRow = [1];
(ref beforeRow);
.单元格合并
object oMissing = ;
Word._Application oWord;
Word._Document oDoc;
oWord = new ation();
e = true;
oDoc = (ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
object start = 0;
object end = 0;
tableLocation = (ref start, ref end);
(tableLocation, 3, 4, ref oMissing, ref
oMissing);
newTable = [1];
object beforeRow = [1];
(ref beforeRow);
cell = (1, 1);
((1, 2));
.单元格分离
object oMissing = ;
Word._Application oWord;
Word._Document oDoc;
oWord = new ation();
e = true;
oDoc = (ref oMissing,
ref oMissing, ref oMissing);
object start = 0;
object end = 0;
tableLocation = (ref start, ref end);
(tableLocation, 3, 4, ref oMissing, ref
oMissing);
newTable = [1];
object beforeRow = [1];
(ref beforeRow);
cell = (1, 1);
((1, 2));
object Rownum = 2;
object Columnnum = 2;
(ref Rownum, ref Columnnum);
通过段落控制插入
object oMissing = ;
object oEndOfDoc = "endofdoc"; /**//* endofdoc is a
predefined bookmark */
//Start Word and create a new document.
Word._Application oWord;
Word._Document oDoc;
oWord = new ation();
e = true;
oDoc = (ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
//Insert a paragraph at the beginning of the document.
aph oPara1;
oPara1 = (ref oMissing);
= "Heading 1";
= 1;
fter = 24; //24 pt spacing after
paragraph.
ParagraphAfter();
读取文字
添加morcosoft word 12.0 object Library引用(低版本的com也可以)
//获得word文件的文本内容
public string Doc2Text(string docFileName)
{
//实例化COM
ationClass wordApp =
new ationClass();
object fileobj = docFileName;
object nullobj = ;
//打开指定文件(不同版本的COM参数个数有差异,一般而言除第
一个外都用nullobj就行了)
nt doc =
(ref fileobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref
nullobj, ref nullobj, ref nullobj
);
//取得doc文件中的文本
string outText = ;
//关闭文件
(ref nullobj, ref nullobj, ref nullobj);
//关闭COM
(ref nullobj, ref nullobj, ref nullobj);
//返回
return outText;
}


发布评论