export a csv file from a dataTable

Last post 05-27-2007 12:58 AM by M A A Mehedi Hasan. 3 replies.

Sort Posts:

  • export a csv file from a dataTable

    05-26-2007, 5:31 PM

    Hi I have a webpage and it will create get the xmlstring and store in dataTable, and what I want to do is to export to a CSV file, when the button is click.

    I wonder is there anyone can show me an exmaple for me..

    thanks

     

  • Re: export a csv file from a dataTable

    05-26-2007, 6:18 PM
  • Re: export a csv file from a dataTable

    05-26-2007, 11:04 PM

    Hi I did try the example in the link, howwever, it didn't work. When I run the program. It didn't say any error, but i can't find the "test.csv" file. Also the line 12 didn't execute too. So I wonder where did i make wrong? Hope someone can help me

     

    1    if (command.Equals("EXPORT~"))
    2            {
    3                DataSet ds = new DataSet("Record Data Set");
    4                StringReader stream = new StringReader(context);
    5                ds.ReadXml(stream);
    6                DataTable dataTable = ds.Tables[0];
    7                // Export the details of specified columns to Excel
    8                Response.ContentType = "Application/x-msexcel";
    9                Response.AddHeader("content-disposition", "attachment; filename=\"c:\\test.csv\"");
    10               Response.Write((new ExportXMLCSV()).ToCSV(dataTable));
    11               Response.End();
    12               callBackString = "The file is create";
    13           }
    14   }
    15   
    16   [ExportXMLCSV class, I have a function called ToCSV]
    17   public string ToCSV(DataTable dataTable)
    18       {
    19           //create the stringbuilder that would hold our data
    20           StringBuilder sb = new StringBuilder();
    21           //check if there are columns in our datatable
    22           if (dataTable.Columns.Count != 0)
    23           {
    24               //loop thru each of the columns so that we could build the headers
    25               //for each field in our datatable
    26               foreach (DataColumn column in dataTable.Columns)
    27               {
    28                   //append the column name followed by our separator
    29                   sb.Append(column.ColumnName + ',');
    30               }
    31               //append a carriage return
    32               sb.Append("\r\n");
    33               //loop thru each row of our datatable
    34               foreach (DataRow row in dataTable.Rows)
    35               {
    36                   //loop thru each column in our datatable
    37                   foreach (DataColumn column in dataTable.Columns)
    38                   {
    39                       //get the value for tht row on the specified column
    40                       // and append our separator
    41                       sb.Append(row[column].ToString() + ',');
    42                   }
    43                   //append a carriage return
    44                   sb.Append("\r\n");
    45               }
    46           }
    47           //return our values
    48           return sb.ToString();
    49       }
    
     
  • Re: export a csv file from a dataTable

    05-27-2007, 12:58 AM
    Answer
    • Contributor
      7,324 point Contributor
    • M A A Mehedi Hasan
    • Member since 02-05-2007, 7:40 AM
    • Dhaka, Bangladesh
    • Posts 939

    Hello,

    Check this

    http://www.devx.com/vb2themax/Tip/19703 

    Mehedi Hasan


    Mark as answer if the post meets your requirement!
Page 1 of 1 (4 items)