我想在代碼隱藏中創建一個Excel工作表,然後下載它。我不想將文件保存在磁盤上,我想直接發送它作爲響應,我嘗試了下面的代碼。但我沒有得到確切的excel。如何發送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);
如何發送excel工作表對象作爲響應。因此,該用戶將被提示下載Excel文件
你是什麼意思**沒有得到確切練成**? –
只是我的直覺,但是寫'xlWorkSheet'到響應實際上做了什麼?充其量,我可以看到它發送對象的'.ToString()',而不是對象的內容。您可能想嘗試將工作簿保存到內存流中,然後發送該文件 – freefaller