MS Excel 2007 file corrupted after save in .NET

I have a very simple 2007 excel file, after I open it and save with cells.dll, then try to open with MS office 2007, the file seems corrupted and asked me to repair. There is the repair message generated by MS 2007:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>

- <recoveryLog xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<logFileName>error301240_01.xml</logFileName>
<summary>Errors were detected in file 'F:\filestore.xlsx'</summary>
- <additionalInfo>
<info>Excel completed file level validation and repair. Some parts of this workbook may have been repaired or discarded.</info>
</additionalInfo>
</recoveryLog>

My cells.dll version is 4.7.1.8, runtime version = v1.0.3705

There is my test code in c#:

string docFile = "BookGood.xlsx";

System.IO.FileStream fstream = null;
Workbook workbook = new Workbook();

FileFormatType fileFormat = FileFormatType.Excel2007Xlsx;

fstream = new System.IO.FileStream(docFile, FileMode.Open);

workbook.Open(fstream, fileFormat);
workbook.Save(fstream, fileFormat);


fstream.Dispose();

workbook = null;

You can use your excel 2007 file or my uploaded one to verify.

Chang

Please please, we are in production support… CL

Hi,

Thank you for considering Aspose.

Please update your code as mentioned below and check if it works fine.

string docFile = "BookGood.xlsx";

System.IO.FileStream fstream = null;

Workbook workbook = new Workbook();


FileFormatType fileFormat = FileFormatType.Excel2007Xlsx;

fstream = new System.IO.FileStream(docFile, FileMode.Open);

workbook.Open(fstream, fileFormat);

fstream.Dispose();

fstream = new System.IO.FileStream(docFile, FileMode.Open);

workbook.Save(fstream, fileFormat);

fstream.Dispose();

Thank You & Best Regards,

It doesn’t help. This time, MS Office 2007 cannot even repair it. There is an error message when try to open worksheet.xml via winzip:

Extracting to "C:\Documents and Settings\CLIU095\Local Settings\Temp\wz6f5b"
Use Path: yes Overlay Files: yes
Error in file #1: bad Zip file offset (Error local header signature not found): disk #1 offset: 2218

I attached the output file renamed ext to zip for you to look.

I try to open and save using workbook directly, which I think your library handles 2007 excel file correctly. However, I can not use this approach – file will be locked if there is an IO error during open or save. With stream , I am able to close stream more safely.





Chang

Hi,

Thank you for sharing the output file.

We will look into it and get back to you soon.

Thank You & Best Regards,

Hi,

Thank you for considering Aspose.

Well, Aspose.Cells for .NET can handle 2007 excel file correctly and this feature is being used by many customers. It’s strange that you cannot get correct file with the previous code.

Please try the attached simple console project in a clear machine and let us know about the results. This will help us figure out the issue soon.

Thank You & Best Regards,

I think is license code causes problem?

I can run your or my own code without any problem, however, adding license will corrupt file

string AsposeLicenseFile = “Aspose.Total.lic”;
string licFile = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, AsposeLicenseFile); //System.Reflection.Assembly.GetExecutingAssembly().Location, AsposeLicenseFile);

Aspose.Cells.License cellLicense = new Aspose.Cells.License();
cellLicense.SetLicense(licFile);

Is my license expired ? How can it only corrupt 2007 excel file?


Hi,

Thank you for considering Aspose.

Please send us your license file and we will check it soon. Please follow the steps mentioned in the following link to send us the license file.

http://www.aspose.com/corporate/purchase/faqs/send-license-to-aspose-staff.aspx

Thank You & Best Regards,

Hi,

Thank you for considering Aspose.

We have received your license file via email. We checked it and it works fine with the provided sample application. Please provide us further information regarding your system environment (e.g. OS, VS.NET version , 32 bit or 64 bit Machine etc). This will help us figure out the issue soon.

Thank You & Best Regards,

Hi,

Thank you for considering Aspose.

Also, Please make sure that the stream passed to workbook.Save() is a newly created stream. Passing a stream with data to workbook.Save() may corrupt file.

System.IO.FileStream fstream = new System.IO.FileStream(docFile, FileMode.Create); // use FileMode.Create here, please do not use FileMode.Open

workbook.Save(fstream, fileFormat);

fstream.Dispose();

Please feel free to contact us if you still find any problem.

Thank You & Best Regards,

hi,

You are right . It works after I create a new filestream and save workbook object with this new stream. Thanks for the help!

CL