ASP.NET Downloading exported excel sheet sometimes renders the source page itselft

TamerM

I have two reports that download excel files when the user clicks the export button. Both of them use the same method in an external library (shown below). One of the reports opens the excel sheet normally with the generated data while the other shows the source page design inside the excel sheet. This happens in VS2008. What is going on, and how to solve it?

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.ClearHeaders();
            HttpContext.Current.Response.Buffer = true;
            HttpContext.Current.Response.ContentType = "application/ms-excel;";
            HttpContext.Current.Response.BinaryWrite(System.Text.Encoding.UTF8.GetPreamble());
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=Report.xls");
            HttpContext.Current.Response.Charset = "utf-8";
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;

            HttpContext.Current.Response.Write(@"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">");

            //sets font
            HttpContext.Current.Response.Write("<font style='font-size:10.0pt; font-family:Calibri;'>");

            if (addCurrentDate)
                HttpContext.Current.Response.Write("<h5 align='left'>" + DateTime.Now.ToString() + "</h5>");
            HttpContext.Current.Response.Write("<h2>" + title + "</h2>");

            string subttitlesString = string.Empty;
            if (subItems != null && subItems.Count > 0)
            {
                subttitlesString = "<table>";
                foreach (var item in subItems)
                {
                    subttitlesString += string.Format("<tr><td style='font-weight:bold;'>{0}</td><td>{1}</td></tr>", item.Key, item.Value);
                }
                subttitlesString += "</table>";
                HttpContext.Current.Response.Write(subttitlesString);
            }

            //sets the table border, cell spacing, border color, font of the text, background, foreground, font height
            HttpContext.Current.Response.Write("<Table border='1' bgColor='#ffffff' " +
              "borderColor='#000000' cellSpacing='0' cellPadding='0' " +
              "style='font-size:10.0pt; font-family:Calibri; background:white;'> <TR>");

            //am getting my grid's column headers
            int columnscount = table.Columns.Count;

            for (int j = 0; j < columnscount; j++)
            {      //write in new column
                HttpContext.Current.Response.Write("<Td>");
                //Get column headers  and make it as bold in excel columns
                HttpContext.Current.Response.Write("<B>");
                HttpContext.Current.Response.Write(table.Columns[j].ColumnName.ToString());
                HttpContext.Current.Response.Write("</B>");
                HttpContext.Current.Response.Write("</Td>");
            }

            HttpContext.Current.Response.Write("</TR>");
            foreach (DataRow row in table.Rows)
            {//write in new row
                HttpContext.Current.Response.Write("<TR>");
                for (int i = 0; i < table.Columns.Count; i++)
                {
                    HttpContext.Current.Response.Write("<Td>");
                    HttpContext.Current.Response.Write(row[i].ToString());
                    HttpContext.Current.Response.Write("</Td>");
                }

                HttpContext.Current.Response.Write("</TR>");
            }

            HttpContext.Current.Response.Write("</Table>");
            HttpContext.Current.Response.Write("</font>");
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.End();

The excel sheet being exported:

TamerM

I found the cause of this, which is quite obvious now. The calling event of the above exporting method has two catch sections:

  1. ThreadAbortException
  2. Exception

The ThreadAbortException part had Thread.ResetAbort() because the ThreadAbortException keeps re-throwing itself. This seems to reset all the HTTPRespnse content that was previously added.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Chinese characters get unreadable when exported to excel in ASP.NET

From Dev

Downloading excel sheet, if there is any errors while uploading it

From Dev

Export dataset to the excel sheet in asp.net c#

From Dev

datetime parsing from excel sheet in asp.net

From Dev

datetime parsing from excel sheet in asp.net

From Dev

ASP.NET repeater only renders the first item in my XML data source

From Dev

FIle Downloading asp.net

From Dev

Downloading FIles on Asp.net

From Dev

exported excel sheet has no .xls file extension in Mozilla

From Dev

c# Asp.NET MVC downloading excel file using FileStreamResult

From Dev

Multiple tabs issue during the downloading of the Excel sheet from Jasper server

From Dev

Error while creating open XML excel sheet from ASP.NET MVC

From Dev

How to bind excel sheet to cascaded dropdown list in ASP.NET C#

From Dev

How to export data from database to excel sheet using ASP.NET MVC?

From Dev

How to always get the website title without downloading all the page source

From Dev

LAMP: PHP not working (keeps downloading source file instead of displaying the page)

From Dev

How does Asp.net Core renders a view

From Dev

ASP.Net application renders differently in IIS Servers

From Dev

Regular expression parsing partial renders with JMeter ASP.net

From Dev

ASP.NET Image Control Renders in wrong DOM location

From Dev

Asp.net gives error when it renders some styles

From Dev

How to avoid erasing the grid line color(default light gray) of excel sheet after changing the background of a cell in C#, ASP.NET?

From Dev

spring mvc export excel failed sometimes and data displays garblend on the page

From Dev

Uploading/Downloading Byte Arrays with AngularJS and ASP.NET Web API

From Dev

ASP.NET 404 when downloading file with unknown extension

From Dev

Downloading of zip file through ASP.NET MVC using DotNetZip

From Dev

Not able to display a label after downloading a file in asp.net

From Dev

file extension not downloading in ASP.NET 3.5 forms

From Dev

Downloading a file with Knockout bindings, ASP.NET MVC stack and no Razor

Related Related

  1. 1

    Chinese characters get unreadable when exported to excel in ASP.NET

  2. 2

    Downloading excel sheet, if there is any errors while uploading it

  3. 3

    Export dataset to the excel sheet in asp.net c#

  4. 4

    datetime parsing from excel sheet in asp.net

  5. 5

    datetime parsing from excel sheet in asp.net

  6. 6

    ASP.NET repeater only renders the first item in my XML data source

  7. 7

    FIle Downloading asp.net

  8. 8

    Downloading FIles on Asp.net

  9. 9

    exported excel sheet has no .xls file extension in Mozilla

  10. 10

    c# Asp.NET MVC downloading excel file using FileStreamResult

  11. 11

    Multiple tabs issue during the downloading of the Excel sheet from Jasper server

  12. 12

    Error while creating open XML excel sheet from ASP.NET MVC

  13. 13

    How to bind excel sheet to cascaded dropdown list in ASP.NET C#

  14. 14

    How to export data from database to excel sheet using ASP.NET MVC?

  15. 15

    How to always get the website title without downloading all the page source

  16. 16

    LAMP: PHP not working (keeps downloading source file instead of displaying the page)

  17. 17

    How does Asp.net Core renders a view

  18. 18

    ASP.Net application renders differently in IIS Servers

  19. 19

    Regular expression parsing partial renders with JMeter ASP.net

  20. 20

    ASP.NET Image Control Renders in wrong DOM location

  21. 21

    Asp.net gives error when it renders some styles

  22. 22

    How to avoid erasing the grid line color(default light gray) of excel sheet after changing the background of a cell in C#, ASP.NET?

  23. 23

    spring mvc export excel failed sometimes and data displays garblend on the page

  24. 24

    Uploading/Downloading Byte Arrays with AngularJS and ASP.NET Web API

  25. 25

    ASP.NET 404 when downloading file with unknown extension

  26. 26

    Downloading of zip file through ASP.NET MVC using DotNetZip

  27. 27

    Not able to display a label after downloading a file in asp.net

  28. 28

    file extension not downloading in ASP.NET 3.5 forms

  29. 29

    Downloading a file with Knockout bindings, ASP.NET MVC stack and no Razor

HotTag

Archive