Export to CSV problem

Discussion in 'ASP .Net' started by arnabit@gmail.com, Jul 19, 2007.

  1. Guest

    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
     
    , Jul 19, 2007
    #1
    1. Advertising

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

    "" 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.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
    >
    >
     
    =?Utf-8?B?U2VyZ2V5IFBvYmVyZXpvdnNraXk=?=, Jul 19, 2007
    #2
    1. Advertising

  3. Guest

    On Jul 19, 12:54 am, Sergey Poberezovskiy
    <> wrote:
    > use vnd.xls instead of vnd.ms-excel in content-disposition
    >
    >
    >
    > "" 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.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
     
    , Jul 20, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JP

    Export Data to .CSV file

    JP, Jul 3, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    3,247
    dave wanta
    Jul 3, 2003
  2. =?Utf-8?B?RGF2aWQgVmFsbGU=?=

    Invalid export DLL or export format

    =?Utf-8?B?RGF2aWQgVmFsbGU=?=, Oct 29, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    3,626
    =?Utf-8?B?RGF2aWQgVmFsbGU=?=
    Oct 29, 2003
  3. VB Programmer
    Replies:
    1
    Views:
    428
    VB Programmer
    Jul 31, 2004
  4. =?Utf-8?B?U2Ft?=

    Export a Web Page to .XML or CSV

    =?Utf-8?B?U2Ft?=, Oct 12, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    1,333
    Diego
    Oct 12, 2004
  5. Maarten Porters
    Replies:
    1
    Views:
    432
    Florian Gilcher
    Jul 28, 2008
Loading...

Share This Page