首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Epplus不读取excel文件

Epplus不读取excel文件
EN

Stack Overflow用户
提问于 2013-01-03 15:27:47
回答 2查看 39.2K关注 0票数 27

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

代码。

代码语言:javascript
复制
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");

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

错误

代码语言:javascript
复制
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

如果有人能在这方面提供建议/帮助,我将不胜感激。谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-01-03 15:32:12

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

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

不过,您也可以使用excellibrary,因为它适用于xls文件。

票数 41
EN

Stack Overflow用户

发布于 2017-12-27 00:12:23

在本文发表之日,EPPLUS (v4.4.1)似乎可以像处理xlsx一样处理xls文件:

下面是一个示例:

代码语言:javascript
复制
  using (var target = new ExcelPackage(new System.IO.FileInfo("D:\\target.xls")))
        {
            target.Workbook.Worksheets.Add("worksheet");
            target.Workbook.Worksheets.Last().Cells["A1:A12"].Value = "Hi";
            target.Save();
        }

我还测试了你的代码:

代码语言:javascript
复制
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");

而且它工作起来没有任何问题。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14134712

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档