1

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?

0

1 Answer 1

2

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();
Sign up to request clarification or add additional context in comments.

1 Comment

Hi I am facing the same issue and I tried to read the missing css file like you did but still getting the same error.:( any more suggestions?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.