We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Aspose.Cells (8.7.2) : Excel file download not working via HttpResponse

Hi There,

Below is my code to update an excel file and then download it through the browser but it doesn’t seem to work and no file is downloaded. I am trying to update an existing excel file specified at a location on the disk and then download it with updated values.

FileStream stream = new FileStream(filePath, FileMode.Open);

Workbook workbook = new Workbook(stream);

Worksheet worksheet = workbook.Worksheets.FirstOrDefault(x => x.Name.Contains(fileName));

List objects = new List;

worksheet.Cells.ImportCustomObjects(objects, objectProperties, false, 1, 0,
objects.Count, true, “dd/mm/yyyy”, false);

workbook.Worksheets[worksheet.Name].AutoFitColumns();

workbook.Save(Response, “Test.xls”, ContentDisposition.Attachment, new OoxmlSaveOptions(SaveFormat.Excel97To2003));

Response.End();
stream.Close();

Am I missing something? Any help would be appreciated.

Hi,

Thanks for your posting and using Aspose.Cells.

For XLS format, please use XlsSaveOptions and for XLSX format, please use OoxmlSaveOptions.

Please modify this line of code

workbook.Save(Response, “Test.xls”, ContentDisposition.Attachment, new OoxmlSaveOptions(SaveFormat.Excel97To2003));

into this

workbook.Save(Response, “Test.xls”, ContentDisposition.Attachment, new XlsSaveOptions(SaveFormat.Excel97To2003));

Please also check the attached web application project that illustrates this issue.

Hi Shakeel,

I tried the way you mentioned but it still doesn’t work. I don’t get any notification in the browser that a file is being downloaded nor do I see any files in the downloads folder. I can see that the workbook has been filled with data.

Tried the below methods:
workbook.Save(Response, workbook.FileName + “.xls”, ContentDisposition.Inline, new XlsSaveOptions(SaveFormat.Excel97To2003));

workbook.Save(Response, workbook.FileName + “.xls”, ContentDisposition.Attachment, new XlsSaveOptions(SaveFormat.Excel97To2003));

Response.End();


Is there another way to implement this?

Hi,


Well, your code is right and it should work fine without any problem. Please check if you got sufficient permissions to do System.IO tasks (e.g write files), please try with Administartor permission on your environment. Also, your pasted Workbook.Save() overloads use HttpResponse object in the background, so the issue might be on your side. For confirmation, could you try to run the following code streaming an Excel file without involving Aspose.Cells APIs to check if it works fine or you got the similar problem?

e.g

Sample code:

FileStream fs1 = new FileStream(strFilePath, FileMode.Open, FileAccess.Read);

byte[] data1 = new byte[fs1.Length];

fs1.Read(data1, 0, data1.Length);

this.Response.ContentType = "application/xls";

Response.AddHeader( "content-disposition","attachment; filename=Book1.xls");

Response.BinaryWrite(data1);

Response.End();


Thank you.