我有jquery,我使用data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8
将我的表导出为xlsx文件。当我想导入该文件(更新)时,我正在通过此功能执行此操作
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
被实例化。我尝试了其他解决方案,无法弄清楚导致此问题的原因。
您可以将HTML放在一个文件中,并为其提供XLSX扩展,Excel可以尽力将其呈现为电子表格,但这不会使该文件成为有效的XLSX文件。
如果要以编程方式读取Excel文件,请确保它是有效的Excel文件。