博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#操纵Word
阅读量:5247 次
发布时间:2019-06-14

本文共 4270 字,大约阅读时间需要 14 分钟。

首先添加引用,解决方案资源管理器-》引用-》添加-》Com-》浏览-》C:\Program Files\Microsoft Office\OFFICE11\MSWORD.OLB    我使用的是office 2003其他版本我不太清楚,.net会自动把OLB控件转换成DLL文件
使用方法:
  object oMissing = System.Reflection.Missing.Value;
   Word.Application oWord =new Word.Application();
  oWord.Visible = false;//设置Word应用程序为不可见
//新建一个Word文档
   Word.Document oDoc=oWord.Documents.Add(ref oMissing,ref oMissing ,ref oMissing,ref oMissing);   
//文档内容的复制与粘贴
    oDoc.Content.Copy();
    oDoc.Content.Paste()
//文档的另存为
oDoc.SaveAs(ref fileName,ref saveFormat,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);
//其他设置
   oDoc.PageSetup.PaperSize=Word.WdPaperSize.wdPaperA3;//页面设置
   oDoc.PageSetup.Orientation=Word.WdOrientation.wdOrientLandscape;//横板还是竖板
   oDoc.PageSetup.TextColumns.SetCount(2);//分栏
//关闭Word
   oWord.Application.Quit(ref b,ref oMissing,ref oMissing);
   System.Runtime.InteropServices.Marshal.ReleaseComObject(oWord);
通过oDoc对象对Word文档进行操作(word能做的它都能做)进行操作里面有很多函数,有兴趣的自己研究
 
 
另一方法:
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的原因,总要考虑到通用,所以多了一些代码。

转载于:https://www.cnblogs.com/chenbg2001/archive/2011/05/04/2037071.html

你可能感兴趣的文章
go:channel(未完)
查看>>
[JS]递归对象或数组
查看>>
CSS与Theme的作用——Asp.Net
查看>>
LeetCode(17) - Letter Combinations of a Phone Number
查看>>
20165115 2017-2018-2 《Java程序设计》第四周学习总结
查看>>
Linux查找命令对比(find、locate、whereis、which、type、grep)
查看>>
WPF自定义集合控件概述与遇到的问题
查看>>
路由器外接硬盘做nas可行吗?
查看>>
python:从迭代器,到生成器,再到协程的示例代码
查看>>
pytest的参数化测试
查看>>
Java多线程系列——原子类的实现(CAS算法)
查看>>
安装Pygame和pip的艰辛之路
查看>>
Hibernate的实体类为什么需要实现 java.io.Serializable 接口
查看>>
在Ubuntu下配置Apache多域名服务器
查看>>
多线程《三》进程与线程的区别
查看>>
Min Stack
查看>>
老鸟的Python入门教程
查看>>
Ubuntu下非常给力的下载工具--uget+aria2
查看>>
Nginx配置
查看>>
棋盘覆盖问题
查看>>