C# Epplus不读取excel文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14134712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Epplus not reading excel file
提问by chinna_82
Below is my code to read excel file.
下面是我读取excel文件的代码。
Code.
代码。
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");
When i run the code it throw a runtime error.
当我运行代码时,它会引发运行时错误。
Error
错误
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
Appreciated if anyone could advice/help on this. Thanks.
感谢有人可以就此提出建议/帮助。谢谢。
采纳答案by scartag
Epplus does not handle .xls (BIFF8 format) files as far as i know.
据我所知,Epplus 不处理 .xls(BIFF8 格式)文件。
It handles the newer .xlsx (Open Office Xml) format.
它处理较新的 .xlsx(Open Office Xml)格式。
You can use excellibrarythough as it works for xls files.
您可以使用excellibrary,因为它适用于 xls 文件。
回答by Yahya Hussein
In the date of this post EPPLUS (v4.4.1) seems to handle xls files just like it does with xlsx:
在这篇文章的日期 EPPLUS (v4.4.1) 似乎像处理 xlsx 一样处理 xls 文件:
Here is an example:
下面是一个例子:
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();
}
also tested your code:
还测试了您的代码:
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");
and it works without any issues.
它可以正常工作。
回答by Avinash
回答by kplshrm7
Solution If You Wants to use Epplus and your version of Eplus not supports .xlx
解决方案如果您想使用 Epplus 而您的 Eplus 版本不支持 .xlx
Note: If you can convert Your .xlx file into .xlsx you can skip steps below
注意:如果您可以将 .xlx 文件转换为 .xlsx,则可以跳过以下步骤
You can Change File Type..(office 10 or later)
您可以更改文件类型..(office 10 或更高版本)
1> File
2> Save & Send
3> File Types > Change File Type
4> Workbook File Types > Select Workbook
5> Click Save As
After Above steps your file will be Saved as .xlsx format
完成上述步骤后,您的文件将被另存为 .xlsx 格式