gpt4 book ai didi

c# - Epplus 不读取 excel 文件

转载 作者:IT王子 更新时间:2023-10-29 04:21:34 24 4
gpt4 key购买 nike

下面是我读取excel文件的代码。

代码。

FileInfo newFile = new FileInfo("C:\\Excel\\SampleStockTakeExceptionReport.xls");
ExcelPackage pck = new ExcelPackage(newFile);
var ws = pck.Workbook.Worksheets.Add("Content");
ws.View.ShowGridLines = false;
ws.Cells["J12"].Value = "Test Write";
pck.Save();
System.Diagnostics.Process.Start("C:\\Excel\\SampleStockTakeExceptionReport.xls");

当我运行代码时,它会抛出一个运行时错误。

错误

System.Exception: Can not open the package. Package is an OLE compound document. If this is an encrypted package, please supply the password ---> System.IO.FileFormatException: File contains corrupted data.
at MS.Internal.IO.Zip.ZipIOEndOfCentralDirectoryBlock.FindPosition(Stream archiveStream)
at MS.Internal.IO.Zip.ZipIOEndOfCentralDirectoryBlock.SeekableLoad(ZipIOBlockManager blockManager)
at MS.Internal.IO.Zip.ZipArchive..ctor(Stream archiveStream, FileMode mode, FileAccess access, Boolean streaming, Boolean ownStream)
at MS.Internal.IO.Zip.ZipArchive.OpenOnStream(Stream stream, FileMode mode, FileAccess access, Boolean streaming)
at System.IO.Packaging.ZipPackage..ctor(Stream s, FileMode mode, FileAccess access, Boolean streaming)
at System.IO.Packaging.Package.Open(Stream stream, FileMode packageMode, FileAccess packageAccess, Boolean streaming)
at System.IO.Packaging.Package.Open(Stream stream, FileMode packageMode, FileAccess packageAccess)
at OfficeOpenXml.ExcelPackage.ConstructNewFile(Stream stream, String password)
--- End of inner exception stack trace ---
at OfficeOpenXml.ExcelPackage.ConstructNewFile(Stream stream, String password)
at OfficeOpenXml.ExcelPackage..ctor(FileInfo newFile)
at Report.Form1.ExportToExcel1(DataTable Tbl, String ExcelFilePath) in C:\SMARTAG_PROJECT\SUREREACH\EXCEL\Report\Report\Form1.cs:line 39

如果有人可以就此提供建议/帮助,我们将不胜感激。谢谢。

最佳答案

据我所知,Epplus 不处理 .xls(BIFF8 格式)文件。

它处理较新的 .xlsx(Open Office Xml)格式。

您可以使用 excellibrary尽管它适用于 xls 文件。

关于c# - Epplus 不读取 excel 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14134712/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com