Export to csv and pdf (grid)
Export to csv and pdf (grid)
Write the below code in aspx page
<%@ Page Language="C#" AutoEventWireup="true" EnableEventValidation="false" CodeBehind="Grid.aspx.cs" Inherits="Grid_Binding_EntityFrameWork_Asp.net_.Grid" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<asp:Button ID="btnExportExcel" runat="server" Text="ExportToExcel" OnClick="btnExportExcel_Click" />
<asp:Button ID="btnExportPDF" runat="server" Text="ExportToPDF" OnClick="btnExportPDF_Click" />
Write
the below code in aspx.cs page
protected void btnExportCSV_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.csv");
Response.Charset = "";
Response.ContentType = "application/text";
GridView1.AllowPaging = false;
GridView1.DataBind();
StringBuilder sb = new StringBuilder();
for (int k = 0; k < GridView1.Columns.Count; k++)
{
//add
separator
sb.Append(GridView1.Columns[k].HeaderText + ',');
}
//append
new line
sb.Append("\r\n");
for (int i = 0; i < GridView1.Rows.Count; i++)
{
for (int k = 0; k < GridView1.Columns.Count; k++)
{
//add separator
sb.Append(GridView1.Rows[i].Cells[k].Text + ',');
}
//append
new line
sb.Append("\r\n");
}
Response.Output.Write(sb.ToString());
Response.Flush();
Response.End();
}
protected void btnExportExcel_Click(object sender, EventArgs e)
{
ExportExcel_Click();
}
private void ExportExcel_Click()
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=UserData.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
using (StringWriter sw = new StringWriter())
{
HtmlTextWriter hw = new HtmlTextWriter(sw);
//To
Export all pages
GridView1.AllowPaging = false;
this.bind();
GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF");
//Apply
style to Individual Cells
GridView1.HeaderRow.Cells[0].Style.Add("background-color", "green");
GridView1.HeaderRow.Cells[1].Style.Add("background-color", "green");
GridView1.HeaderRow.Cells[2].Style.Add("background-color", "green");
GridView1.HeaderRow.Cells[3].Style.Add("background-color", "green");
GridView1.HeaderRow.Cells[4].Style.Add("background-color", "green");
GridView1.HeaderRow.Cells[5].Style.Add("background-color", "green");
GridView1.HeaderRow.Cells[5].Visible = false;
GridView1.HeaderRow.Cells[6].Visible = false;
GridView1.HeaderRow.Cells[7].Visible = false;
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)
{
row.Cells[0].Style.Add("background-color", "#C2D69B");
row.Cells[1].Style.Add("background-color", "#C2D69B");
row.Cells[2].Style.Add("background-color", "#C2D69B");
row.Cells[3].Style.Add("background-color", "#C2D69B");
row.Cells[4].Style.Add("background-color", "#C2D69B");
row.Cells[5].Style.Add("background-color", "#C2D69B");
row.Cells[5].Visible
= false;
row.Cells[6].Visible = false;
row.Cells[7].Visible = false;
cell.BackColor =
GridView1.AlternatingRowStyle.BackColor;
}
else
{
cell.BackColor =
GridView1.RowStyle.BackColor;
}
row.Cells[5].Visible = false;
row.Cells[6].Visible = false;
row.Cells[7].Visible = false;
cell.CssClass = "textmode";
}
}
GridView1.RenderControl(hw);
//style
to format numbers to string
string style = @"<style>
.textmode { } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
}
Comments
Post a Comment