using System;
using System.Data;
using System.Web;
using System.Text;
using System.Drawing;
using System.Xml.XPath;
using System.Reflection;
//using Excel = Microsoft.Office.Interop.Excel;
using System.Web.UI.WebControls;
using System.IO;
using System.Web.UI;
/// <summary>
/// Summary description for SpreadToExcel
/// </summary>
public class SpreadToExcel
{
StringBuilder strb = new StringBuilder();
public SpreadToExcel()
{
//
// TODO: Add constructor logic here
//
}
public Boolean ConvertToCSV(DataTable dt, string FileName)
{
try
{
string attachment = string.Format("attachment; filename={0}", FileName + ".csv");
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.AddHeader("content-disposition", attachment);
HttpContext.Current.Response.Charset = "";
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.ContentType = "text/csv";
HttpContext.Current.Response.AddHeader("Pragma", "public");
HttpContext.Current.Response.Write(Environment.NewLine);
//write gridview details
int iColCount = dt.Columns.Count;
// First we will write the headers.
for (int i = 0; i < iColCount; i++)
{
//HttpContext.Current.Response.Write(dt.Columns[i].ToString());
AddComma(dt.Columns[i].ToString(), strb);
}
HttpContext.Current.Response.Write(strb.ToString());
HttpContext.Current.Response.Write(Environment.NewLine);
strb.Remove(0, strb.Length);
foreach (DataRow dr in dt.Rows)
{
for (int i = 0; i < iColCount; i++)
{
//if (!Convert.IsDBNull(dr[i]))
//{
AddComma(dr[i].ToString(), strb);
//}
//if (i < iColCount - 1)
//{
// HttpContext.Current.Response.Write(",");
//}
}
HttpContext.Current.Response.Write(strb.ToString());
HttpContext.Current.Response.Write(Environment.NewLine);
strb.Remove(0, strb.Length);
}
HttpContext.Current.Response.End();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ClearContent();
return true;
}
catch
{
return false;
}
}
public Boolean ConvertToCSV(DataTable dtHeader, DataTable dt, string FileName)
{
try
{
string attachment = string.Format("attachment; filename={0}", FileName + ".csv");
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.AddHeader("content-disposition", attachment);
HttpContext.Current.Response.Charset = "";
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.ContentType = "text/csv";
HttpContext.Current.Response.AddHeader("Pragma", "public");
//write header values
int headColCount = dtHeader.Columns.Count;
foreach (DataRow dr in dtHeader.Rows)
{
for (int i = 0; i < headColCount; i++)
{
//if (!Convert.IsDBNull(dr[i]))
//{
AddComma(dr[i].ToString(), strb);
//}
//if (i < iColCount - 1)
//{
// HttpContext.Current.Response.Write(",");
//}
}
HttpContext.Current.Response.Write(strb.ToString());
HttpContext.Current.Response.Write(Environment.NewLine);
strb.Remove(0, strb.Length);
}
HttpContext.Current.Response.Write(Environment.NewLine);
//write gridview details
int iColCount = dt.Columns.Count;
// First we will write the headers.
for (int i = 0; i < iColCount; i++)
{
//HttpContext.Current.Response.Write(dt.Columns[i].ToString());
AddComma(dt.Columns[i].ToString(), strb);
}
HttpContext.Current.Response.Write(strb.ToString());
HttpContext.Current.Response.Write(Environment.NewLine);
strb.Remove(0, strb.Length);
foreach (DataRow dr in dt.Rows)
{
for (int i = 0; i < iColCount; i++)
{
//if (!Convert.IsDBNull(dr[i]))
//{
AddComma(dr[i].ToString(), strb);
//}
//if (i < iColCount - 1)
//{
// HttpContext.Current.Response.Write(",");
//}
}
HttpContext.Current.Response.Write(strb.ToString());
HttpContext.Current.Response.Write(Environment.NewLine);
strb.Remove(0, strb.Length);
}
HttpContext.Current.Response.End();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ClearContent();
return true;
}
catch
{
return false;
}
}
public void ConvertToCSV(DataTable dt, DataTable dt2, string FileName, Boolean showheader)
{
try
{
string attachment = string.Format("attachment; filename={0}", FileName + ".csv");
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.AddHeader("content-disposition", attachment);
HttpContext.Current.Response.Charset = "";
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.ContentType = "text/csv";
HttpContext.Current.Response.AddHeader("Pragma", "public");
HttpContext.Current.Response.Write(Environment.NewLine);
//write gridview details
int iColCount = dt.Columns.Count;
//// First we will write the headers.
if (showheader)
{
AddComma(dt2.Rows[0][0].ToString(), strb);
HttpContext.Current.Response.Write(strb.ToString());
HttpContext.Current.Response.Write(Environment.NewLine);
strb.Remove(0, strb.Length);
for (int i = 0; i < iColCount; i++)
{
AddComma(dt.Columns[i].ToString(), strb);
}
HttpContext.Current.Response.Write(strb.ToString());
HttpContext.Current.Response.Write(Environment.NewLine);
strb.Remove(0, strb.Length);
foreach (DataRow dr in dt.Rows)
{
for (int i = 0; i < iColCount; i++)
{
//if (!Convert.IsDBNull(dr[i]))
//{
AddComma(dr[i].ToString(), strb);
//}
//if (i < iColCount - 1)
//{
// HttpContext.Current.Response.Write(",");
//}
}
HttpContext.Current.Response.Write(strb.ToString());
HttpContext.Current.Response.Write(Environment.NewLine);
strb.Remove(0, strb.Length);
}
}
else
{
AddComma(dt2.Rows[0][0].ToString(), strb);
HttpContext.Current.Response.Write(strb.ToString());
HttpContext.Current.Response.Write(Environment.NewLine);
strb.Remove(0, strb.Length);
foreach (DataRow dr in dt.Rows)
{
for (int i = 0; i < iColCount; i++)
{
//if (!Convert.IsDBNull(dr[i]))
//{
AddComma(dr[i].ToString(), strb);
//}
//if (i < iColCount - 1)
//{
// HttpContext.Current.Response.Write(",");
//}
}
HttpContext.Current.Response.Write(strb.ToString());
HttpContext.Current.Response.Write(Environment.NewLine);
strb.Remove(0, strb.Length);
}
}
HttpContext.Current.Response.End();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ClearContent();
}
catch
{
}
}
public Boolean ConvertToCSV(DataTable dtHeader, DataTable dt, DataTable dt2, string FileName)
{
try
{
string attachment = string.Format("attachment; filename={0}", FileName + ".csv");
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.AddHeader("content-disposition", attachment);
HttpContext.Current.Response.Charset = "";
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.ContentType = "text/csv";
HttpContext.Current.Response.AddHeader("Pragma", "public");
//write header values
int headColCount = dtHeader.Columns.Count;
foreach (DataRow dr in dtHeader.Rows)
{
for (int i = 0; i < headColCount; i++)
{
//if (!Convert.IsDBNull(dr[i]))
//{
AddComma(dr[i].ToString(), strb);
//}
//if (i < iColCount - 1)
//{
// HttpContext.Current.Response.Write(",");
//}
}
HttpContext.Current.Response.Write(strb.ToString());
HttpContext.Current.Response.Write(Environment.NewLine);
strb.Remove(0, strb.Length);
}
//write gridview1 details
int iColCount = dt.Columns.Count;
// First we will write the headers.
for (int i = 0; i < iColCount; i++)
{
//HttpContext.Current.Response.Write(dt.Columns[i].ToString());
AddComma(dt.Columns[i].ToString(), strb);
}
HttpContext.Current.Response.Write(strb.ToString());
HttpContext.Current.Response.Write(Environment.NewLine);
strb.Remove(0, strb.Length);
foreach (DataRow dr in dt.Rows)
{
for (int i = 0; i < iColCount; i++)
{
//if (!Convert.IsDBNull(dr[i]))
//{
AddComma(dr[i].ToString(), strb);
//}
//if (i < iColCount - 1)
//{
// HttpContext.Current.Response.Write(",");
//}
}
HttpContext.Current.Response.Write(strb.ToString());
HttpContext.Current.Response.Write(Environment.NewLine);
strb.Remove(0, strb.Length);
}
HttpContext.Current.Response.Write(Environment.NewLine);
//write gridview2 details
iColCount = 0;
iColCount = dt2.Columns.Count;
// First we will write the headers.
for (int i = 0; i < iColCount; i++)
{
//HttpContext.Current.Response.Write(dt.Columns[i].ToString());
AddComma(dt2.Columns[i].ToString(), strb);
}
HttpContext.Current.Response.Write(strb.ToString());
HttpContext.Current.Response.Write(Environment.NewLine);
strb.Remove(0, strb.Length);
foreach (DataRow dr in dt2.Rows)
{
for (int i = 0; i < iColCount; i++)
{
//if (!Convert.IsDBNull(dr[i]))
//{
AddComma(dr[i].ToString(), strb);
//}
//if (i < iColCount - 1)
//{
// HttpContext.Current.Response.Write(",");
//}
}
HttpContext.Current.Response.Write(strb.ToString());
HttpContext.Current.Response.Write(Environment.NewLine);
strb.Remove(0, strb.Length);
}
HttpContext.Current.Response.End();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ClearContent();
return true;
}
catch
{
return false;
}
}
private static void AddComma(string item, StringBuilder strb)
{
strb.Append(item.Replace(',', ' '));
strb.Append(" ,");
}
public Boolean ConvertToExcel(System.Data.DataTable dtHeader, System.Data.DataTable dt, string FileName)
{
try
{
// string attachment = string.Format("attachment; filename={0}", FileName + ".csv");
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + FileName + ".xls");
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWriter);
DataGrid dataExportExcel = new DataGrid();
StringBuilder sbResponseString = new StringBuilder();
sbResponseString.Append("<html xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns=\"http://www.w3.org/TR/REC-html40\"> <head><meta http-equiv=\"Content-Type\" content=\"text/html;charset=windows-1252\"><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>Test</x:Name><x:WorksheetOptions><x:Panes></x:Panes></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head> <body>");
dataExportExcel.ItemDataBound += new DataGridItemEventHandler(dataGrid_ItemDataBound);
dataExportExcel.DataSource = dtHeader;
dataExportExcel.DataBind();
dataExportExcel.RenderControl(htmlWrite);
dataExportExcel.ItemDataBound += new DataGridItemEventHandler(dataGrid_ItemDataBound);
dataExportExcel.DataSource = dt;
dataExportExcel.DataBind();
dataExportExcel.RenderControl(htmlWrite);
sbResponseString.Append(stringWriter);
sbResponseString.Append("</body></html>");
HttpContext.Current.Response.Write(sbResponseString.ToString());
HttpContext.Current.Response.End();
return true;
}
catch
{
return false;
}
}
public Boolean ConvertToExcel(System.Data.DataTable dt, string FileName)
{
try
{
string attachment = string.Format("attachment; filename={0}", FileName + ".csv");
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + FileName + ".xls");
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWriter);
DataGrid dataExportExcel = new DataGrid();
StringBuilder sbResponseString = new StringBuilder();
sbResponseString.Append("<html xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns=\"http://www.w3.org/TR/REC-html40\"> <head><meta http-equiv=\"Content-Type\" content=\"text/html;charset=windows-1252\"><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>Test</x:Name><x:WorksheetOptions><x:Panes></x:Panes></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head> <body>");
dataExportExcel.ItemDataBound += new DataGridItemEventHandler(dataGrid_ItemDataBound);
dataExportExcel.DataSource = dt;
dataExportExcel.DataBind();
dataExportExcel.RenderControl(htmlWrite);
sbResponseString.Append(stringWriter);
sbResponseString.Append("</body></html>");
HttpContext.Current.Response.Write(sbResponseString.ToString());
//HttpContext.Current.Response.End();
return true;
}
catch(Exception ex)
{
return false;
}
}
private void dataGrid_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Header)
{
//Header Text Format can be done as follows
e.Item.Font.Bold = true;
//Adding Filter/Sorting functionality for the Excel
int cellIndex = 0;
while (cellIndex < e.Item.Cells.Count)
{
// e.Item.Cells[cellIndex].Attributes.Add("x:autofilter", "all");
e.Item.Cells[cellIndex].Width = 200;
e.Item.Cells[cellIndex].BackColor = Color.DarkOrange;
e.Item.Cells[cellIndex].ForeColor = Color.White;
e.Item.Cells[cellIndex].HorizontalAlign = HorizontalAlign.Center;
cellIndex++;
}
}
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
int cellIndex = 0;
while (cellIndex < e.Item.Cells.Count)
{
//Any Cell specific formatting should be done here
e.Item.Cells[cellIndex].HorizontalAlign = HorizontalAlign.Left;
if (e.Item.ItemType == ListItemType.AlternatingItem && e.Item.Cells.Count != 1)
{
e.Item.Cells[cellIndex].BackColor = Color.LightGray;
}
else
{
e.Item.Cells[cellIndex].BackColor = Color.White;
}
cellIndex++;
}
}
if (e.Item.Cells.Count == 1)
{
e.Item.Cells[0].ColumnSpan = 9;
}
}
}
//In .net Click event we call using the following code
public Boolean ExportToData(string formatType, string status)
{
var res = false;
DataTable dt = (DataTable)oExportDataTo.ExportData(status);
createHeader();
SetHeaderName(dt);
if (formatType == "Excel")
res= oEDI.ConvertToExcel(dtHeader,dt, "Report");
else if (formatType == "CSV")
res=oEDI.ConvertToCSV(dt, "Report");
return res;
}
private void createHeader()
{
dtHeader = new DataTable();
string str = "Celestica Internal Users - " + String.Format("{0:dd-MMM-yyyy}", DateTime.Now);
dtHeader.Columns.Add(new DataColumn(str, Type.GetType("System.String")));
DataColumn c = new DataColumn();
DataRow drow = dtHeader.NewRow();
drow = dtHeader.NewRow();
drow[0] = "Generated By : Admin";
dtHeader.Rows.Add(drow);
}
private void SetHeaderName(DataTable dt)
{
dt.Columns.Remove("CLUM_Id");
dt.Columns.Remove("CLUM_SpecialPassword");
dt.Columns.Remove("CLUM_Password");
dt.Columns.Remove("CLUM_FrgtPwd_Ques");
dt.Columns.Remove("CLUM_FrgtPwd_Ans");
dt.Columns.Remove("CLUM_PermittedLogin");
dt.Columns.Remove("CLUM_BuyerCode");
dt.Columns.Remove("CLUM_Notes");
dt.Columns[0].ColumnName = "First Name";
dt.Columns[1].ColumnName = "Last Name";
dt.Columns[2].ColumnName = "Title";
dt.Columns[3].ColumnName = "eMail";
dt.Columns[4].ColumnName = "Phone No";
dt.Columns[5].ColumnName = "Mobile No";
}
No comments:
Post a Comment