#region Save & Quit
Guid guid = Guid.NewGuid();
//Save and quit, use SaveCopyAs since SaveAs does not always work
string fileName = "IRSReport_" + guid.ToString() + ".xls";
string target = Server.MapPath("~/" + fileName);
xlApp.DisplayAlerts = false; //Supress overwrite request
xlWorkBook.SaveAs(target, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
//Release objects
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
//Give the user the option to save the copy of the file anywhere they desire
String FilePath = Server.MapPath("~/" + fileName);
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.ClearContent();
response.Clear();
response.ContentType = "text/plain";
response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ";");
response.TransmitFile(FilePath);
response.Flush();
response.Close();
//Delete the temporary file
DeleteFile(fileName);
#endregion
答案 0 :(得分:2)
我会删除那些应该没用的ClearContent,Clear,Flush和Close。
内容类型也不正确,因为它是一个xls文件,它应该是“application / vnd.ms-excel”。
最后,我会尽量不要过早删除文件,让服务器在删除之前发送文件。