I'm currently having an issue with Spire.Xls. My code is currently just opening a file, and checking that it doesn't have any issues. As the system I'm working on, we don't have control of the files that we are manipulating with the Spire library. The code is multithreaded, with threading logic being handled outside of the code block below. The code creates a seperate thread for the processing of a list of Excel files using Spire.XLS, sequencially. The other threads are used to process Word and PowerPoint files.
What is currently happening, is that when I stop the application it throws a null reference exception (see below). It also comes up with the following message "Your app has entered a break state, but there is no code to show because all threads were executing external code (typically system or framework code)". I've been unable to track down the cause for this as processing of all the spreadsheet processing looks to have already completed and the exception and information provdied isn't that helpful. Looking at the exception and the extra information I found, it looks like the issue is actually in he Spire.XLS library. What I think is happening is that the workbook is loosing the worksheet objects, and when dispose is called it errors as there is an assumption that the workbook has worksheets.
As you can see from my code I've tried a few solutions already:
* Disposing the weeksheet objects
* Switching from LoadFromFile to LoadFromStream
* using a "Using" statement
* Disposing object after an manually when an exception occurs.
None of these solutions seem to have worked. I also have not been able to isolate a file as the cause. Try..Catch blocks appear to have no effect on handling this issue.
Are you able to provide any help, suggestions on how to prevent this?
Library version: 7.9.45.4040 (Evaluation)
Exception:
System.NullReferenceException was unhandled
Message: An unhandled exception of type 'System.NullReferenceException' occurred in Spire.XLS.dll
Additional information: Object reference not set to an instance of an object.
Extra info I was able to get:
Unhandled Exception: System.NullReferenceException: Object reference not set to
an instance of an object.
at spr8000.1(XlsWorksheet A_0, Dictionary`2 A_1)
at Spire.Xls.Core.Spreadsheet.XlsWorksheet.ParseData(Dictionary`2 dictUpdated
SSTIndexes)
at Spire.Xls.Core.Spreadsheet.XlsWorksheetBase.ParseData()
at Spire.Xls.Core.Spreadsheet.XlsWorksheet.40()
at Spire.Xls.Core.Spreadsheet.XlsWorkbook.ClearAll()
at Spire.Xls.Core.Spreadsheet.XlsWorkbook.DisposeAll()
at Spire.Xls.Core.Spreadsheet.XlsWorkbook.Close(Boolean SaveChanges, String F
ilename)
at Spire.Xls.Core.Spreadsheet.XlsWorkbook.Close(Boolean saveChanges)
at Spire.Xls.Core.Spreadsheet.XlsWorkbook.Close()
at Spire.Xls.Core.Spreadsheet.XlsWorkbook.Finalize()
Code:
- Code: Select all
public OfficeFileValidationStatus Validate(string filePath)
{
OfficeFileValidationStatus fileStatus = OfficeFileValidationStatus.Pass;
Workbook workbook = null;
try
{
//using (workbook = new Workbook())
//{
FileStream x = new FileStream(filePath, FileMode.Open);
workbook = new Workbook();
//workbook.LoadFromFile(filePath);
workbook.LoadFromStream(x);
if (workbook == null || workbook.Worksheets == null)
{
fileStatus = OfficeFileValidationStatus.OtherError;
}
else
{
if (workbook.IsWindowProtection)
{
fileStatus = OfficeFileValidationStatus.IsNonEditable;
}
foreach (Worksheet sheet in workbook.Worksheets)
{
if (sheet.ProtectContents || sheet.ProtectScenarios)
{
fileStatus = OfficeFileValidationStatus.IsNonEditable;
//sheet.Dispose();
break;
}
//sheet.Dispose();
}
}
//}
}
catch (Exception ex)
{
if (ex.Message.Contains("Workbook is protected and password wasn't specified.")
|| ex.Message.Contains("Invalid password."))
{
fileStatus = OfficeFileValidationStatus.PasswordRequired;
}
else if (ex.Message.Contains("Zip exception.Can't find local header signature - wrong file format or file is corrupt.")
|| ex.Message.Contains("Maximum number of extended formats exceeded.")
|| ex.Message.Contains("Cannot read that as a ZipFile")
|| ex.Message.Contains("Zip exception.Wrong Crc value.")
|| ex.Message.Contains("File does not contain workbook stream"))
{
fileStatus = OfficeFileValidationStatus.Corrupt;
}
else
{
fileStatus = OfficeFileValidationStatus.OtherError;
}
}
finally
{
//if(workbook != null)
//{
// workbook.Dispose();
//}
try
{
var x = workbook.Worksheets[0];
}
catch
{
workbook = new Workbook();
}
workbook = null;
}
return fileStatus;
}