How to prevent css missing when exporting some html to excel file?


Tags: c#,asp.net,css,export-to-excel

Problem :

I am exporting an html table to excel file successfully using following code

    public void exportGridToExcel(Control ctl)
    {
        string attachment = "attachment; filename=etrack_excel_export.xls";
        HttpContext.Current.Response.ClearContent();
        HttpContext.Current.Response.AddHeader("content-disposition", attachment);
        HttpContext.Current.Response.ContentType = "application/ms-excel";
        StringWriter stw = new StringWriter();
        HtmlTextWriter htextw = new HtmlTextWriter(stw);

        ctl.RenderControl(htextw);
        HttpContext.Current.Response.Write(stw.ToString());
        HttpContext.Current.Response.End();
    }

The issue is that after exportation all css I have applied to the table is missing in the excel file , How can I prevent the css from missing?



Solution :

I finally got what to do ! I share It with anyone who has the same issue :

        string attachment = "attachment; filename=etrack_excel_export.xls";
        HttpContext.Current.Response.ClearContent();
        HttpContext.Current.Response.AddHeader("content-disposition", attachment);
        HttpContext.Current.Response.ContentType = "application/ms-excel";
        StringWriter stw = new StringWriter();
        HtmlTextWriter htextw = new HtmlTextWriter(stw);
        ctl.RenderControl(htextw);
        HttpContext.Current.Response.Write(stw.ToString());
        FileInfo fi = new FileInfo(Server.MapPath("../Content/Styles/StyleSheet.css"));
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        StreamReader sr = fi.OpenText();
        while (sr.Peek() >= 0)
        {
            sb.Append(sr.ReadLine());
        }
        sr.Close();
        Response.Write("<html><head><style type='text/css'>" + sb.ToString() + "</style></head>" + stw.ToString() + "</html>");
        stw = null;
        htextw = null;
        Response.Flush();
        Response.End();

    CSS Howto..

    How to “break line” via CSS?

    Shadow-Piercing descendant combinator, '/deep/' , including '::shadow' pseudo elements, are being deprecated so how do we pierce the Shadow DOM?

    CSS how to keep text always inline with a paragraph

    CSS: How to make this kind layout?

    background image not showing on nested

    How to add CSS to CKEDITOR?

    how to center text AND have max width of 800px in CSS?

    How to create an image hover fade effect with jQuery and CSS?

    How can i apply css stylesheet to single specific element?

    Trying to show list items vertically but two items horizontally

    How can i use css frameworks without cluttering html with too many classes [closed]

    How to put a inside a

    without transpassing border height? [closed]

    How to run multiple css selector classes at once?

    How can I make use of `::selection` for in chrome browsers?

    CSS: How to position divs 2 by 2

    CSS: How to put form input fields and button next to each other in the same line?

    How to make a moving dashed border with CSS?

    How to style Repeater item background color ontap?

    How to create HTML/CSS menu 100% width where text goes to the edges?

    How to use CSS to create a particular stylized, multi-lined text box?

    How to achieve this CSS layout? [closed]

    how to insert a ribbon inside a div how background without images only pure css? [closed]

    How to implement dynamic layout columns by using the `content_for?` method?

    Color of links not always showing correctly

    How can I get li items to slide and/or fade into view on this jQuery list?

    Sencha Touch 2: how to remove right arrow of datepickerfield by using css?

    CSS: How to scale an image from the center instead of top-left

    How do I create a 'linklist' like a tooltip with HTML and CSS or jQuery and CSS?

    How to create a pricetag shape in CSS and HTML

    CSS? How is this arrow made?