private void ExportGridToPDF(GridView GridView1)
{
try
{
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
//To Export all pages
GridView1.AllowPaging = false;
this.BindGrid();
GridView1.Columns[19].Visible=false;
GridView1.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A2, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=LevelWiseCourseReport+'" + System.DateTime.UtcNow.ToString() + "'.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();
}
}
}
catch (Exception ex)
{
}
}
private void BindGrid()
{
DataTable dt = new DataTableimgexcel_Click
();
try
{
SqlParameter[] para = new SqlParameter[]
{
new SqlParameter("@CA_ID",drp_certifying.SelectedIndex>0?drp_certifying.SelectedItem.Value: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",drp_batch.SelectedIndex>0?Convert.ToInt32(drp_batch.SelectedItem.Value):(int?)null)
};
dt = null;
dt = SqlHelper.ExecuteDataTable(Configuration.ConnectionString, CommandType.StoredProcedure, "[USP_STUDENT_DETAILS_REPORT]", para);
if (dt != null && dt.Rows.Count > 0)
{
grdRequestCourse.DataSource = dt;
grdRequestCourse.DataBind();
imgexcel.Visible = true;
// imgepdf.Visible = true;
}
else
{
grdRequestCourse.DataSource = null;
grdRequestCourse.DataBind();
imgexcel.Visible = false;
imgepdf.Visible = false;
}
}
catch (System.Data.SqlClient.SqlException ee)
{
ExceptionHandler.WriteException(ee.Message);
}
catch (Exception ee)
{
ExceptionHandler.WriteException(ee.Message);
}
finally
{
if (dt != null)
{
dt.Dispose();
}
}
}
No comments:
Post a Comment