J
John Straumann
Hi all:
I found an earlier thread that showed creating an Excel file from ASP.NET,
pasted below and works great! However I set the code to create 3 columns and
25 data rows, and it creates a CSV file but when I open the file in Excel
the data shows up in one column thus:
A
Col 1;Col 2; Col3;
0;2;3
1;3;4;
etc.
So all the data is showing up in the first column, separated by ;.
Does anyone know how I could modify the code so I can create separate
columns in Excel?
Thanks for any and all input.
John.
public static void ExportToSpreadsheet(DataTable table, string name)
{
HttpContext context = HttpContext.Current;
context.Response.Clear();
foreach (DataColumn column in table.Columns)
{
context.Response.Write(column.ColumnName + ";");
}
context.Response.Write(Environment.NewLine);
foreach (DataRow row in table.Rows)
{
for (int i = 0; i < table.Columns.Count; i++)
{
context.Response.Write(row.ToString().Replace(";",
string.Empty) + ";");
}
context.Response.Write(Environment.NewLine);
}
context.Response.ContentType = "text/csv";
context.Response.AppendHeader("Content-Disposition", "attachment;
filename=" + name + ".csv");
context.Response.End();
}
I found an earlier thread that showed creating an Excel file from ASP.NET,
pasted below and works great! However I set the code to create 3 columns and
25 data rows, and it creates a CSV file but when I open the file in Excel
the data shows up in one column thus:
A
Col 1;Col 2; Col3;
0;2;3
1;3;4;
etc.
So all the data is showing up in the first column, separated by ;.
Does anyone know how I could modify the code so I can create separate
columns in Excel?
Thanks for any and all input.
John.
public static void ExportToSpreadsheet(DataTable table, string name)
{
HttpContext context = HttpContext.Current;
context.Response.Clear();
foreach (DataColumn column in table.Columns)
{
context.Response.Write(column.ColumnName + ";");
}
context.Response.Write(Environment.NewLine);
foreach (DataRow row in table.Rows)
{
for (int i = 0; i < table.Columns.Count; i++)
{
context.Response.Write(row.ToString().Replace(";",
string.Empty) + ";");
}
context.Response.Write(Environment.NewLine);
}
context.Response.ContentType = "text/csv";
context.Response.AppendHeader("Content-Disposition", "attachment;
filename=" + name + ".csv");
context.Response.End();
}