【问题标题】:C# Convert Excel 2007 (xlsx) file into Excel 2003 (xls) fileC# 将 Excel 2007 (xlsx) 文件转换为 Excel 2003 (xls) 文件
【发布时间】:2013-03-21 13:17:57
【问题描述】:

我正在开发一个控制台应用程序,它将 xlsx 文件转换为 xls 文件。我不想将它从 xlsx 重命名为 xls,因为它将在 excel 2007 中打开,但它会在 excel 2003 中显示为损坏的文件。寻找一种加载文档的方法,然后将其保存为 xls 格式.

我当前的代码只是将 xlsx 重命名为 xls

string fileName = @"C:\Users\L-3\Desktop\my.xlsx";
string svfileName = @"C:\Users\L-3\Desktop\ssc\my1.xls";
object oMissing = Type.Missing;
var app = new Microsoft.Office.Interop.Excel.Application();
var wb = app.Workbooks.Open(fileName, oMissing, oMissing,
                oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
wb.SaveAs(svfileName, XlFileFormat.xlOpenXMLTemplate, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
app.Quit();

【问题讨论】:

    标签: c# excel interop excel-2007


    【解决方案1】:

    你的枚举是错误的,而不是 XlFileFormat.xlOpenXMLTemplate 你想要 XlFileFormat.xlExcel8,所以你的代码会是这样的:

    string fileName = @"C:\Users\L-3\Desktop\my.xlsx";
    string svfileName = @"C:\Users\L-3\Desktop\ssc\my1.xls";
    object oMissing = Type.Missing;
    var app = new Microsoft.Office.Interop.Excel.Application();
    var wb = app.Workbooks.Open(fileName, oMissing, oMissing,
                    oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
    wb.SaveAs(svfileName, XlFileFormat.xlExcel8, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
    app.Quit();
    

    更多信息here.

    【讨论】:

    • 这会引发异常:Exception from HRESULT: 0x800A03EC COMEXception 未处理。它对你有用吗?
    • 我认为 xlExcel8 造成了问题。
    • 我只是花时间测试了上面的代码,它在我的电脑上运行良好。
    • 由于某种原因 xlExcel8 不适合我。我正在使用确切的代码,它会引发 COM 异常。顺便说一句,我添加了 Microsoft.Office.Interop.Excel v 12.0.0.0 (2007) 会有什么不同吗?
    • xlWorkbookNormal 怎么样?它工作得很好,我猜它甚至会保留 Excel 2003 + 的功能。
    【解决方案2】:

    尝试使用XlFileFormat.xlExcel9795 保存。你也可以使用XlFileFormat.xlWorkbookNormal

    【讨论】:

    • 这会将文件保存为 Excel 95-97 格式,如果您使用 >= 2003 引入的任何功能,它们将不起作用!
    • 抛出异常:“HRESULT 中的异常:0x800A03EC”
    【解决方案3】:

    我从here找到了以下sn-p:

    // Create new XLSX file.
    var xlsxFile = new ExcelFile();
    
    // Load data from XLSX file.
    xlsxFile.LoadXlsx(fileName + ".xls", XlsxOptions.PreserveMakeCopy);
    
    // Save XLSX file to XLS file.
    xlsxFile.SaveXls(fileName + ".xls");
    

    它使用this component

    【讨论】:

    • 这将在我的应用程序中添加依赖项。 o.O
    【解决方案4】:

    我认为这就是您要寻找的答案:

    What is the correct `XlFileFormat` enumeration for Excel 97-2003

    您正在使用文件格式xlOpenXMLTemplate。尝试改用xlExcel8?那应该是 97-2003 Workbooks 的文件格式。

    【讨论】:

    • 它抛出了一个 com 异常。你能创建一个控制台并再次检查吗?
    • 很遗憾,我无法做到这一点。我注意到@JMK 说这是有效的。我确定您之前使用的文件格式 xlOpenXMLTemplate 不正确。 SaveAs 方法应该为 Excel 97-2003 工作簿 (.xls) 使用正确的文件格式枚举,它应该是 xlExcel8。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-11
    • 2011-07-18
    • 1970-01-01
    • 1970-01-01
    • 2018-05-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多