Export to CSV problem

A

arnabit

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(DataSet 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.Substring(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.ToString().IndexOf(",") > 0)
{
record = record + (char)34 +
arr.ToString() + (char)34 + ",";
}
else
{
record = record + arr.ToString() + ",";
}
}


body = body + record.Substring(0, record.Length) +
Environment.NewLine;
record = "";

String strData = header + Environment.NewLine +
body;
byte[] data =
System.Text.ASCIIEncoding.ASCII.GetBytes(strData);


Response.Clear();
Response.AddHeader("Content-Type", "text/vnd.ms-
excel");
Response.AddHeader("Content-Disposition",
"attachment;filename=report.csv" );
Response.BinaryWrite(data);
}
}
catch (Exception bug)
{
string tmp;
tmp = bug.Message;
Response.Redirect("../PageDenied.aspx");
}
finally
{
Response.End();

}


}

Thanks in Advance,
Arnab
 
A

arnabit

use vnd.xls instead of vnd.ms-excel in content-disposition



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(DataSet 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.Substring(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.ToString().IndexOf(",") > 0)
{
record = record + (char)34 +
arr.ToString() + (char)34 + ",";
}
else
{
record = record + arr.ToString() + ",";
}
}

body = body + record.Substring(0, record.Length) +
Environment.NewLine;
record = "";
String strData = header + Environment.NewLine +
body;
byte[] data =
System.Text.ASCIIEncoding.ASCII.GetBytes(strData);
Response.Clear();
Response.AddHeader("Content-Type", "text/vnd.ms-
excel");
Response.AddHeader("Content-Disposition",
"attachment;filename=report.csv" );
Response.BinaryWrite(data);
}
}
catch (Exception bug)
{
string tmp;
tmp = bug.Message;
Response.Redirect("../PageDenied.aspx");
}
finally
{
Response.End();


Thanks in Advance,
Arnab- Hide quoted text -

- Show quoted text -


My problem is solved i have used System.Web.Current.Response.Write()
instead of Response.BinaryWrite.It solves my problem
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top