protected void WordBtn_Click(object sender, ImageClickEventArgs e) { try { DataTable dataTable = (DataTable)Session["dataTable"]; ExportStaffGrid.AllowPaging = false; ExportStaffGrid.HeaderStyle.ForeColor = System.Drawing.Color.White; ExportStaffGrid.BorderWidth = 0; ExportStaffGrid.DataSource = dataTable; ExportStaffGrid.DataBind(); Response.ClearContent(); Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "StaffDetails.doc")); Response.Charset = ""; Response.ContentType = "application/ms-word"; StringWriter sw = new StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); //Applying stlye to gridview header cells for (int i = 0; i < ExportStaffGrid.HeaderRow.Cells.Count; i++) { ExportStaffGrid.HeaderRow.Cells[i].Style.Add("background-color", "Black"); } ExportStaffGrid.RenderControl(htw); Response.Write(sw.ToString()); Response.End(); } catch (Exception ex) { MessageBoxShow(ex.Message.ToString()); } }
protected void ExportStaffGrid_PageIndexChanging(object sender, GridViewPageEventArgs e) { ExportStaffGrid.PageIndex = e.NewPageIndex; DataTable dt = (DataTable)Session["dataTable"]; ExportStaffGrid.DataSource = dt; ExportStaffGrid.DataBind(); }
protected void PdfBtn_Click(object sender, ImageClickEventArgs e) { Response.ContentType = "application/pdf"; Response.AddHeader("content-disposition", "attachment;filename=StaffEvaluation.pdf"); Response.Cache.SetCacheability(HttpCacheability.NoCache); StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); ExportStaffGrid.AllowPaging = false; ExportStaffGrid.DataBind(); ExportStaffGrid.RenderControl(hw); ExportStaffGrid.HeaderRow.Style.Add("width", "15%"); ExportStaffGrid.HeaderRow.Style.Add("font-size", "10px"); ExportStaffGrid.Style.Add("text-decoration", "none"); ExportStaffGrid.Style.Add("font-family", "Arial, Helvetica, sans-serif;"); ExportStaffGrid.Style.Add("font-size", "8px"); StringReader sr = new StringReader(sw.ToString()); Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f); HTMLWorker htmlparser = new HTMLWorker(pdfDoc); PdfWriter.GetInstance(pdfDoc, Response.OutputStream); pdfDoc.Open(); htmlparser.Parse(sr); pdfDoc.Close(); Response.Write(pdfDoc); Response.End(); }
protected void ExcelBtn_Click(object sender, ImageClickEventArgs e) { try { DataTable dataTable = (DataTable)Session["dataTable"]; Response.ClearContent(); Response.Buffer = true; Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "StaffDetails.csv")); Response.ContentType = "application/text"; ExportStaffGrid.BorderWidth = 0; ExportStaffGrid.AllowPaging = false; ExportStaffGrid.DataSource = dataTable; ExportStaffGrid.DataBind(); System.Text.StringBuilder strbldr = new System.Text.StringBuilder(); for (int i = 0; i < ExportStaffGrid.HeaderRow.Cells.Count; i++) { //separting header columns text with comma operator strbldr.Append(ExportStaffGrid.HeaderRow.Cells[i].Text + ','); } //appending new line for gridview header row strbldr.Append("\n"); for (int j = 0; j < ExportStaffGrid.Rows.Count; j++) { for (int i = 0; i < ExportStaffGrid.HeaderRow.Cells.Count; i++) { string stLine = ""; string value = HttpUtility.HtmlDecode(ExportStaffGrid.Rows[j].Cells[i].Text); var append = value.Contains(",") ? string.Format("\"{0}\"", value) : value; stLine = string.Format("{0}{1},", stLine, append); strbldr.Append(stLine); } //appending new line for gridview rows strbldr.Append("\n"); } Response.Write(strbldr.ToString()); Response.End(); } catch (Exception) { } }
protected void viewAllBtn_Click(object sender, EventArgs e) { if (Session["dataTable"] != null) { DataTable dataTableRead = (DataTable)Session["dataTable"]; if (Session["ViewALLExport"] == null) { ExportStaffGrid.AllowPaging = false; ExportStaffGrid.DataSource = dataTableRead; ExportStaffGrid.DataBind(); viewAllBtn.Text = "view with paging"; Session["ViewALLExport"] = true; } else { ExportStaffGrid.AllowPaging = true; ExportStaffGrid.DataSource = dataTableRead; ExportStaffGrid.DataBind(); viewAllBtn.Text = "view all"; Session["ViewALLExport"] = null; } } }
protected void exportStaffInfo() { string query = "select * from StaffInfo order by Name"; lblNote.Text = ""; try { SqlConnection excelConnection = new SqlConnection(connectionString); SqlCommand myCommand = new SqlCommand(query, excelConnection); try { DataTable dataTable = new DataTable(); DataTable dataTableRead = new DataTable(); //add data into datatable excelConnection.Open(); SqlDataAdapter oledbda = new SqlDataAdapter(); oledbda.SelectCommand = myCommand; oledbda.Fill(dataTableRead); SqlDataReader dr = (SqlDataReader)myCommand.ExecuteReader(); if (!dr.HasRows) { ExportPanel.Visible = false; ExportStaffGrid.Visible = false; excelConnection.Close(); oledbda = null; lblNote.Visible = true; lblNote.ForeColor = System.Drawing.Color.Red; lblNote.Text = "No result found."; } else { excelConnection.Close(); oledbda = null; ExportStaffGrid.Visible = true; ExportStaffGrid.AllowPaging = true; ExportStaffGrid.HorizontalAlign = HorizontalAlign.Center; ExportStaffGrid.DataSource = dataTableRead; ExportStaffGrid.DataBind(); if (dataTableRead.Rows.Count > 10) { viewAllBtn.Text = "view all"; } dataTable = dataTableRead; ExportPanel.Visible = true; Session["dataTable"] = dataTable; } } catch (SqlException ex) { ExportPanel.Visible = false; ExportStaffGrid.Visible = false; lblNote.Visible = true; lblNote.ForeColor = System.Drawing.Color.Red; lblNote.Text = ex.Message; } } catch (Exception ee) { ExportPanel.Visible = false; MessageBoxShow(ee.Message.ToString()); } }