Spire.XLS is a professional Excel API that enables developers to create, manage, manipulate, convert and print Excel worksheets. Get free and professional technical support for Spire.XLS for .NET, Java, Android, C++, Python.

Thu Sep 17, 2015 2:17 pm

Hi,
I am having some difficulty saving a workbook to the Response stream.
I can load the .xlsx from disk and do my updates, then I can wb.SaveToFile("foo.xlsx"), which works fine and reflects the changes that I made.

Tried to write the file to the Response stream, wb.SaveToStream(Response.OutputStream), but no luck.
Tried to simply read in the saved file from disk and stream it to Response but cannot seem to get that to work either.
Might have to do with the encoding. not sure.

Code: Select all
               
Using sr As System.IO.StreamReader = New System.IO.StreamReader("foo.xlsx")
      content = sr.ReadToEnd()
End Using

Then I write the file to the Response stream.

Code: Select all
Response.BinaryWrite(System.Text.Encoding.ASCII.GetBytes(content))

File is saved yet cannot open the .xlsx file.
Error says "The file format and extension don't match. file could be corrupted or unsafe."

I feel that I am close but just cannot seem to get over the hump on this one.

Thanks,
Bill

hoganw
 
Posts: 1
Joined: Wed Sep 16, 2015 8:23 pm

Fri Sep 18, 2015 8:00 am

Hello,

Thanks for your inquiry.
Please try to use the below code.
Code: Select all
Workbook wb=new Workbook();
wb.LoadFromFile(Server.MapPath("5883test.xlsx"));
wb.SaveToStream(Response.OutputStream);
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";             
Response.AddHeader("Content-Disposition", "attachment;filename=5883.xlsx");
Response.End();

If the issue still exists, please offer us your sample document.
It would be helpful to replicate the issue and work out the solution for you ASAP.
If the information is confidential, you can send it to us ( Support@e-iceblue.com ) via email.

Best Regards,
Sweety
E-iceblue support team
User avatar

sweety1
 
Posts: 539
Joined: Wed Mar 11, 2015 1:14 am

Return to Spire.XLS

cron