Ok, in that case Is it possible to accomplish this using the ASHX handler ?
[ i.e. Passwing the CSV data to ASHX handler and write code in ProcessRequest(HttpContext context) to export it into Excel ]
I tried writing one hanlder to export the data, it works fine [When I dont pass any data to it]
Below is the Snippet to export fle to Excel it works fine :
private string ExportToExcelWorking()
{
string _exportContent = "";
using (StringWriter sb = new StringWriter())
{
using (HtmlTextWriter htmlWriter = new HtmlTextWriter(sb))
{
Table table = new Table();
table.GridLines = GridLines.Horizontal;
table.BorderWidth = new Unit(1);
for (int i = 0; i < 100; i++)
{
TableRow row = new TableRow();
row.BackColor = (i % 2 == 0) ? System.Drawing.Color.BlanchedAlmond :
System.Drawing.Color.BurlyWood;
for (int j = 0; j < 5; j++)
{
TableCell cell = new TableCell();
cell.Text = "Row: " + i + " :: Cell:" + j;
row.Cells.Add(cell);
}
table.Rows.Add(row);
}
table.RenderControl(htmlWriter);
_exportContent = sb.ToString();
}
}
return _exportContent;
}
But When I pass my CSV data, it goes into the function but does not export anything into Excel.
Below is the modified snap for my scenario :
public void ProcessRequest(HttpContext context)
{
string key1 = context.Request.Form["csvData"];
string[] rows = key1.Split('|');
string strRows = "";
string[] cells = null;
for (int j = 0; j < rows.Length; j++) //Get column names
{
cells = null;
strRows = rows[j].ToString();
cells = strRows.Split(':');
Header.Add(cells[0].ToString());
if (cells[0].ToString() == "Total")
{
break;
}
}
context.Response.AddHeader("content-disposition", "attachment; filename=excelData.xls");
context.Response.ContentType = "application/ms-excel";
HttpRequest request = context.Request;
HttpResponse response = context.Response;
string exportContent = ExportToExcel();
response.Write(exportContent);
}
private string ExportToExcel()
{
string _exportContent = "";
using (StringWriter sb = new StringWriter())
{
using (HtmlTextWriter htmlWriter = new HtmlTextWriter(sb))
{
Table table = new Table();
table.GridLines = GridLines.Horizontal;
table.BorderWidth = new Unit(1);
for (int i = 0; i < 1; i++)
{
TableRow row = new TableRow();
row.BackColor = (i % 2 == 0) ? System.Drawing.Color.BlanchedAlmond :
System.Drawing.Color.BurlyWood;
foreach (string dc in Header) //Add headers
{
TableCell cell = new TableCell();
cell.Text = dc;
row.Cells.Add(cell);
}
table.Rows.Add(row);
}
table.RenderControl(htmlWriter);
_exportContent = sb.ToString();
}
}
return _exportContent;
}