private void Export2Excel()
{
DataTable dt = new DataTable();
try
{
SqlParameter[] para = new SqlParameter[]
{
new SqlParameter("@CA_ID",drp_certifying.SelectedIndex>0?Convert.ToString(drp_certifying.SelectedValue):null),
new SqlParameter("@TP_Id",drp_trainingPartner.SelectedIndex>0?drp_trainingPartner.SelectedItem.Value:null),
new SqlParameter("@State_Id",drp_State.SelectedIndex>0?drp_State.SelectedItem.Value:null),
new SqlParameter("@Exam_Result",ddlResult.SelectedIndex>0? ddlResult.SelectedItem.Value:null),
new SqlParameter("@Course_ID",drp_course.SelectedIndex>0?drp_course.SelectedItem.Value:null),
new SqlParameter("@catogory_id",ddlCategory.SelectedIndex>0? ddlCategory.SelectedItem.Value:null),
new SqlParameter("@gender",ddlGender.SelectedIndex>0? ddlGender.SelectedItem.Value:null),
new SqlParameter("@Level_Name",ddlLevelName.SelectedIndex>0? ddlLevelName.SelectedItem.Text:null),
new SqlParameter("@FROMDATE",txtFromDate.Text!=""? txtFromDate.Text:null),
new SqlParameter("@TODATE",txtToDate.Text!=""? txtToDate.Text:null),
new SqlParameter("@DistrictID",drp_district.SelectedIndex>0?drp_district.SelectedItem.Value:null),
new SqlParameter("@BATCHID",(int?)null)
};
dt = SqlHelper.ExecuteDataTable(Configuration.ConnectionString, CommandType.StoredProcedure, "[SP_STUDENT_DETAILS_REPORT_excel12]", para);
if (dt != null && dt.Rows.Count > 0)
{
dt.Columns.Remove("Course_Id");
dt.Columns.Remove("CA_Id");
dt.Columns.Remove("Batch_Id");
dt.Columns.Remove("State_Id");
dt.Columns.Remove("catogory_id");
dt.Columns.Remove("Exams_Appeared");
dt.Columns.Remove("Exam_Result");
dt.Columns.Remove("Upload_photo");
dt.Columns.Remove("Upload_Signature");
dt.Columns.Remove("Upload_Eligibility_certificate");
dt.Columns.Remove("upload_income_certificate");
dt.Columns.Remove("upload_cast_certificate");
dt.Columns.Remove("TP_DISTRICT_ID");
dt.Columns.Remove("Adharcard_No");
dt.Columns["CA_Name"].ColumnName = "KIA Name";
//dt.Columns["Adharcard_No"].ColumnName = "Aadhaar No";
dt.AcceptChanges();
GridView GridView1 = new GridView();
GridView1.AllowPaging = false;
GridView1.DataSource = dt;
GridView1.DataBind();
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=StudentList.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
string sdate = !string.IsNullOrEmpty(txtFromDate.Text) ? txtFromDate.Text.Trim() : string.Empty;
string ddate = !string.IsNullOrEmpty(txtToDate.Text) ? txtToDate.Text.Trim() : string.Empty;
hw.Write("<b> Showing Results from " + sdate + " to " + ddate + " </b>");
for (int i = 0; i < GridView1.Rows.Count; i++)
{
//Apply text style to each Row
GridView1.Rows[i].Attributes.Add("class", "textmode");
}
GridView1.RenderControl(hw);
//style to format numbers to string
string style = @"<style> .textmode { mso-number-format:\@; } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
}
catch (System.Data.SqlClient.SqlException ee)
{
ExceptionHandler.WriteException(ee.Message);
}
catch (Exception ee)
{
ExceptionHandler.WriteException(ee.Message);
}
finally
{
if (dt != null)
{
dt.Dispose();
}
}
}
or
void ExportToExcel(DataTable dt, string FileName)
{
if (dt.Rows.Count > 0)
{
string filename = FileName + ".xls";
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
tw.Write("<div align='left'>");
tw.Write("<h1>");
tw.WriteLine("Exam Schedule List");
tw.Write("</div>");
tw.Write("</h1>");
DataGrid dgGrid = new DataGrid();
dgGrid.DataSource = dt;
dgGrid.DataBind();
//Get the HTML for the control.
dgGrid.RenderControl(hw);
//Write the HTML back to the browser.
//Response.ContentType = application/vnd.ms-excel;
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName + "");
this.EnableViewState = false;
Response.Write(tw.ToString());
// Response.End();
HttpContext.Current.Response.Flush(); // Sends all currently buffered output to the client.
HttpContext.Current.Response.SuppressContent = true; // Gets or sets a value indicating whether to send HTTP content to the client.
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
}
No comments:
Post a Comment