We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

SaveAs corrupt excel file

SaveAs call corrupt excel file, even if i don't change anything. Attached excel error message.

Attachment: message_a1f31c74.rar

3 Replies

UN Unknown Syncfusion Team March 26, 2019 10:37 AM UTC

using (var stream = new FileStream(filepath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
{
                        stream.Position = 0;
                        var Workbook = application.Workbooks.Open(stream);
                        Workbook.SaveAs(stream);
}

Something like this


KC Karthikeyan Chandrasekar Syncfusion Team March 27, 2019 10:09 AM UTC

Hi Maxim, 

This is an usage level issue. In the given code snippet we can see that you are using the same stream to read and write the excel file, so the already opened stream has been overwritten which leads to corruption. (i.e) InputStream object is used to load the excel file and the stream is used for saving the excel file. 

In order to resolve the issue , clear the stream before saving using SetLength() method. Below we have shared the code snippet to resolve this. 

Code snippet : 
stream.Position = 0; 
IWorkbook workbook = application.Workbooks.Open(stream); 
stream.SetLength(0); 
workbook.SaveAs(stream); 
 
Please let us know if this helpful. 

Regards, 
Karthikeyan


ZO ZorroOP April 15, 2021 12:36 PM UTC

Hi Maxim,

 

I downloaded the file and was able to see the screen shot that you have attached. The corruption of the file is due to invalid extension. This happens very often in Excel. Here are the steps which is mostly used to fix it:

 

Method 1 – Change the Default File Format to Save Excel Workbooks

  • In Excel, click on  the ‘Microsoft Office’ button and then ‘Excel Options’.
  • Select the Export option.
  • Tap on Change File Type option.

Method 2 – Use 'Open and Repair' Feature of MS Excel

  • Open the MS Excel.
  • Navigate to File and Select the Open option.
  • Select the corrupt file and choose Open and Repair option.
  • Click Repair to start repairing the file.

You can also read this thread: https://social.technet.microsoft.com/Forums/office/en-US/63020ccc-51d7-46d9-b956-121c0e6efcc8/excel-file-error-the-file-format-and-extension-dont-match?forum=Office2016ITPro 

 


Loader.
Live Chat Icon For mobile
Up arrow icon