我有一个非常奇怪的问题。我正在尝试使用OpenXML / SpreadsheetLight库来处理用户上传的excel文件的数据(仅限.xlsx而不是.xls),但是在尝试在实时服务器上运行代码时出现错误。
我在本地计算机和登台服务器实例上测试了代码,在这两种情况下我都上传了一个.xlsx文件,代码能够正确处理它。但是,在我们的实时服务器上进行测试并使用完全相同的.xlsx文件时,代码会发出此错误:File contains corrupted data.
到目前为止我尝试过:
此时我不确定还有什么要检查或问题是什么,欢迎提出任何可能的建议。
这是我的错误消息/堆栈跟踪:
File contains corrupted data. : at MS.Internal.IO.Zip.ZipIOExtraFieldZip64Element.ParseDataField(BinaryReader reader, UInt16 size)
at MS.Internal.IO.Zip.ZipIOExtraFieldElement.Parse(BinaryReader reader, ZipIOZip64ExtraFieldUsage zip64extraFieldUsage)
at MS.Internal.IO.Zip.ZipIOExtraField.ParseRecord(BinaryReader reader, ZipIOZip64ExtraFieldUsage zip64extraFieldUsage, UInt16 expectedExtraFieldSize)
at MS.Internal.IO.Zip.ZipIOLocalFileHeader.ParseRecord(BinaryReader reader, Encoding encoding)
at MS.Internal.IO.Zip.ZipIOLocalFileBlock.ParseRecord(BinaryReader reader, String fileName, Int64 position, ZipIOCentralDirectoryBlock centralDir, ZipIOCentralDirectoryFileHeader centralDirFileHeader)
at MS.Internal.IO.Zip.ZipIOBlockManager.LoadLocalFileBlock(String zipFileName)
at MS.Internal.IO.Zip.ZipArchive.GetFile(String zipFileName)
at MS.Internal.IO.Zip.ZipArchive.GetFiles()
at System.IO.Packaging.ZipPackage.ContentTypeHelper..ctor(ZipArchive zipArchive, IgnoredItemHelper ignoredItemHelper)
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 DocumentFormat.OpenXml.Packaging.OpenXmlPackage.OpenCore(Stream stream, Boolean readWriteMode)
at DocumentFormat.OpenXml.Packaging.SpreadsheetDocument.Open(Stream stream, Boolean isEditable, OpenSettings openSettings)
at SpreadsheetLight.SLDocument.OpenExistingSpreadsheet(String SheetNameOnOpen)
at MyProjectName.FileUploader.LoadFileFromExcelFile() in c:\Users\Username\MyProjectName\FileUploader.aspx.cs:line 123
以下是实际代码:
// Make sure a file was uploaded
if (MyFileUpload.HasFile && !string.IsNullOrEmpty(MyFileUpload.FileName))
{
// Make sure the file is a .xlsx
string fileExt = Path.GetExtension(MyFileUpload.FileName);
if (fileExt.ToLower() == ".xlsx")
{
// Upload File to Temp directory
string filePath = string.Empty;
string filename = Path.GetFileName(MyFileUpload.FileName);
string targetFolder = System.IO.Path.GetTempFileName();
filePath = targetFolder + filename;
// Replace any existing file at that location
if (System.IO.File.Exists(filePath))
{
System.IO.File.Delete(filePath);
}
MyFileUpload.SaveAs(filePath);
// Open the file for use
using (SLDocument theSpreadsheet = new SLDocument(filePath, "Sheet1")) // <-- Error occurs here
{
// Do stuff with the file here...
}
}
}
抛出错误的行是这一行:
using (SLDocument theSpreadsheet = new SLDocument(filePath, "Sheet1"))