protected void lnkExportToExcel_Click(object sender, EventArgs e) { string[] HeaderCaptions = { }; string[] DataColumnsName = { }; DataTable dtFilters = GetSearchDataTable(); DataTable dtExportColumns = new DataTable(); dtExportColumns.Columns.Add("ParamName", typeof(String)); dtExportColumns.Columns.Add("ParamValue", typeof(String)); foreach (ListItem li in CheckBoxList1.Items) { if (li.Selected == true) { dtExportColumns.Rows.Add(li.Value.ToString(), "1"); Array.Resize(ref HeaderCaptions, HeaderCaptions.Length + 1); HeaderCaptions[HeaderCaptions.Length - 1] = li.Text.ToString(); Array.Resize(ref DataColumnsName, DataColumnsName.Length + 1); DataColumnsName[DataColumnsName.Length - 1] = li.Value.ToString(); } } if (dtExportColumns.Rows.Count == 0) { string js = "alert('Please Select at least one column!');"; ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", js, true); return; } DataTable dt = BLL_Crew_CrewList.Get_Crewlist_Export(dtFilters, dtExportColumns, GetSessionUserID()); ChangeColumnDataType(dt, "EOC", typeof(string)); ChangeColumnDataType(dt, "Joining_date", typeof(string)); ChangeColumnDataType(dt, "Staff_Birth_Date", typeof(string)); foreach (DataRow item in dt.Rows) { if (!string.IsNullOrEmpty(item["EOC"].ToString())) { item["EOC"] = " " + UDFLib.ConvertUserDateFormat(Convert.ToString(item["EOC"]), UDFLib.GetDateFormat()); } if (!string.IsNullOrEmpty(item["Joining_date"].ToString())) { item["Joining_date"] = " " + UDFLib.ConvertUserDateFormat(Convert.ToString(item["Joining_date"]), UDFLib.GetDateFormat()); } if (!string.IsNullOrEmpty(item["Staff_Birth_Date"].ToString())) { item["Staff_Birth_Date"] = " " + UDFLib.ConvertUserDateFormat(Convert.ToString(item["Staff_Birth_Date"]), UDFLib.GetDateFormat()); } } GridViewExportUtil.ExportToExcel(dt, HeaderCaptions, DataColumnsName, "CrewListExport.xls", "CrewList Export"); }
protected void lnkExportToText_Click(object sender, EventArgs e) { string[] HeaderCaptions = { }; string[] DataColumnsName = { }; DataTable dtFilters = GetSearchDataTable(); DataTable dtExportColumns = new DataTable(); dtExportColumns.Columns.Add("ParamName", typeof(String)); dtExportColumns.Columns.Add("ParamValue", typeof(String)); foreach (ListItem li in CheckBoxList1.Items) { if (li.Selected == true) { dtExportColumns.Rows.Add(li.Value.ToString(), "1"); Array.Resize(ref HeaderCaptions, HeaderCaptions.Length + 1); HeaderCaptions[HeaderCaptions.Length - 1] = li.Text.ToString(); Array.Resize(ref DataColumnsName, DataColumnsName.Length + 1); DataColumnsName[DataColumnsName.Length - 1] = li.Value.ToString(); } } if (dtExportColumns.Rows.Count == 0) { string js = "alert('Please select at least one column in Export Options!');"; ScriptManager.RegisterStartupScript(this, this.GetType(), "least", js, true); return; } DataTable dt = BLL_Crew_CrewList.Get_Crewlist_Export(dtFilters, dtExportColumns, GetSessionUserID()); ChangeColumnDataType(dt, "EOC", typeof(string)); ChangeColumnDataType(dt, "Joining_date", typeof(string)); ChangeColumnDataType(dt, "Staff_Birth_Date", typeof(string)); foreach (DataRow item in dt.Rows) { if (!string.IsNullOrEmpty(item["EOC"].ToString())) { item["EOC"] = UDFLib.ConvertUserDateFormat(Convert.ToString(item["EOC"]), UDFLib.GetDateFormat()); } if (!string.IsNullOrEmpty(item["Joining_date"].ToString())) { item["Joining_date"] = UDFLib.ConvertUserDateFormat(Convert.ToString(item["Joining_date"]), UDFLib.GetDateFormat()); } if (!string.IsNullOrEmpty(item["Staff_Birth_Date"].ToString())) { item["Staff_Birth_Date"] = UDFLib.ConvertUserDateFormat(Convert.ToString(item["Staff_Birth_Date"]), UDFLib.GetDateFormat()); } } using (StringWriter sw = new StringWriter()) { for (int i = 0; i <= dt.Rows.Count - 1; i++) { sw.WriteLine((i + 1).ToString()); sw.WriteLine("------------------------------"); foreach (ListItem li in CheckBoxList1.Items) { if (li.Selected == true) { sw.WriteLine(li.Text + ": " + dt.Rows[i][li.Value.ToString()].ToString()); } } sw.WriteLine(" "); } string fileName = "CrewList.txt"; HttpContext.Current.Response.Clear(); HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", fileName)); HttpContext.Current.Response.ContentType = "text/plain"; HttpContext.Current.Response.Write(sw.ToString()); HttpContext.Current.Response.End(); } }