我有 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
它会被实例化。我尝试了其他解决方案,但无法弄清楚导致此问题的原因。