C#操縱Word

來源:互聯網
上載者:User
首先添加引用,方案總管-》引用-》添加-》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的原因,總要考慮到通用,所以多了一些代碼。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.