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 }