2009-04-30 68 views
2

我發現了ExcelPackage,這是一個比Excel Interop API更好的庫,用於創建和維護excel表格,但它們是以.xlsx生成的。大多數會看到這些文件的人只安裝了Office 2003,所以我需要將我的C#代碼中的最終結果轉換爲.xls文件。如何在C#中以編程方式將xlsx文件轉換爲2003 xls文件?

你知道有什麼辦法嗎in C#code

** 更新我試圖使用SaveAs方法,但它不工作,它只是不做任何事情,或返回錯誤0x800A03EC。

+0

請使用'excel-2007'標記而不是'xlsx'。在標記您的問題時,請注意提示的提示:名稱後小於10的任何標籤可能是錯誤的。 – 2009-06-05 15:09:40

回答

1

您可以嘗試使用Microsoft.Office.Interop.Excel。您需要在試圖進行轉換的計算機上安裝Excel。您可以添加COM選項卡中的引用,並使用Microsoft Excel 12.0 Object Library組件。

基本上,您將使用Workbook.Open()打開現有工作簿,創建一個新的Worksheet並複製現有數據。然後您可以使用Workbook.SaveAs()方法,這將允許您在第二個參數中設置文件格式。

+1

但是,這需要您在執行此操作的計算機上安裝Excel - 這可能並非如此。網絡服務器。一個簡單的解決方案 - 但在企業環境中並不真正可行。 – 2009-04-30 13:27:34

5

我懷疑這不會是一個流行的答案,但我不認爲它是可取的將文件轉換爲.xlsx .xlsx(我會建議,這是不必要的,但不幸的是,但不幸的是,這太籠統了)。

「Microsoft Office兼容包」可以免費下載,並且可以將新格式支持添加到Office XP和Office 2003中 - 因此,至少在一般情況下,要更好地說服用戶將他們的系統提升到規範,而不是沉迷於辦公室interop(這基本上會導致你,很可能是你的用戶,很多痛苦)。同樣,我相信支持Open Office 3中的新格式。

我明白,有些情況下人們不會被允許將這種功能添加到他們的系統中,但大多數情況下添加上述工具讓人們的生活更輕鬆,因爲它將減少使用Office 2007的用戶和使用舊版本的用戶之間的摩擦。

+0

是的,我現在放棄了轉換,因爲之前我不知道這個兼容包,謝謝! – 2009-04-30 13:47:31

0

試試這個代碼:

 try 
     { 
      Microsoft.Office.Interop.Word.ApplicationClass oWord = new ApplicationClass(); 
      object oMissing = Type.Missing; 
      object fileName = @"c:\test.docx"; 
      Document oDoc = oWord.Application.Documents.Open(ref fileName, 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, ref oMissing); 
      object fileName2 = @"c:\test2.doc"; 

      object fileFormat = WdSaveFormat.wdFormatDocument97; 
      oDoc.SaveAs(ref fileName2, ref fileFormat, 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.Close(ref oMissing, ref oMissing, ref oMissing); 
      oWord = null; 
      Console.WriteLine("Done"); 

     } 
     catch (Exception ex) 
     { 
      Console.WriteLine(ex.ToString()); 
     } 
     Console.Read(); 
+0

您的代碼編譯正常,但會引發運行時錯誤。也許這是因爲我在IIS上運行它(我有ASP.NET應用程序)。行號4.我的服務器運行的是WinServ 2008 64位 – chester89 2009-08-20 10:48:13

0

這是我與的IBM iSeries項目的一段代碼。它會將任意Excel文件轉換爲Excel 2003:

string MOVE_DOWNLOADED(string FILENAME) 
{ 
    string Path = FILENAME; 
    Microsoft.Office.Interop.Excel.ApplicationClass app = new Microsoft.Office.Interop.Excel.ApplicationClass();   
    Microsoft.Office.Interop.Excel.Workbook workBook = app.Workbooks.Open(Path, 
    0, 
    true, 
    5, 
    "", 
    "", 
    true, 
    Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, 
    "\t", 
    false, 
    false, 
    0, 
    true, 
    1, 
    0); 

    string retval = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\" + "_tmp_" + ".xlsx"; 

    try 
    { 
    workBook.SaveAs(retval, 
     Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel9795, 
     null, 
     null, 
     false, 
     false, 
     Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlShared, 
     false, 
     false, 
     null, 
     null, 
     null); 
    } 
    catch (Exception E) 
    { 
    MessageBox.Show(E.Message); 
    } 

    workBook.Close(null, null, null); 
    System.Runtime.InteropServices.Marshal.ReleaseComObject(workBook); 
    workBook = null; 
    GC.Collect(); // force final cleanup! 

    return retval;  
} 
相關問題