2013-08-26 33 views
0

我正在使用Microsoft.Office.Interop,使用C#掃描Word和Excel文件,我將文件作爲用戶的輸入,將其保存在本地目錄中,然後掃描它的Interop庫。 它在我的本地發佈上工作正常,但是當我在服務器上發佈它時,它沒有掃描Word或Excel文件。 我在我的服務器上安裝了MS-Office,與我在PC上的版本相同。 我還需要做什麼?我已經在我的bin目錄中複製了所需的DLL。 這是我如何讀字的文件使用Microsoft.Office.Interop Word和Excel

public static string word_to_text(string source) 
{ 
    Word.Application newApp = new Word.Application(); 

    object Source = source; 
    object Unknown = Type.Missing; 

    object miss = System.Reflection.Missing.Value; 
    object readOnly = true; 
    Microsoft.Office.Interop.Word.Document docs = newApp.Documents.Open(ref Source, ref miss, ref readOnly, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss); 
    string totaltext = ""; 
    for (int j = 0; j < docs.Paragraphs.Count; j++) 
    { 
    totaltext += " \r\n " + docs.Paragraphs[j + 1].Range.Text.ToString(); 
    } 
    docs.Close(); 
    newApp.Quit(); 
    return totaltext; 
} 

我得到這個例外

Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 80070005 Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)).

我按照this post 但是現在我有Excel和Word文件的問題固定它。 在Word文件的情況下,它不從上面的代碼讀取文件,docs對象顯示爲空。 在Excel它的情況下顯示異常

Microsoft Office Excel cannot access the file 'myfile path'. There are several possible reasons: • The file name or path does not exist. • The file is being used by another program. • The workbook you are trying to save has the same name as a currently open workbook. 

我讀液得到權利的文件夾,並創建內部SYSTEM32 Here桌面文件夾,我沒有工作這兩種解決方案:( 當我的問題只出現其發佈

+0

您是否收到任何錯誤?我們可以看到一些示例代碼嗎? – ganders

+0

不,我沒有收到任何錯誤 –

+0

錯誤?例外?哪裏? –

回答

1

你不應該在服務器上使用辦公自動化,只有一個線程可以同時訪問Word,它相對於其他的選擇非常緩慢的,並且Word還沒有爲服務器端執行製作。

很多最好使用OpenXML SDK:

http://www.microsoft.com/en-us/download/details.aspx?id=5124

或者一些其他的包裝:

http://docx.codeplex.com/

http://www.codeproject.com/Articles/87711/Manipulate-Docx-with-C-without-Microsoft-Word-inst

如果你覺得你絕對必須使用服務器上辦公自動化(你應該),然後在至少把它放在一個單獨的進程中,並且enque請求到另一個進程。通過這種方式,您可以確保一次只處理一個請求,您可以安全地授予其他進程所需的權限,並且如果Word停止,則可以終止該進程和輔助進程。

+0

查看我的更新後的問題 –

+0

@SyedSalmanRazaZaidi,我看到了您的更新,但我仍堅持強烈建議您不要在服務器上使用Office自動化。改用OpenXML SDK。 –

相關問題