-1

我有 jquery,我将表导出为 xlsx 文件,使用data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8. 当我想导入该文件(更新)时,我正在通过此功能执行此操作

public static Dictionary<string, string> GetTableFromData(string data, string root)
    {
        if (string.IsNullOrEmpty(data))
        {
            return null;
        }
        var myString = data.Split(new char[] { ',' });
        byte[] bytes = Convert.FromBase64String(myString[1]);
        string fileName = Guid.NewGuid().ToString() + ".xlsx";
        string path = Path.Combine(root, "excel", fileName);
        using (Stream file = File.Create(path))
        {
            file.Write(bytes, 0, bytes.Length);
        }
        FileInfo fileInfo = new FileInfo(path);

        using (ExcelPackage package = new ExcelPackage(fileInfo))
        {
            ExcelWorksheet workSheet = package.Workbook.Worksheets[$"{fileName}"];
            int totalRows = workSheet.Dimension.Rows;

            Dictionary<string, string> languageDictionaries = new Dictionary<string, string>();

            for (int i = 1; i <= totalRows; i++)
            {
                languageDictionaries.Add(workSheet.Cells[i, 1].Value.ToString(), workSheet.Cells[i, 2].Value.ToString());
            }

            return languageDictionaries;
        }
    }

而 ExcelPackage 总是抛出我和错误说'The file is not an valid Package file. If the file is encrypted, please supply the password in the constructor

我尝试在 excel 中制作一些测试 xlsx 文件,当我导入该测试文件时,package它会被实例化。我尝试了其他解决方案,但无法弄清楚导致此问题的原因。

4

1 回答 1

2

您可以将 HTML 放入文件中并为其提供 XLSX 扩展名,Excel 可以尽最大努力将其呈现为电子表格,但这不会使该文件成为有效的 XLSX 文件。

如果您想以编程方式读取 Excel 文件,请确保它是有效的 Excel 文件。

于 2019-04-04T09:27:15.023 回答