protected void btnPdf_Click(object sender, EventArgs e) { List <GetAbsentMembersByID_Result> Absentemployee = MembersAttendanceHistoryController.GetAbsentMemberById(txtSearchText.Text, SortField, SortDir, txtFromDate.Text, txtToDate.Text, memberId); GridView GridView1 = new GridView(); GridView1.DataSource = Absentemployee; GridView1.DataBind(); Response.ContentType = "application/pdf"; Response.AddHeader("content-disposition", "attachment;filename=AbsentMember" + memberId + ".PDF"); Response.Cache.SetCacheability(HttpCacheability.NoCache); StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); GridView1.RenderControl(hw); StringReader sr = new StringReader(sw.ToString()); Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f); HTMLWorker htmlparser = new HTMLWorker(pdfDoc); PdfWriter.GetInstance(pdfDoc, Response.OutputStream); pdfDoc.Open(); htmlparser.Parse(sr); pdfDoc.Close(); Response.Write(pdfDoc); Response.End(); }
public void BindPresentMemberById() { try { if (ddlOptions.SelectedValue == "1") { grdPresentById.DataSource = MembersAttendanceHistoryController.GetPresentMemberById(txtSearchText.Text, SortField, SortDir, txtFromDate.Text, txtToDate.Text, memberId); grdPresentById.DataBind(); } if (ddlOptions.SelectedValue == "2") { grdAbsentbyId.DataSource = MembersAttendanceHistoryController.GetAbsentMemberById(txtSearchText.Text, SortField, SortDir, txtFromDate.Text, txtToDate.Text, memberId); grdAbsentbyId.DataBind(); } } catch (Exception ex) { throw ex; } }
public void BindExcel() { try { Response.Clear(); Response.Buffer = true; Response.AddHeader("content-disposition", "attachment;filename=" + memberId + "AbsentMember.xls"); Response.Charset = ""; Response.ContentType = "application/vnd.ms-excel"; StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); var grid = new GridView(); grid.DataSource = MembersAttendanceHistoryController.GetAbsentMemberById(txtSearchText.Text, SortField, SortDir, txtFromDate.Text, txtToDate.Text, memberId); grid.DataBind(); grid.AllowPaging = false; //Change the Header Row back to white color grid.HeaderRow.Style.Add("background-color", "#FFFFFF"); //Apply style to Individual Cells grid.HeaderRow.Cells[0].Style.Add("background-color", "green"); grid.HeaderRow.Cells[1].Style.Add("background-color", "green"); grid.HeaderRow.Cells[2].Style.Add("background-color", "green"); grid.HeaderRow.Cells[3].Style.Add("background-color", "green"); grid.HeaderRow.Cells[4].Style.Add("background-color", "green"); for (int i = 0; i < grid.Rows.Count; i++) { GridViewRow row = grid.Rows[i]; //Change Color back to white row.BackColor = System.Drawing.Color.White; //Apply text style to each Row row.Attributes.Add("class", "textmode"); //Apply style to Individual Cells of Alternating Row if (i % 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"); } } grid.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 (Exception ex) { throw ex; } }
protected void btnExcel_Click(object sender, EventArgs e) { List <GetAbsentMembersByID_Result> Absentemployee = MembersAttendanceHistoryController.GetAbsentMemberById(txtSearchText.Text, SortField, SortDir, txtFromDate.Text, txtToDate.Text, memberId); ExportToExcel(Absentemployee); //BindExcel(); //var grid = new GridView(); //grid.DataSource = MembersAttendanceHistoryController.GetAbsentMemberById(txtSearchText.Text, SortField, SortDir, txtFromDate.Text, txtToDate.Text, memberId); //grid.DataBind(); //Response.ClearContent(); //Response.AddHeader("content-disposition", "attachment; filename=iDealConfig.xls"); //Response.ContentType = "application/excel"; //StringWriter sw = new StringWriter(); //HtmlTextWriter htmlTextWriter = new HtmlTextWriter(sw); //grid.RenderControl(htmlTextWriter); //Response.Write(sw.ToString()); //Response.End(); //DataTable dt = new DataTable(); //for (int i = 0; i < grdAbsentbyId.Columns.Count; i++) //{ // //dt.Columns.Add("column" + i.ToString()); // dt.Columns.Add(grdAbsentbyId.Columns[i].HeaderText); //} //foreach (GridViewRow row in grdAbsentbyId.Rows) //{ // DataRow dr = dt.NewRow(); // for (int j = 1; j <= grdAbsentbyId.Columns.Count; j++) // { // dr["column" + j.ToString()] = row.Cells[j].Text; // } // dt.Rows.Add(dr); // IEnumerable<string> columnNames = dt.Columns.Cast<DataColumn>(). // Select(column => column.ColumnName); // StringBuilder sb = new StringBuilder(); // sb.AppendLine(string.Join(",", columnNames)); // foreach (DataRow row1 in dt.Rows) // { // string[] fields = row1.ItemArray.Select(field => field.ToString()). // ToArray(); // sb.AppendLine(string.Join(",", fields)); // } // File.WriteAllText("E:\\AbsentEmployee.csv", sb.ToString()); //} ////IEnumerable<string> columnNames = dt.Columns.Cast<DataColumn>(). //// Select(column => column.ColumnName); ////StringBuilder sb = new StringBuilder(); ////sb.AppendLine(string.Join(",", columnNames)); ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "succ_msg", "MessageBox('Success','Excel Saved Successfully in E drive');", true); }