可以先使用 Microsoft Office.Interop.Excel 元件產生並另存 Excel
再利用 Stream 方式將新建的 Excel 供使用者下載
下面是以 Stream 方式下載已存在的 Excel 的 Sample Code :
protected void OutputExcel(Excel_RelPath)
{
string Excel_PhyPath = Server.MapPath(Excel_RelPath);
FileInfo fi = new FileInfo(Excel_PhyPath );
if (fi.Exists)
{
FileStream fs = new FileStream(ExcelName, FileMode.Open);
long FileSize = fs.Length;
// 將 Excel 內容以 Binary 方式存入 Buffer 變數
byte[] Buffer = new byte[(int)FileSize];
fs.Read(Buffer, 0, (int)FileSize);
fs.Close();
Response.AddHeader("Content-Disposition", "attachment;
filename=" + fi.Name);
Response.ContentType = "application/octet-stream";
Response.BinaryWrite(Buffer);
Response.Flush();
}
}
{
string Excel_PhyPath = Server.MapPath(Excel_RelPath);
FileInfo fi = new FileInfo(Excel_PhyPath );
if (fi.Exists)
{
FileStream fs = new FileStream(ExcelName, FileMode.Open);
long FileSize = fs.Length;
// 將 Excel 內容以 Binary 方式存入 Buffer 變數
byte[] Buffer = new byte[(int)FileSize];
fs.Read(Buffer, 0, (int)FileSize);
fs.Close();
Response.AddHeader("Content-Disposition", "attachment;
filename=" + fi.Name);
Response.ContentType = "application/octet-stream";
Response.BinaryWrite(Buffer);
Response.Flush();
}
}
沒有留言:
張貼留言