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("List of Registered Student");
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();
}
}
protected void btnDownload_Click(object sender, ImageClickEventArgs e)
{
string Std_Name; string Batch_id; string Course_id; string catagory;
if (ddl_Stuname.SelectedIndex > 0)
{
Std_Name = ddl_Stuname.SelectedItem.Text.Trim();
}
else
{
Std_Name = null;
}
if (drp_batchID.SelectedIndex > 0)
{
Batch_id = drp_batchID.SelectedItem.Text;
}
else
{
Batch_id = null;
}
if (!(ddlCourses.SelectedIndex > 0))
{
Course_id = null;
}
else
{
Course_id = ddlCourses.SelectedItem.Value.Trim();
}
if (!(ddlCategory.SelectedIndex > 0))
{
catagory = null;
}
else
{
catagory = ddlCategory.SelectedItem.Text.Trim();
}
DataTable dt = new DataTable();
try
{
string TPID = Session["trainingpartnercode"] == null ? null : Convert.ToString(Session["trainingpartnercode"]);
// dt = new BLL_StudentRegistration().SearchStudentList(Std_Name, Batch_id, Course_id, catagory,txtAadhaar.Text.Trim()!=""?txtAadhaar.Text.Trim():null,TPID);
dt = new BLL_StudentRegistration().Exp2Excel_StudentList(Std_Name, Batch_id, Course_id, catagory, txtAadhaar.Text.Trim() != "" ? txtAadhaar.Text.Trim() : null, TPID);
if (dt != null && dt.Rows.Count > 0)
{
dt.Columns.Remove("Course_id");
dt.Columns.Remove("Guardian_Name");
dt.Columns.Remove("Marital_Status");
dt.Columns.Remove("DOB");
dt.Columns.Remove("Email_id");
dt.Columns.Remove("PER_COUNTRY_NAME");
dt.Columns.Remove("PER_STATE_NAME");
dt.Columns.Remove("PER_DIST_NAME");
dt.Columns.Remove("Permanent_district_id");
dt.Columns.Remove("Permanent_City");
dt.Columns.Remove("catogory_id");
dt.Columns.Remove("Permanent_Landmark");
dt.Columns.Remove("Permanent_Pincode");
dt.Columns.Remove("Present_Address_Line");
dt.Columns.Remove("PRE_COUNTRY_NAME");
dt.Columns.Remove("Present_Country_id");
dt.Columns.Remove("PRE_STATE_NAME");
dt.Columns.Remove("Present_State_id");
dt.Columns.Remove("Upload_photo");
dt.Columns.Remove("Upload_Signature");
dt.Columns.Remove("Upload_Fingerprint");
dt.Columns.Remove("Upload_Eligibility_certificate");
dt.Columns.Remove("Student_registration_status");
dt.Columns.Remove("Exams_Appeared");
dt.Columns.Remove("Exam_Result");
dt.Columns.Remove("upload_income_certificate");
dt.Columns.Remove("Bank_Address");
dt.Columns.Remove("MICR_Code");
dt.Columns.Remove("Ifsc_Code");
dt.Columns.Remove("Account_number");
dt.Columns.Remove("Beneficiary_Code");
dt.Columns.Remove("Benificiary_Name");
dt.Columns.Remove("Branch_Name");
dt.Columns.Remove("Bank_name");
dt.Columns.Remove("Present_Pincode");
dt.Columns.Remove("Present_Landmark");
dt.Columns.Remove("Present_City");
dt.Columns.Remove("PRE_DIST_NAME");
dt.Columns.Remove("Phone_No");
dt.Columns.Remove("Present_district_id");
dt.Columns["Training_Partner_Name"].ColumnName= "Training Partner ID";
dt.Columns["trainpname"].ColumnName = "Training Partner Name";
dt.Columns["Student_registration_id"].ColumnName = "Stud. Registration ID";
dt.Columns["Course_name"].ColumnName = "Course";
dt.Columns["Training_Partner_State"].ColumnName = "State";
dt.Columns["Certifying_agency_name"].ColumnName = "Key Implementing Agency";
dt.Columns["Father_Name"].ColumnName = "Father Name";
dt.Columns["Mother_Name"].ColumnName = "Mother Name";
dt.Columns["Catagory_name"].ColumnName = "Catagory";
dt.Columns["Permanent_Address_Line"].ColumnName = "Address";
// dt.Columns["level_name"].ColumnName = "Course Level";
// dt.Columns["TP_Name"].ColumnName = "Training PartnerName";
ExportToExcel(dt, "StudentListExport.xls");
}
else
{
}
}
catch (Exception ex) { ExceptionHandler.WriteException(ex.Message); }
finally
{
if (dt != null)
{
dt.Dispose();
}
GC.SuppressFinalize(this);
}
}
public override void VerifyRenderingInServerForm(Control control)
{
/*Verifies that the control is rendered */
}
No comments:
Post a Comment