要在C#讀寫Excel檔,直接呼叫Excel.exe是最直覺功能最齊全的做法,但Excel屬桌面互動程式,透過Web或排程等背景程序執行常有問題要克服,同時,只為單純讀取資料招喚龐大笨重的Excel程式有殺雞用牛刀之嫌,第三方元件是更理想的方式。

過去用過多套Excel處理程式庫:NPOIEPPlusOpenXML SDKClosedXML,一路下來,ClosedXML支援不少Excel VBA風格的簡潔API,檔案相容性測試又比NPOI及EPPlus好,成為我處理Excel的首選。但有個問題,ClosedXML基於OpenXML SDK,只支援xlsx格式,遇上必須支援xls的場合,只能回歸NPOI。

用慣了ClosedXML,不甘心回頭用NPOI,心生一念,何不先將xls轉成xlsx再用ClosedXML讀取?於是,我展開尋找xls轉xlsx方法的旅程。

  1. 線上轉換服務
    網路有一些線上轉檔服務,例如:ZAMZARConvertio,提供各式檔案格式轉換,xls只是其中之一,這些線上服務有API可供程式整合,免費付費都有。但線上轉換有個致命缺點,上傳內部文件到Internet有資安疑慮,直接領便當。
  2. Office Migration Planning Manager
    OMPM是升級Office 2007/2010的規劃工具,內附一個轉檔工具:Office File Converter (OFC),能將doc、xls、ppt轉成docx、xlsx、pptx格式。使用前必須安裝Microsoft Office相容性套件,但由於原本被拿來批次轉換,ofc.exe不接受參數,要先將檔案放在特定資料夾並修改ofc.ini設定才能轉換。實測我一直卡在"failed to convert"錯誤,轉換失敗。 
    X:\Tools\OMPM\TOOLS>ofc
    Microsoft Office File Converter version 2.2.0.0
    Copyright (c) 2010 Microsoft Corporation.  All rights reserved.

    Automatically converts Office documents to 2010 Microsoft Office system f
    mat based on settings in the OFC.INI control file.

    Converting files from folder E:\ExcelConv\Src
    Converting: E:\ExcelConv\Src\ie_data.xls
    Writing converted file to: E:\ExcelConv\Src\ie_data.xlsx
    Error: E:\ExcelConv\Src\ie_data.xls failed to convert
    Start:  2016-08-05 20:11:13
    End:    2016-08-05 20:11:13
    Total time used to convert files (sec): 0
    Total number of files processed: 1
    Total number of files converted: 0
    Conversion Complete.
  3. Office Binary(doc, xls, ppt) Translator
    b2xtranslator開源專案出自微軟RD,執行時只需一行指令:xls2x.exe Blah.xls就可搞定,比ofc.exe輕巧好用。 但不幸地,雖然很快轉換出xlsx,但檔案格式有錯,無法用Excel開啟。
  4. EXCELCNV.exe
    在Stackoverflow有人提到一件神祕工具-excelcnv.exe,指令如下:
    "x:\Program Files (x86)\Microsoft Office\Office15\excelcnv.exe" -oice test.xls test.xlsx
    但實測也是沒成功,執行還會導致Excel進入安全模式。
  5. 第三方元件
    有不少好用的商業元件,例如:Spire.XLS for .NETExcel Jetcell .NET,評估都很輕巧好用,但目前使用NPOI、ClosedXML已能滿足大部分需求,只為xls、xlsx轉換採購元件,報酬率偏低。

查完一輪無所獲,突發奇想:NPOI同時支援xls及xlsx,那可不可能用NPOI開啟xls再轉存xlsx呢?答案是:可能但不容易,NPOI開啟xls要用HSSFWorkbook物件,xlsx則是XSSFWorkbook,若要轉換必須開兩個物件,再一格一格螞蟻搬家。參考

無功而返。結論:面對C#讀取xls別想太多,還是乖乖用NPOI吧!


Comments

# by 路過的假面騎士

還有 Microsoft.Office.Interop.Excel 也可以轉換

# by Cliff Liu

Free Spire.XLS for .NET https://www.e-iceblue.com/Introduce/free-xls-component.html#.XU0WYegkuCQ https://www.nuget.org/packages/FreeSpire.XLS/ Free version is limited to 5 sheets per workbook and 200 rows per sheet. This limitation is enforced during reading or writing XLS or PDF files. From Free Spire.XLS v7.8, there is no any limitation when loading and saving .xlsx file format. When converting Excel files to PDF files, you can only get the first 3 pages of PDF file. Upgrade to Commercial Edition of Spire.XLS

# by Roger Wiga

My friend suggested me to use ZetExcel Library.

# by Leon

excelcnv.exe 我試是可以用的沒問題,黑執大要不要再試試。

Post a comment