简体   繁体   English

将 .xlsx 文件转换为 .xls(97-2003) 工作簿 C#

[英]convert .xlsx file to .xls(97-2003) workbook C#

i am trying to convert .xlsx file to .xls(97-2003) workbook.我正在尝试将 .xlsx 文件转换为 .xls(97-2003) 工作簿。 which might have around 1000 rows n more .其中可能有大约 1000 行 n 多。 is there any nuget package that i can use which is free?有没有我可以免费使用的 nuget 包? i have tried using Microsoft.Office.Interop.Excel but it didnt work out on IIS.我曾尝试使用 Microsoft.Office.Interop.Excel,但它在 IIS 上不起作用。

    string sourceFileName = appSettings.ImageDirectory + "Report.xlsx";
    string DestFileName = appSettings.ImageDirectory + "Report.xls";
    object oMissing = Type.Missing;
    var app = new Microsoft.Office.Interop.Excel.Application();
    var wb = app.Workbooks.Open(sourceFileName, oMissing, oMissing,
                                    oMissing, oMissing, oMissing, oMissing, 
    oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, 
    oMissing);
    wb.Application.DisplayAlerts = false;
    wb.SaveAs(DestFileName , 
    Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel8, Type.Missing, 
    Type.Missing, Type.Missing, Type.Missing, 
    Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, 
    Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
    app.Quit();
    string fullPath = appSettings.ReportFolderPath + "/Report.xls"; 
    return Json(new { path = fullPath, errorMessage = "" });

As far as I know, you couldn't use Microsoft.Office.Interop.Excel on a server where Excel is not installed.据我所知,您无法在未安装 Excel 的服务器上使用Microsoft.Office.Interop.Excel

Microsoft.Office.Interop.Excel.dll is just a translator (called an Interop Assembly) to help you talk to Excel. Microsoft.Office.Interop.Excel.dll 只是一个翻译器(称为互操作程序集),可帮助您与 Excel 对话。

You could try to use ClosedXML to achieve your requirement.您可以尝试使用ClosedXML来实现您的要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM