Export to CSV problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • arnabit@gmail.com

    Export to CSV problem

    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

  • =?Utf-8?B?U2VyZ2V5IFBvYmVyZXpvdnNraXk=?=

    #2
    RE: Export to CSV problem

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

    "arnabit@gmail. com" 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(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

    • arnabit@gmail.com

      #3
      Re: Export to CSV problem

      On Jul 19, 12:54 am, Sergey Poberezovskiy
      <SergeyPoberezo vs...@discussio ns.microsoft.co mwrote:
      use vnd.xls instead of vnd.ms-excel in content-disposition
      >
      >
      >
      "arna...@gmail. com" 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(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- Hide quoted text -
      >
      - Show quoted text -
      My problem is solved i have used System.Web.Curr ent.Response.Wr ite()
      instead of Response.Binary Write.It solves my problem

      Comment

      Working...