最快和最簡單的解決方法是使用你已經取得了各項產品。
只需使用Excel Interop進行保存即可將xlsb轉換爲xlsx,然後照常讀取新文件。通常我不會推薦使用Excel Interop,因爲它非常慢,但對於只是轉換文件而言,它足夠快(假設條件正常)。
我建議您儘可能使用Office Open XML SDK來讀取生成的xml文件。
這是一個我早些時候:
public class XlConversion
{
public static void MarshalReleaseComObject(object comObject)
{
if ((comObject != null) && (Marshal.IsComObject(comObject)))
{
Marshal.ReleaseComObject(comObject);
}
}
public static void ConvertTsvToExcel(string inputFullPath, string outputExcelFullPath, bool deleteInput = false)
{
if (String.IsNullOrWhiteSpace(inputFullPath))
{
throw new ArgumentOutOfRangeException(nameof(inputFullPath));
}
if (String.IsNullOrWhiteSpace(outputExcelFullPath))
{
throw new ArgumentOutOfRangeException(nameof(outputExcelFullPath));
}
var inputFilename = new FileInfo(inputFullPath);
var xlFilename = new FileInfo(outputExcelFullPath);
const int maxSupportedXlFilenameLength = 218;
if (xlFilename.FullName.Length > maxSupportedXlFilenameLength)
{
throw new ArgumentOutOfRangeException(nameof(outputExcelFullPath), outputExcelFullPath, ("The full path filename (" + xlFilename.FullName.Length + " characters) is longer than Microsoft Excel supports (" + maxSupportedXlFilenameLength + " characters)"));
}
var excelApp = new Application();
Workbooks wbs = excelApp.Workbooks;
Workbook wb = wbs.Open(inputFilename.FullName);
wb.SaveAs(xlFilename.FullName, XlFileFormat.xlOpenXMLWorkbook);
try
{
wb.Close();
//excel.Quit();
}
finally
{
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
MarshalReleaseComObject(wb);
MarshalReleaseComObject(wbs);
MarshalReleaseComObject(excelApp);
}
if (deleteInput)
{
File.Delete(inputFilename.FullName);
}
}
}
它應該是一個評論,沒有回答。 – 2015-02-08 20:13:01
儘管此鏈接可能回答此問題,但最好在此處包含答案的重要部分,並提供供參考的鏈接。如果鏈接頁面更改,則僅鏈接答案可能會失效。 – PiotrWolkowski 2015-02-08 21:09:45
非常感謝informatin Rob。我不太確定客戶是否會選擇全新的解決方案,但我會仔細研究一下。 – 2015-02-08 21:22:33