[英]How to send an excel workbook as an response?
我正在嘗試在代碼隱藏區中創建一個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文件
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("Content-Disposition", "attachment; filename=translationText.xls");
Response.Write("<table>");
for (int i = 1; i <= 100; i++)
{
Response.Write("<tr>");
for (int j = 1; j < 100; j++)
{
Response.Write("<td>"+i + " : " + j+"</td>");
}
Response.Write("</tr>");
}
Response.Write("</table>");
Response.Flush();
Response.End();
嘗試上面的代碼,如果您正在從excel中讀取內容,然后編寫它,則無需在此處創建excel對象。
很好的不同的代碼來讀取和寫入excel文件到瀏覽器是
Response.BinaryWrite(yourexcelstream)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.