C# 操作word文檔

object oFileName = @"C:/Documents and Settings/liush/My Documents/TestDoc.doc";
object oReadOnly = true;
object oMissing = System.Reflection.Missing.Value;

Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;//只是爲了方便觀察
oDoc = oWord.Documents.Open(ref oFileName, ref oMissing, ref oReadOnly, ref oMissing, ref oMissing,
    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

//MessageBox.Show(oDoc.Tables.Count.ToString());
for (int tablePos = 1; tablePos <= oDoc.Tables.Count; tablePos++)
{
    Word.Table nowTable = oDoc.Tables.Item(tablePos);
    string tableMessage = string.Format("第{0}/{1}個表:/n", tablePos, oDoc.Tables.Count);

    for (int rowPos = 1; rowPos <= nowTable.Rows.Count; rowPos++)
    {
for (int columPos = 1; columPos <= nowTable.Columns.Count; columPos++)
{
tableMessage += nowTable.Cell(rowPos, columPos).Range.Text;
tableMessage = tableMessage.Remove(tableMessage.Length - 2, 2);//remove /r/a
tableMessage += "/t";
}

tableMessage += "/n";
    }

    MessageBox.Show(tableMessage);
}

如果看過了上面kaneboy的文章(這是一個系列的之一),再看這段代碼應該不會很難理解。打開一個已有文檔,然後遍歷其中的所有的表。這裏只是簡單的將信息顯示出來,具體實踐上可以對這些信息進行分析。做完這些後,終於找到了一些官方的支持文檔,地址如下:
http://msdn2.microsoft.com/zh-CN/library/y1xatbkd.aspx
其中的word任務有對word各種操作的簡單代碼事例,用vb和c#寫的。看完之後,我想每個人都會明白vb對com的支持比c#不是簡單明瞭一點兩點。(可以看下這個http://blog.joycode.com/kaneboy/archive/2005/08/03/61489.aspx)同樣的代碼,用vb實現打開word文檔的操作,代碼如下:

Dim fileName As String = "C:/Documents and Settings/liush/My Documents/TestDoc.doc"
Dim isReadOnly As Boolean = True

Dim wordApplication As Word.Application = New Word.Application()
Dim wordDocument As Word.Document
wordApplication.Visible = True
wordDocument = wordApplication.Documents.Open(fileName, , isReadOnly)

/***********************************************************************/

在CSDN上總是有網友問這個問題,自己也遇到過,因些寫出來供參考:
症狀:
oWordApplic = New Word.Application
當程序運行到這句時出現下面的錯誤:
檢索 COM 類工廠中 CLSID 爲 {000209FF-0000-0000-C000-000000000046} 的組件時失敗,原因是出現以下錯誤: 80070005。
oWordApplic = New Word.Application
當程序運行到這句時出現下面的錯誤:
檢索 COM 類工廠中 CLSID 爲 {000209FF-0000-0000-C000-000000000046} 的組件時失敗,原因是出現以下錯誤: 80070005。 
解決方法一:
控制面板-》管理工具-》組件服務-》計算機-》我的電腦-》DCom配置-》找到Microsoft Word文檔
之後
單擊屬性打開此應用程序的屬性對話框。 
2. 單擊標識選項卡,然後選擇交互式用戶。
3.單擊"安全"選項卡,分別在"啓動和激活權限"和"訪問權限"組中選中"自定義",然後
自定義->編輯->添加ASP.NET賬戶和IUSER_計算機名



* 這些帳戶僅在計算機上安裝有 IIS 的情況下才存在。
13. 確保允許每個用戶訪問,然後單擊確定。
14. 單擊確定關閉 DCOMCNFG。

解決方法二:
如果上述方法不能解決問題,就應該是權限問題,請嘗試用下面的方法:
在web.config中使用身份模擬,在<system.web>節中加入  <identity impersonate="true" userName="你的用戶名" password="密碼"/>
 </system.web>

/************************************************************/

protected void Page_Load(object sender, EventArgs e)
        {
            Word.Application wdApplication;   //As   Word.Application();  
            Word.Document wdWords;   //As   Word.Document  
    
            wdApplication   =   new   Word.Application();
            Object filename = @"D:/web/image/a.doc";
            object oReadOnly = true;
            object oMissing = System.Reflection.Missing.Value;
            wdWords = wdApplication.Documents.Open(ref filename, ref oMissing, ref oReadOnly, ref oMissing, ref oMissing,
    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

            for (int tablePos = 1; tablePos <= wdWords.Tables.Count; tablePos++)
            {
                Word.Table nowTable = wdWords.Tables.Item(tablePos);
                string tableMessage = string.Format("第{0}/{1}個表:/n", tablePos, wdWords.Tables.Count);

                for (int rowPos = 1; rowPos <= nowTable.Rows.Count; rowPos++)
                {
                    for (int columPos = 1; columPos <= nowTable.Columns.Count; columPos++)
                    {
                        tableMessage += nowTable.Cell(rowPos, columPos).Range.Text;
                        tableMessage = tableMessage.Remove(tableMessage.Length - 2, 2);//remove /r/a
                        tableMessage += "/t";
                    }

                    tableMessage += "/n";
                }

                Response.Write(tableMessage);
            }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章