我想使用 Microsoft.Office.InterOp.Excel 将 .csv 文件转换为 .xlsx c# .net 6

问题描述 投票:0回答:1

希望你一切都好,写在这里寻求你的帮助,我想使用

Microsft.Office.Interop.Excel
从 CSV 生成
.xlsx
文件。但是该程序会生成以下异常:

“System.IO.FileNotFoundException H结果=0x80070002 Mensaje = 无法加载文件或程序集“office,版本=15.0.0.0,文化=中性,PublicKeyToken=71e9bce111e9429c”。 El sistema no puede encontrar el archivo especialado。 Origen = XLSXInterOp Seguimiento de la pila: XLSXInterOp.ConverterX.Conver() C:\path\XLSXInterOp\Program.cs: 第 39 行 XLSXInterOp.Program.Main(String[] args) C:\path\XLSXInterOp\Program.cs: línea 10 ”

我尝试使用命令提示符 gcutil 添加引用,但不起作用。

了解如何解决此问题以生成 xlsx 文件的建议


        public  void ConverterF() 
        {
            var excelApp = new Microsoft.Office.Interop.Excel.Application();
        //Rename the file .csv to .xls(Even if u r going to create the workbook in .xlsx extension)
        string path =@"C:\Escritorio\ExcelFile.csv";
        string xlsFile;
       xlsFile = Path.ChangeExtension(path, ".xls");

        //Create the workbook
        var workbook = excelApp.Workbooks;

            workbook.OpenText("ExcelFile.Csv.xls",
                              DataType: Microsoft.Office.
                Interop.Excel.XlTextParsingType.xlDelimited,
                              ConsecutiveDelimiter: true,
                             Comma: true);

            //Convert To excel
            workbook[1].SaveAs("NewTest.xlsx",     Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookDefault);

            workbook.Close();


        }
c# excel package
1个回答
0
投票

这通常是由以下两种情况之一引起的:

  • 您尝试运行此程序的计算机上未安装 Office/Excel
  • 此程序没有 MS Office dll 所在目录的权限

作为替代解决方案,我强烈建议您查看EPPlus - 它为您提供了一些创建或操作 Excel 文档的好功能,而无需互操作的单线程性或其对实际安装 Excel 的依赖。

© www.soinside.com 2019 - 2024. All rights reserved.