C#操縱Word

首先添加引用,解決方案資源管理器-》引用-》添加-》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的緣由,總要考慮到通用,因此多了一些代碼。
相關文章
相關標籤/搜索