Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Export to CSV problem

Reply
Thread Tools

Export to CSV problem

 
 
arnabit@gmail.com
Guest
Posts: n/a
 
      07-19-2007
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[i].ToString().IndexOf(",") > 0)
{
record = record + (char)34 +
arr[i].ToString() + (char)34 + ",";
}
else
{
record = record + arr[i].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

 
Reply With Quote
 
 
 
 
=?Utf-8?B?U2VyZ2V5IFBvYmVyZXpvdnNraXk=?=
Guest
Posts: n/a
 
      07-19-2007
use vnd.xls instead of vnd.ms-excel in content-disposition

"(E-Mail Removed)" wrote:

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

 
Reply With Quote
 
 
 
 
arnabit@gmail.com
Guest
Posts: n/a
 
      07-20-2007
On Jul 19, 12:54 am, Sergey Poberezovskiy
<(E-Mail Removed)> wrote:
> use vnd.xls instead of vnd.ms-excel in content-disposition
>
>
>
> "(E-Mail Removed)" wrote:
> > 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[i].ToString().IndexOf(",") > 0)
> > {
> > record = record + (char)34 +
> > arr[i].ToString() + (char)34 + ",";
> > }
> > else
> > {
> > record = record + arr[i].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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Export to PDF with google maps + msacces export Maarten Porters Ruby 1 07-28-2008 01:45 PM
Export a Web Page to .XML or CSV =?Utf-8?B?U2Ft?= ASP .Net 1 10-12-2004 11:35 AM
Question: CSV export file displayed in Datagrid or Table - How? VB Programmer ASP .Net 1 07-30-2004 11:14 PM
Invalid export DLL or export format =?Utf-8?B?RGF2aWQgVmFsbGU=?= ASP .Net 0 10-29-2003 11:46 AM
Export Data to .CSV file JP ASP .Net 3 07-03-2003 02:57 PM



Advertisments