ASHX下载xlsx文件

时间:2016-01-05 16:51:55

标签: c# .net web ashx

尝试编写ASHX响应时,我遇到了一个非常奇怪的问题。我在我的Web项目中使用它来允许用户下载生成的.xlsx文件。首先,我有一个response.redirect从我的主页到.ashx文件,在.ashx文件中,我生成一个excel文件并将其发送到响应。

这在Microsoft Visual Studio中运行时非常有效 - 我的编码环境,但在将其导出到IIS 8.5服务器时不起作用。我得到的只是一个白页而不是xlsx文件。我已经尝试了Response.end和CompleteRequest。如果有人有线索,请帮忙。 (WriteExcelFile可以工作,因为它在编码环境中生成一个完美的文件)

   System.Web.HttpResponse Response = System.Web.HttpContext.Current.Response;

                System.Web.HttpRequest Request = System.Web.HttpContext.Current.Request;

                //Remove block
                System.Data.DataTable myExcelData = new System.Data.DataTable();
                myExcelData.Columns.Add("Tid", typeof(string));
                myExcelData.Columns.Add("Grupp", typeof(string));
                myExcelData.Columns.Add("Felkod", typeof(Int32));
                myExcelData.Columns.Add("FelText", typeof(string));
                myExcelData.Columns.Add("Kommentar", typeof(string));
                myExcelData.Columns.Add("Kommentar2", typeof(string));
                myExcelData.Columns.Add("Severity", typeof(Int32));
                myExcelData.AcceptChanges();
                myExcelData.Rows.Add( "STARTTID",
                   "FELTYP",
                   17,
                   "HEJSAN",
                   "NO COMMENT",
                   "NO COMMENT",
                   23);
                myExcelData.AcceptChanges();
                System.Data.DataSet ds = new System.Data.DataSet();
                ds.Tables.Add(myExcelData);

                //Remove Memorystream block
                System.IO.MemoryStream stream = new System.IO.MemoryStream();
                using (SpreadsheetDocument document = SpreadsheetDocument.Create(stream, SpreadsheetDocumentType.Workbook, true))
                {

                    ExportToExcel.CreateExcelFile.WriteExcelFile(ds, document, "AlarmList");
                }
                stream.Flush();
                stream.Position = 0;

                Response.ClearContent();
                Response.Clear();
                Response.Buffer = true;
                Response.Charset = "";

                //  NOTE: If you get an "HttpCacheability does not exist" error on the following line, make sure you have
                //  manually added System.Web to this project's References.

                Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
                Response.AddHeader("content-disposition", "attachment; filename=" + "header.xlsx");
                Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                byte[] data1 = new byte[stream.Length];
                stream.Read(data1, 0, data1.Length);
                stream.Close();
                Response.BinaryWrite(data1);
                Response.Flush();

                Response.End();

            //    System.Web.HttpContext.Current.Response.SuppressContent = true;  // Gets or sets a value indicating whether to send HTTP content to the client.
            //    System.Web.HttpContext.Current.ApplicationInstance.CompleteRequest(); // Causes ASP.NET to bypass all events 
            //Response kastar alltid exception .end exekveras inte. så vi kör linjen ovan istellet

0 个答案:

没有答案