如何用C#读取Word内容?
将word文档的标题、段落、全篇文章的字数、段数等以及每段的句数、字数等读出并写入数据库?如果解答的非常好,可以追加分。在MSDN上找到了,谢谢大家,给分!...
将word文档的标题、段落、全篇文章的字数、段数等以及每段的句数、字数等读出并写入数据库?如果解答的非常好,可以追加分。
在MSDN上找到了,谢谢大家,给分! 展开
在MSDN上找到了,谢谢大家,给分! 展开
6个回答
展开全部
前提:
导入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(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 Rownum = 2;
object Columnnum = 2;
cell.Split(ref Rownum, ref Columnnum);
通过段落控制插入
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();
导入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(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 Rownum = 2;
object Columnnum = 2;
cell.Split(ref Rownum, ref Columnnum);
通过段落控制插入
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();
展开全部
安装个vsto2005,去网上找找 关于vsto的文章,
然后弄清楚docunment,application,selection,range这几个类的概念.
你的问题就可以解决了.
如果e文够好,推荐你找下《VSTO 2005编程指南》这本书,讲得很透彻的.
然后弄清楚docunment,application,selection,range这几个类的概念.
你的问题就可以解决了.
如果e文够好,推荐你找下《VSTO 2005编程指南》这本书,讲得很透彻的.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你好!
1、对于TXT文件,用什么语言都不能实现任意10条内容,只能从头开始读取;但可能有变通的办法:先产生一个随机数,打开TXT文件,从头开始读取数据,当已读取的数据=随机数时,开始将读取的数据放到变量中,连续存放10个;
2、用OFFICE的WORD文档控件的DLL链接到VB中,在VB中创建一个WORD文档工作平台(空间),将10个数据写入到该文档中,文档取名为www.DOC,最后保存该文档;
3、当然,若你对WORD文档控件的DLL用法不太了解的话,请参考MSDN或网络上查找,这里就不给出代码了,不好意思!
祝你成功!
1、对于TXT文件,用什么语言都不能实现任意10条内容,只能从头开始读取;但可能有变通的办法:先产生一个随机数,打开TXT文件,从头开始读取数据,当已读取的数据=随机数时,开始将读取的数据放到变量中,连续存放10个;
2、用OFFICE的WORD文档控件的DLL链接到VB中,在VB中创建一个WORD文档工作平台(空间),将10个数据写入到该文档中,文档取名为www.DOC,最后保存该文档;
3、当然,若你对WORD文档控件的DLL用法不太了解的话,请参考MSDN或网络上查找,这里就不给出代码了,不好意思!
祝你成功!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
c# 读取word文件文本内容
这里有实例代码,是office 2007的,office 2003 的word open函数里面的好像少几个,不记得了,自己试试就出来了。
http://blog.e-works.net.cn/147018/articles/7340.html
这里有实例代码,是office 2007的,office 2003 的word open函数里面的好像少几个,不记得了,自己试试就出来了。
http://blog.e-works.net.cn/147018/articles/7340.html
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
新建项目->Visual C#->Office->Word
可以建一个使用 VSTO 的项目,你可以看里面是如何访问 Office Word 的。
前提是必须安装 : Office 2003 或 Office 2007
可以建一个使用 VSTO 的项目,你可以看里面是如何访问 Office Word 的。
前提是必须安装 : Office 2003 或 Office 2007
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
关注
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询