Hi everyone,
I am facing a problem in exporting CSV file.I able to export it
properly in IE7 bbut in Mozila it seems the file name is changed and
an extra , is added in the file name.say if the file name is
"Report .csv" in mozila it becomes Report .csv," .I am writting the
code below.
private void ExportToCsv(Dat aSet ds)
{
try
{
string header = string.Empty;
string body = string.Empty;
string record = string.Empty;
foreach (DataColumn col in ds.Tables[0].Columns)
{
header = header + (char)34 + col.ColumnName +
(char)34 + ",";
}
header = header.Substrin g(0, header.Length - 1);
foreach (DataRow row in ds.Tables[0].Rows)
{
Object[] arr = row.ItemArray;
for (int i = 0; i < arr.Length - 1; i++)
{
if (arr[i].ToString().Ind exOf(",") 0)
{
record = record + (char)34 +
arr[i].ToString() + (char)34 + ",";
}
else
{
record = record + arr[i].ToString() + ",";
}
}
body = body + record.Substrin g(0, record.Length) +
Environment.New Line;
record = "";
String strData = header + Environment.New Line +
body;
byte[] data =
System.Text.ASC IIEncoding.ASCI I.GetBytes(strD ata);
Response.Clear( );
Response.AddHea der("Content-Type", "text/vnd.ms-
excel");
Response.AddHea der("Content-Disposition",
"attachment;fil ename=report.cs v" );
Response.Binary Write(data);
}
}
catch (Exception bug)
{
string tmp;
tmp = bug.Message;
Response.Redire ct("../PageDenied.aspx ");
}
finally
{
Response.End();
}
}
Thanks in Advance,
Arnab
I am facing a problem in exporting CSV file.I able to export it
properly in IE7 bbut in Mozila it seems the file name is changed and
an extra , is added in the file name.say if the file name is
"Report .csv" in mozila it becomes Report .csv," .I am writting the
code below.
private void ExportToCsv(Dat aSet ds)
{
try
{
string header = string.Empty;
string body = string.Empty;
string record = string.Empty;
foreach (DataColumn col in ds.Tables[0].Columns)
{
header = header + (char)34 + col.ColumnName +
(char)34 + ",";
}
header = header.Substrin g(0, header.Length - 1);
foreach (DataRow row in ds.Tables[0].Rows)
{
Object[] arr = row.ItemArray;
for (int i = 0; i < arr.Length - 1; i++)
{
if (arr[i].ToString().Ind exOf(",") 0)
{
record = record + (char)34 +
arr[i].ToString() + (char)34 + ",";
}
else
{
record = record + arr[i].ToString() + ",";
}
}
body = body + record.Substrin g(0, record.Length) +
Environment.New Line;
record = "";
String strData = header + Environment.New Line +
body;
byte[] data =
System.Text.ASC IIEncoding.ASCI I.GetBytes(strD ata);
Response.Clear( );
Response.AddHea der("Content-Type", "text/vnd.ms-
excel");
Response.AddHea der("Content-Disposition",
"attachment;fil ename=report.cs v" );
Response.Binary Write(data);
}
}
catch (Exception bug)
{
string tmp;
tmp = bug.Message;
Response.Redire ct("../PageDenied.aspx ");
}
finally
{
Response.End();
}
}
Thanks in Advance,
Arnab
Comment