protected void ExportToExcel()
{
DataTable dt = new DataTable();
if (!String.IsNullOrEmpty(txtFromDate.Text))
{
FromDate = txtFromDate.Text;
}
if (!String.IsNullOrEmpty(txtToDate.Text))
{
ToDate = txtToDate.Text;
}
//if (!String.IsNullOrEmpty(txtDateOfJournery.Text))
//{
// ReuestDate = txtDateOfJournery.Text;
//}
//if (ddlStatus.SelectedIndex > 0)
//{
StatusId = Convert.ToInt32(ddlStatus.SelectedValue);
//}
dt = GuestHouseBAL.GuestHouseBALInstance.GetGHBookingExportToExelReport(FromDate, ToDate, StatusId);
if(dt.Rows.Count > 0)
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=GuestHouseReport.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
using (StringWriter sw = new StringWriter())
{
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView GridView1 = new GridView();
//To Export all pages
GridView1.AllowPaging = false;
GridView1.DataSource = dt;
GridView1.DataBind();
string reason = "";
// 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);
//style to format numbers to string
string style = @"<style> .textmode { } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
}
else
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('No Record Found...');", true);
}
}
also add this if error occured
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
No comments:
Post a Comment