1
/**//// <summary>
2
/// 导出DataSet到客户端
3
/// </summary>
4
/// <param name="dsResults">源DataSet</param>
5
/// <param name="enExport">导出类型</param>
6
/// <param name="strColDelim">列字符</param>
7
/// <param name="strRowDelim">行字符</param>
8
/// <param name="strFileName">导出文件名</param> 9
public static void ExportDataSet(DataSet dsResults , ExportFormat enExport,string strColDelim, string strRowDelim, string strFileName)
10
{
11
DataGrid dgExport = new DataGrid();
12
dgExport.AllowPaging =false;
13
dgExport.DataSource =dsResults;
14
dsResults.DataSetName ="NewDataSet";
15
dgExport.DataMember = dsResults.Tables[0].TableName;
16
dgExport.DataBind();
17
System.Web.HttpContext.Current.Response.Clear();
18
System.Web.HttpContext.Current.Response.Buffer= true;
19
System.Web.HttpContext.Current.Response.ContentEncoding = Encoding.GetEncoding("gb2312");
20
System.Web.HttpContext.Current.Response.Charset = "";
21
System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" +strFileName );
22
switch(enExport.ToString().ToLower())
23
{
24
case "xls":
25
{
26
System.Web.HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
27
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
28
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
29
dgExport.RenderControl(oHtmlTextWriter);
30
System.Web.HttpContext.Current.Response.Write(oStringWriter.ToString());
31
break;
32
}
33
case "custom":
34
{
35
string strText;
36
System.Web.HttpContext.Current.Response.ContentType = "text/txt";
37
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
38
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
39
dgExport.RenderControl(oHtmlTextWriter);
40
strText = oStringWriter.ToString();
41
strText = ParseToDelim(strText ,strRowDelim,strColDelim);
42
System.Web.HttpContext.Current.Response.Write(strText);
43
44
break;
45
}
46
case "csv":
47
{
48
string strText;
49
strRowDelim = System.Environment.NewLine;
50
strColDelim = ",";
51
System.Web.HttpContext.Current.Response.ContentType = "text/txt";
52
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
53
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
54
dgExport.RenderControl(oHtmlTextWriter);
55
strText = oStringWriter.ToString();
56
strText = ParseToDelim(strText ,strRowDelim,strColDelim);
57
System.Web.HttpContext.Current.Response.Write(strText);
58
break;
59
}
60
case "tsv":
61
{
62
string strText;
63
strRowDelim = System.Environment.NewLine;
64
strColDelim = "\t";
65
System.Web.HttpContext.Current.Response.ContentType = "text/txt";
66
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
67
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
68
dgExport.RenderControl(oHtmlTextWriter);
69
strText = oStringWriter.ToString();
70
strText = ParseToDelim(strText ,strRowDelim,strColDelim);
71
System.Web.HttpContext.Current.Response.Write(strText);
72
break;
73
}
74
case "xml":
75
{
76
System.Web.HttpContext.Current.Response.ContentType = "text/xml";
77
System.Web.HttpContext.Current.Response.Write(dsResults.GetXml());
78
break;
79
}
80
case "htm":
81
{
82
System.Web.HttpContext.Current.Response.ContentType = "text/html";
83
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
84
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
85
dgExport.RenderControl(oHtmlTextWriter);
86
System.Web.HttpContext.Current.Response.Write(oStringWriter.ToString());
87
break;
88
}
89
}
90
System.Web.HttpContext.Current.Response.End ();
91
}
92
93
"Export To a Delim Format"#region "Export To a Delim Format"
94
public static string ParseToDelim(string strText, string strRowDelim , string strColDelim)
95
{
96
Regex objReg = new Regex(@"(>\s+<)",RegexOptions.IgnoreCase);
97
strText = objReg.Replace(strText,"><");
98
strText = strText.Replace(System.Environment.NewLine,"");
99
strText = strText.Replace("</td></tr><tr><td>",strRowDelim);
100
strText = strText.Replace("</td><td>",strColDelim);
101
objReg = new Regex(@"<[^>]*>",RegexOptions.IgnoreCase);
102
strText = objReg.Replace(strText,"");
103
strText = System.Web.HttpUtility.HtmlDecode(strText);
104
return strText;
105
}
106
#endregion
107
}
108
109
110
111
public enum ExportFormat
112
{
113
XML,
114
XLS,
115
HTML,
116
CSV,
117
CUSTOM,
118
TSV
119
}
转载于:https://www.cnblogs.com/lx0831/archive/2008/12/12/1353934.html