I am trying to create an excel sheet in a code-behind and then download it. I don't want to save the file on the disk, I want to directly send it as a response, I tried the following code. But im not getting the exact excel.
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
for (int i = 1; i <= 100; i++)
for (int j = 1; j < 100; j++)
xlWorkSheet.Cells[i, j] = i + " : " + j;
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("Content-Disposition", "attachment; filename=translationText.xls");
this.EnableViewState = false;
Response.Write(xlWorkSheet);
Response.End();
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
How to send the excel worksheet object as the response. So, that user will be prompted to download the excel file