Hi comunidadmexi...,
Check this example. Now please take its reference and correct your code.
Namespaces
C#
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Threading;
VB.Net
Imports System.Data
Imports System.Drawing
Imports System.IO
Imports System.Text
Imports System.Threading
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
MTxlssp();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i < e.Row.Cells.Count; i++)
{
string encoded = e.Row.Cells[i].Text.Trim();
e.Row.Cells[i].Text = Context.Server.HtmlDecode(encoded);
}
}
}
public override void VerifyRenderingInServerForm(Control control)
{
}
private DataTable GetData()
{
DataTable dt = new DataTable();
dt.Columns.Add("Contents");
dt.Rows.Add("<p><h3><span style=color:#0000ff;><strong>test</strong></span></h3></p>");
dt.Rows.Add("<h4><strong>- test1</strong></h4>");
dt.Rows.Add("<h4><strong>- test2</strong></h4>");
return dt;
}
private void MTxlssp()
{
DataTable dt = GetData();
GridView GridView1 = new GridView
{
AllowPaging = false,
DataSource = dt
};
GridView1.RowDataBound += GridView1_RowDataBound;
GridView1.DataBind();
Thread.Sleep(3000);
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.ContentEncoding = Encoding.UTF8;
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("content-disposition", "attachment;filename=test.xls");
HttpCookie cookie2 = new HttpCookie("ExcelDownloadFlag")
{
Value = "Flag",
Expires = DateTime.Now.AddDays(1)
};
Response.AppendCookie(cookie2);
using (StringWriter sw = new StringWriter())
{
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.HeaderRow.BackColor = Color.White;
foreach (TableCell cell in GridView1.HeaderRow.Cells)
{
cell.BackColor = GridView1.HeaderStyle.BackColor;
}
foreach (GridViewRow row in GridView1.Rows)
{
row.BackColor = Color.White;
foreach (TableCell cell in row.Cells)
{
if (row.RowIndex % 2 == 0)
{
cell.BackColor = GridView1.AlternatingRowStyle.BackColor;
}
else
{
cell.BackColor = GridView1.RowStyle.BackColor;
}
cell.CssClass = "textmode";
}
}
GridView1.RenderControl(hw);
string style = @"<style> .textmode { } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
MTxlssp()
End Sub
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
For i As Integer = 0 To e.Row.Cells.Count - 1
Dim encoded As String = e.Row.Cells(i).Text.Trim()
e.Row.Cells(i).Text = Context.Server.HtmlDecode(encoded)
Next
End If
End Sub
Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
End Sub
Private Function GetData() As DataTable
Dim dt As DataTable = New DataTable()
dt.Columns.Add("Contents")
dt.Rows.Add("<p><h3><span style=color:#0000ff;><strong>test</strong></span></h3></p>")
dt.Rows.Add("<h4><strong>- test1</strong></h4>")
dt.Rows.Add("<h4><strong>- test2</strong></h4>")
Return dt
End Function
Private Sub MTxlssp()
Dim dt As DataTable = GetData()
Dim GridView1 As GridView = New GridView With {
.AllowPaging = False,
.DataSource = dt
}
AddHandler GridView1.RowDataBound, AddressOf GridView1_RowDataBound
GridView1.DataBind()
Thread.Sleep(3000)
Response.Clear()
Response.Buffer = True
Response.Charset = ""
Response.ContentEncoding = Encoding.UTF8
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader("content-disposition", "attachment;filename=test.xls")
Dim cookie2 As HttpCookie = New HttpCookie("ExcelDownloadFlag") With {
.Value = "Flag",
.Expires = DateTime.Now.AddDays(1)
}
Response.AppendCookie(cookie2)
Using sw As StringWriter = New StringWriter()
Dim hw As HtmlTextWriter = New HtmlTextWriter(sw)
GridView1.HeaderRow.BackColor = Color.White
For Each cell As TableCell In GridView1.HeaderRow.Cells
cell.BackColor = GridView1.HeaderStyle.BackColor
Next
For Each row As GridViewRow In GridView1.Rows
row.BackColor = Color.White
For Each cell As TableCell In row.Cells
If row.RowIndex Mod 2 = 0 Then
cell.BackColor = GridView1.AlternatingRowStyle.BackColor
Else
cell.BackColor = GridView1.RowStyle.BackColor
End If
cell.CssClass = "textmode"
Next
Next
GridView1.RenderControl(hw)
Dim style As String = "<style> .textmode { } </style>"
Response.Write(style)
Response.Output.Write(sw.ToString())
Response.Flush()
Response.End()
End Using
End Sub
Screenshot