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. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

SaveAs corrupt excel file

Thread ID:

Created:

Updated:

Platform:

Replies:

143545 Mar 26,2019 10:31 AM UTC Mar 27,2019 10:09 AM UTC ASP.NET Core - EJ 2 2
loading
Tags: XlsIO
Maxim Konoplyanik
Asked On March 26, 2019 10:31 AM UTC

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

Attachment: message_a1f31c74.rar

Maxim Konoplyanik
Replied On 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

KarthikeyanC [Syncfusion]
Replied On 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

CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;