将DataSet导出为CSV格式
导出DataSet所有的列到CSV格式
遍历DataSet的Tables
遍历Table的Rows
遍历Rows的Columns
使用示例:
Export.SetExport(ds.供应商列表视图,dg.Columns);
string data = Export.ExportCSV(ds);
string temp = string.Format("attachment;filename={0}","ExportData.csv");
Response.ClearHeaders();
Response.AppendHeader("Content-disposition", temp);
Response.Write(data);
Response.End();
遍历DataSet的Tables
遍历Table的Rows
遍历Rows的Columns
1/// <summary>
2/// 将DataSet导出成CSV格式
3/// </summary>
4/// <param name="ds">DataSet</param>
5/// <returns>CSV字符串数据</returns>
6public static string ExportCSV(DataSet ds)
7{
8 string data = "";
9 //data = ds.DataSetName + "\n";
10
11 foreach(DataTable tb in ds.Tables)
12 {
13 data += tb.TableName + "\n";
14
15 //写出列名
16 foreach (DataColumn column in tb.Columns)
17 {
18 data += column.ColumnName + ",";
19 }
20 data += "\n";
21
22 //写出数据
23 foreach (DataRow row in tb.Rows)
24 {
25 foreach (DataColumn column in tb.Columns)
26 {
27 data += row[column].ToString() + ",";
28 }
29 data += "\n";
30 }
31 data += "\n";
32 }
33
34 return data;
35}
36
2/// 将DataSet导出成CSV格式
3/// </summary>
4/// <param name="ds">DataSet</param>
5/// <returns>CSV字符串数据</returns>
6public static string ExportCSV(DataSet ds)
7{
8 string data = "";
9 //data = ds.DataSetName + "\n";
10
11 foreach(DataTable tb in ds.Tables)
12 {
13 data += tb.TableName + "\n";
14
15 //写出列名
16 foreach (DataColumn column in tb.Columns)
17 {
18 data += column.ColumnName + ",";
19 }
20 data += "\n";
21
22 //写出数据
23 foreach (DataRow row in tb.Rows)
24 {
25 foreach (DataColumn column in tb.Columns)
26 {
27 data += row[column].ToString() + ",";
28 }
29 data += "\n";
30 }
31 data += "\n";
32 }
33
34 return data;
35}
36
DataSet与DataGrid问题
一般情况下我们用DataGrid显示DataSet时,只显示我们需要的列,并不显示DataSet所有的列,所以导出成CSV格式的时候可能只需要导出DataGrid中的列。
本文解决方法:
将DataSet中需要导出的列做上标记,在导出时只将有标记的列导出。
代码
1/// <summary>
2/// 标记DataColumn为接受导出的
3/// </summary>
4/// <param name="column">DataColumn</param>
5public static void SetExport(DataColumn column)
6{
7 if (column != null)
8 {
9 if (column.ExtendedProperties["IsExport"] == null)
10 column.ExtendedProperties.Add("IsExport", "true");
11 else
12 column.ExtendedProperties["IsExport"] = "true";
13 }
14}
15
16/// <summary>
17/// 标记DataTable中的一些列为接受导出的
18/// </summary>
19/// <param name="tb">DataTable</param>
20/// <param name="columns">列</param>
21public static void SetExport(DataTable tb,params string[] columns)
22{
23 foreach(string column in columns)
24 {
25 SetExport(tb.Columns[column]);
26 }
27}
28
29/// <summary>
30/// 标记DataTable中的一些列为接受导出的
31/// </summary>
32/// <param name="tb">DataTable</param>
33/// <param name="columns">DataGrid的列</param>
34public static void SetExport(DataTable tb,DataGridColumnCollection columns)
35{
36 foreach(DataGridColumn column in columns)
37 {
38 if (column.GetType().Name == "BoundColumn" ¦ ¦ column.GetType().Name == "HyperLinkColumn" ¦ ¦ column.GetType().Name == "TemplateColumn")
39 SetExport(tb.Columns[column.HeaderText]);
40 }
41}
42
43/// <summary>
44/// 判断DataColumn是否为接受导出的
45/// </summary>
46/// <param name="column">DataColumn</param>
47/// <returns>bool型的结果</returns>
48public static bool IsExport(DataColumn column)
49{
50 if (column.ExtendedProperties["IsExport"] != null && column.ExtendedProperties["IsExport"].ToString().Trim().ToLower() == "true")
51 return true;
52 else
53 return false;
54}
55
2/// 标记DataColumn为接受导出的
3/// </summary>
4/// <param name="column">DataColumn</param>
5public static void SetExport(DataColumn column)
6{
7 if (column != null)
8 {
9 if (column.ExtendedProperties["IsExport"] == null)
10 column.ExtendedProperties.Add("IsExport", "true");
11 else
12 column.ExtendedProperties["IsExport"] = "true";
13 }
14}
15
16/// <summary>
17/// 标记DataTable中的一些列为接受导出的
18/// </summary>
19/// <param name="tb">DataTable</param>
20/// <param name="columns">列</param>
21public static void SetExport(DataTable tb,params string[] columns)
22{
23 foreach(string column in columns)
24 {
25 SetExport(tb.Columns[column]);
26 }
27}
28
29/// <summary>
30/// 标记DataTable中的一些列为接受导出的
31/// </summary>
32/// <param name="tb">DataTable</param>
33/// <param name="columns">DataGrid的列</param>
34public static void SetExport(DataTable tb,DataGridColumnCollection columns)
35{
36 foreach(DataGridColumn column in columns)
37 {
38 if (column.GetType().Name == "BoundColumn" ¦ ¦ column.GetType().Name == "HyperLinkColumn" ¦ ¦ column.GetType().Name == "TemplateColumn")
39 SetExport(tb.Columns[column.HeaderText]);
40 }
41}
42
43/// <summary>
44/// 判断DataColumn是否为接受导出的
45/// </summary>
46/// <param name="column">DataColumn</param>
47/// <returns>bool型的结果</returns>
48public static bool IsExport(DataColumn column)
49{
50 if (column.ExtendedProperties["IsExport"] != null && column.ExtendedProperties["IsExport"].ToString().Trim().ToLower() == "true")
51 return true;
52 else
53 return false;
54}
55
使用示例:
Export.SetExport(ds.供应商列表视图,dg.Columns);
string data = Export.ExportCSV(ds);
string temp = string.Format("attachment;filename={0}","ExportData.csv");
Response.ClearHeaders();
Response.AppendHeader("Content-disposition", temp);
Response.Write(data);
Response.End();