Click here to Skip to main content
15,895,192 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
 this is the code that i m using , can anyone explain why the table is not aligned properly when viewed in excel and some colors are also not displayed

protected void Page_Load(object sender, EventArgs e)
        {
            //check if session is valid
           // WebUtility.WebUtility.CheckValidSession(Context, Response, Session);

            tdOnTime2.BackColor = Color.FromName(Valuelist.FirstOrDefault().Color == "Amber" ? "Orange" : Valuelist.FirstOrDefault().Color);
            tdAceeptableDelay2.BackColor = Color.FromName(Valuelist.Skip(1).FirstOrDefault().Color == "Amber" ? "Orange" : Valuelist.Skip(1).FirstOrDefault().Color);
            tdDelayed2.BackColor = Color.FromName(Valuelist.LastOrDefault().Color == "Amber" ? "Orange" : Valuelist.LastOrDefault().Color);

            tdOnTime.BackColor = Color.FromName(Valuelist.FirstOrDefault().Color == "Amber" ? "Orange" : Valuelist.FirstOrDefault().Color);
            tdAceeptableDelay.BackColor = Color.FromName(Valuelist.Skip(1).FirstOrDefault().Color == "Amber" ? "Orange" : Valuelist.Skip(1).FirstOrDefault().Color);
            tdDelayed.BackColor = Color.FromName(Valuelist.LastOrDefault().Color == "Amber" ? "Orange" : Valuelist.LastOrDefault().Color);
            //get CP from querystring
            int CPID ;
            int.TryParse(Request.QueryString.Get("CPID"), out CPID);
            this.ucComplianceReportForCP.CPID = CPID;
            this.ucComplianceReportForCP.DisplayComplianceForCP();
        }
        protected void btnExportToExcel_Click(object sender, EventArgs e)
        {
            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment;filename=ComplianceReport.xls");
            Response.Charset = "";
            Response.ContentType = "application/vnd.ms-excel";
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
          //  rptCustomers.RenderControl(hw);
            
           // this.ltExcelCPtitle.Visible = true;
            pnlComplianceData.RenderControl(hw);
            Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();
           // ltExcelCPtitle.Visible = false;
        }


What I have tried:

i don't no what is causing this issue..
Posted
Updated 10-May-16 19:19pm
Comments
Jewel-Davis 23-Jun-16 6:06am    
Hi madhankannan346, you can refer to this example, it demonstrates how to create a "real" excel file with all style and formatting kept from GridView, by using this .NET library for excel files.

1 solution

When you are export your gridview to excel then you are writing it as 'HtmlTextWriter' and it write only plain text not with rich text, if you want to export your gridview to excel with all style and formatting then you need to use style tags
C#
@"<style> .textmode { } </style>";

see below link for more help
Export GridView to Excel in ASP.Net with Formatting using C# and VB.Net[^]
 
Share this answer
 
Comments
madhankannan 11-May-16 1:41am    
thank you for the solution but it doesn't seem to working..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900