c#读取word内容,c#提取word内容
2016-12-21 11:08
441 查看
Post by 54admin, 2009-5-8, Views:575
1:
对项目添加引用,Microsoft Word 11.0 Object Library
2:
在程序中添加 using Word = Microsoft.Office.Interop.Word;
3:
程序中添加
Word.Application app = new Microsoft.Office.Interop.Word.Application(); //可以打开word程序
Word.Document doc = null; //一会要记录word打开的文档
word文档和word程序可不是一回事奥!
4:
一般来说,对于抽取word内容,用的方法很少
public override void openFile(object fileName){} //打开文档
public override object readPar(int i){} //读取word文档的第i段
public override int getParCount(){} //返回word文档一共几段
public override void closeFile(){} //关闭文档
public override void quit(){} //关闭word程序
//从网页上拷贝的目录有时候会出现手动换行符^l,,先将其换成回车段落标记,才能正确读取
public void replaceChar(){}
5:代码
public override void openFile(object fileName)
{
try
{
if (app.Documents.Count > 0)
{
if (MessageBox.Show("已经打开了一个word文档,你想关闭重新打开该文档吗?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
object unknow = Type.Missing;
doc = app.ActiveDocument;
if (MessageBox.Show("你想保存吗?", "保存", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
app.ActiveDocument.Save();
}
app.ActiveDocument.Close(ref unknow, ref unknow, ref unknow);
app.Visible = false;
}
else
{
return;
}
}
}
catch (Exception)
{
//MessageBox.Show("您可能关闭了文档");
app = new Microsoft.Office.Interop.Word.Application();
}
try
{
object unknow = Type.Missing;
app.Visible = true;
doc = app.Documents.Open(ref fileName,
ref unknow, ref unknow, ref unknow, ref unknow, ref unknow,
ref unknow, ref unknow, ref unknow, ref unknow, ref unknow,
ref unknow, ref unknow, ref unknow, ref unknow, ref unknow);
}
catch (Exception ex)
{
MessageBox.Show("出现错误:" + ex.ToString());
}
}
public override object readPar(int i)
{
try
{
string temp = doc.Paragraphs[i].Range.Text.Trim();
return temp;
}
catch (Exception e) {
MessageBox.Show("Error:"+e.ToString());
return null;
}
}
public override int getParCount()
{
return doc.Paragraphs.Count;
}
public override void closeFile()
{
try
{
object unknow = Type.Missing;
object saveChanges = Word.WdSaveOptions.wdPromptToSaveChanges;
app.ActiveDocument.Close(ref saveChanges, ref unknow, ref unknow);
}
catch (Exception ex)
{
MessageBox.Show("Error:" + ex.ToString());
}
}
public override void quit()
{
try
{
object unknow = Type.Missing;
object saveChanges = Word.WdSaveOptions.wdSaveChanges;
app.Quit(ref saveChanges, ref unknow, ref unknow);
}
catch (Exception)
{
}
}
public void replaceChar() {
try
{
object replaceAll = Word.WdReplace.wdReplaceAll;
object missing = Type.Missing;
app.Selection.Find.ClearFormatting();
app.Selection.Find.Text = "^l";
app.Selection.Find.Replacement.ClearFormatting();
app.Selection.Find.Replacement.Text = "^p";
app.Selection.Find.Execute(
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref replaceAll, ref missing, ref missing, ref missing, ref missing);
}
catch (Exception e)
{
MessageBox.Show("文档出现错误,请重新操作");
}
}
6:
刚才是用读取一段做的例子,如果要读取一句或一篇只需要把doc.Paragraphs[i](readPar中)改成 doc.Sentences[i]或doc.content即可,因为都是微软的东东,所以用起来没有一点的障碍,再加上现在的vs2005做的很智能, 所以先从java转到了c#上
7:
实际上,c#中读取word是不用那么麻烦的,但是如果考虑到可能还要抽取txt,ppt等多种格式,所以就写了一个抽象类,调用起来也方便,这就是为什么我的程序方法开头会有override的原因,总要考虑到通用,所以多了一些代码。
1:
对项目添加引用,Microsoft Word 11.0 Object Library
2:
在程序中添加 using Word = Microsoft.Office.Interop.Word;
3:
程序中添加
Word.Application app = new Microsoft.Office.Interop.Word.Application(); //可以打开word程序
Word.Document doc = null; //一会要记录word打开的文档
word文档和word程序可不是一回事奥!
4:
一般来说,对于抽取word内容,用的方法很少
public override void openFile(object fileName){} //打开文档
public override object readPar(int i){} //读取word文档的第i段
public override int getParCount(){} //返回word文档一共几段
public override void closeFile(){} //关闭文档
public override void quit(){} //关闭word程序
//从网页上拷贝的目录有时候会出现手动换行符^l,,先将其换成回车段落标记,才能正确读取
public void replaceChar(){}
5:代码
public override void openFile(object fileName)
{
try
{
if (app.Documents.Count > 0)
{
if (MessageBox.Show("已经打开了一个word文档,你想关闭重新打开该文档吗?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
object unknow = Type.Missing;
doc = app.ActiveDocument;
if (MessageBox.Show("你想保存吗?", "保存", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
app.ActiveDocument.Save();
}
app.ActiveDocument.Close(ref unknow, ref unknow, ref unknow);
app.Visible = false;
}
else
{
return;
}
}
}
catch (Exception)
{
//MessageBox.Show("您可能关闭了文档");
app = new Microsoft.Office.Interop.Word.Application();
}
try
{
object unknow = Type.Missing;
app.Visible = true;
doc = app.Documents.Open(ref fileName,
ref unknow, ref unknow, ref unknow, ref unknow, ref unknow,
ref unknow, ref unknow, ref unknow, ref unknow, ref unknow,
ref unknow, ref unknow, ref unknow, ref unknow, ref unknow);
}
catch (Exception ex)
{
MessageBox.Show("出现错误:" + ex.ToString());
}
}
public override object readPar(int i)
{
try
{
string temp = doc.Paragraphs[i].Range.Text.Trim();
return temp;
}
catch (Exception e) {
MessageBox.Show("Error:"+e.ToString());
return null;
}
}
public override int getParCount()
{
return doc.Paragraphs.Count;
}
public override void closeFile()
{
try
{
object unknow = Type.Missing;
object saveChanges = Word.WdSaveOptions.wdPromptToSaveChanges;
app.ActiveDocument.Close(ref saveChanges, ref unknow, ref unknow);
}
catch (Exception ex)
{
MessageBox.Show("Error:" + ex.ToString());
}
}
public override void quit()
{
try
{
object unknow = Type.Missing;
object saveChanges = Word.WdSaveOptions.wdSaveChanges;
app.Quit(ref saveChanges, ref unknow, ref unknow);
}
catch (Exception)
{
}
}
public void replaceChar() {
try
{
object replaceAll = Word.WdReplace.wdReplaceAll;
object missing = Type.Missing;
app.Selection.Find.ClearFormatting();
app.Selection.Find.Text = "^l";
app.Selection.Find.Replacement.ClearFormatting();
app.Selection.Find.Replacement.Text = "^p";
app.Selection.Find.Execute(
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref replaceAll, ref missing, ref missing, ref missing, ref missing);
}
catch (Exception e)
{
MessageBox.Show("文档出现错误,请重新操作");
}
}
6:
刚才是用读取一段做的例子,如果要读取一句或一篇只需要把doc.Paragraphs[i](readPar中)改成 doc.Sentences[i]或doc.content即可,因为都是微软的东东,所以用起来没有一点的障碍,再加上现在的vs2005做的很智能, 所以先从java转到了c#上
7:
实际上,c#中读取word是不用那么麻烦的,但是如果考虑到可能还要抽取txt,ppt等多种格式,所以就写了一个抽象类,调用起来也方便,这就是为什么我的程序方法开头会有override的原因,总要考虑到通用,所以多了一些代码。
相关文章推荐
- c#读取word内容,c#提取word内容
- c#读取word内容,c#提取word内容
- C#读取word文件内容
- C#读取word文档内容并显示
- c#读取word内容,c#写入word内容,C#操作word
- C#/VB.NET 操作Word批注(二)——如何插入图片、读取、回复Word批注内容
- C#读取Word指定页的内容
- C#读取word文档中的内容
- C#读取Word内容
- 通过C#从数据库中读取内容生成WORD文档
- 能够按页号提取word文档文本内容的小程序,由C#实现
- poi读取word简历(伪word格式的doc文件 )提取文本内容报错
- C#-XML文件提取字符串+字符串存为XML文件+创建XML(自定义节点)文件+读取节点内容
- 能够按页号提取word文档文本内容的小程序,由C#实现
- ASP.NET C#如何读取word,写入word,复制内容到另一个word文档,批量修改文件名
- [转]C#读取Word指定页的内容
- [转]ASP.NET C#如何读取word,写入word,复制内容到另一个word文档,批量修改文件名
- C#读取Word文档内容代码
- C#提取Word中的内容并添加到另一人word中
- C#提取Word中的内容并添加到另一人word中(http://blog.e-works.net.cn/147018/articles/7340.html)