1 /// <summary>
2 /// 从DataSet导出到CSV文件中
3 /// </summary>
4 /// <param name="dsData">需要导出的数据集合</param>
5 /// <param name="strFileName">CSV文件路径</param>
6 public static void OutputToCSV(DataSet dsData,string strFileName)
7 {
8 DataTable dtTable = dsData.Tables[0];
9 //写入文件
10 StreamWriter sw = new StreamWriter(strFileName,false,System.Text.Encoding.Default);
11
12 foreach(DataColumn dcColumn in dtTable.Columns)
13 {
14 sw.Write(dcColumn.ColumnName);
15 if (dcColumn.ColumnName != dtTable.Columns[dtTable.Columns.Count - 1].ColumnName)
16 {
17 sw.Write(",");
18 }
19 }
20 sw.Write("\n");
21 foreach(DataRow drRow in dtTable.Rows)
22 {
23 foreach(DataColumn dcColumn in dtTable.Columns)
24 {
25 if (dcColumn.DataType == Type.GetType("System.String"))
26 {
27 sw.Write(drRow[dcColumn.ColumnName].ToString());
28 }
29 if (dcColumn.DataType == Type.GetType("System.Decimal") ||
30 dcColumn.DataType == Type.GetType("System.Double") ||
31 dcColumn.DataType == Type.GetType("System.Single"))
32 {
33 string strWrite = string.Format("{0:F6}",drRow[dcColumn.ColumnName]);
34 sw.Write(strWrite);
35 }
36 if (dcColumn.DataType == Type.GetType("System.DateTime"))
37 {
38 DateTime dtTime = System.Convert.ToDateTime(drRow[dcColumn.ColumnName]);
39 sw.Write(dtTime.ToShortDateString());
40 }
41 if (dcColumn.DataType == Type.GetType("System.Boolean"))
42 {
43 sw.Write(drRow[dcColumn.ColumnName].ToString());
44 }
45 if (dcColumn.DataType == Type.GetType("System.Int16") ||
46 dcColumn.DataType == Type.GetType("System.Int32") ||
47 dcColumn.DataType == Type.GetType("System.Int64"))
48 {
49 sw.Write(drRow[dcColumn.ColumnName].ToString());
50 }
51 if (dcColumn.ColumnName != dtTable.Columns[dtTable.Columns.Count - 1].ColumnName)
52 {
53 sw.Write(",");
54 }
55 }
56 sw.Write("\n");
57 }
58 sw.Close();
59 }
2 /// 从DataSet导出到CSV文件中
3 /// </summary>
4 /// <param name="dsData">需要导出的数据集合</param>
5 /// <param name="strFileName">CSV文件路径</param>
6 public static void OutputToCSV(DataSet dsData,string strFileName)
7 {
8 DataTable dtTable = dsData.Tables[0];
9 //写入文件
10 StreamWriter sw = new StreamWriter(strFileName,false,System.Text.Encoding.Default);
11
12 foreach(DataColumn dcColumn in dtTable.Columns)
13 {
14 sw.Write(dcColumn.ColumnName);
15 if (dcColumn.ColumnName != dtTable.Columns[dtTable.Columns.Count - 1].ColumnName)
16 {
17 sw.Write(",");
18 }
19 }
20 sw.Write("\n");
21 foreach(DataRow drRow in dtTable.Rows)
22 {
23 foreach(DataColumn dcColumn in dtTable.Columns)
24 {
25 if (dcColumn.DataType == Type.GetType("System.String"))
26 {
27 sw.Write(drRow[dcColumn.ColumnName].ToString());
28 }
29 if (dcColumn.DataType == Type.GetType("System.Decimal") ||
30 dcColumn.DataType == Type.GetType("System.Double") ||
31 dcColumn.DataType == Type.GetType("System.Single"))
32 {
33 string strWrite = string.Format("{0:F6}",drRow[dcColumn.ColumnName]);
34 sw.Write(strWrite);
35 }
36 if (dcColumn.DataType == Type.GetType("System.DateTime"))
37 {
38 DateTime dtTime = System.Convert.ToDateTime(drRow[dcColumn.ColumnName]);
39 sw.Write(dtTime.ToShortDateString());
40 }
41 if (dcColumn.DataType == Type.GetType("System.Boolean"))
42 {
43 sw.Write(drRow[dcColumn.ColumnName].ToString());
44 }
45 if (dcColumn.DataType == Type.GetType("System.Int16") ||
46 dcColumn.DataType == Type.GetType("System.Int32") ||
47 dcColumn.DataType == Type.GetType("System.Int64"))
48 {
49 sw.Write(drRow[dcColumn.ColumnName].ToString());
50 }
51 if (dcColumn.ColumnName != dtTable.Columns[dtTable.Columns.Count - 1].ColumnName)
52 {
53 sw.Write(",");
54 }
55 }
56 sw.Write("\n");
57 }
58 sw.Close();
59 }
如果大家对CSV文件不是很了解的话,可以参考本博客中的另外一篇文章:http://fengjun19912.cnblogs.com/archive/2006/03/07/344787.html;
以上例子进行了简单化处理,将数据集合DataSet里只存在DataTable一个,多个情况完全类似;
作者:冯珺;
时间:2006-03-07