asp.net导出数据到Excel
Code
1 Sql数据列表导出到EXCEL#region Sql数据列表导出到EXCEL
2 /**////<summary>
3 ///<param name="queryStr">sql语句</param>
4 /// <param name="connectionString">l数据库连接</param>
5 /// <param name="ExcelFileName">Excel文件名</param>
6 ///</summary>
7 public static bool ToExcel(string queryStr,string ExcelFileName)
8 {
9 // TODO: implement
10 DataSet ds = new DataSet();
11 ds=SqlHelper.ExecuteDataset(SqlHelper.conn, CommandType.Text, queryStr);
12 //queryStr语句生成DataSet
13
14 DataTable dt = new DataTable();
15 dt=ds.Tables[0];
16
17 HttpContext context =HttpContext.Current;
18 context.Response.Clear();
19 context.Response.Buffer = true;
20 context.Response.Charset = "GB2312";
21
22 System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
23 //设置区域性信息
24 StringWriter sw = new StringWriter(myCItrad);
25 //定义字符串写入流对象
26
27 string header = "";
28
29 foreach (DataColumn column in dt.Columns)
30 {
31 header += " " + column.Caption.ToString() +"\t";
32 }
33
34 if (header != "")
35 header = header.Remove(0, 1) + " ";
36 sw.WriteLine(header);
37
38 foreach (DataRow dr in dt.Rows)
39 {
40 string record = "";
41 for (int i = 0; i < dt.Columns.Count; i++)
42 {
43 record += " " + dr[i].ToString().Replace(" ", " ").ToString() + "\t";
44 }
45
46 if (record != "")
47 record = record.Remove(0, 1) + " ";
48 sw.WriteLine(record);
49 }
50 sw.Close();
51 context.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(ExcelFileName, System.Text.Encoding.UTF8) + ".xls");
52 context.Response.ContentType = "application/ms-excel";
53 context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
54 context.Response.Write(sw);
55 context.Response.End();
56 return true;
57 }
58 #endregion
1 Sql数据列表导出到EXCEL#region Sql数据列表导出到EXCEL
2 /**////<summary>
3 ///<param name="queryStr">sql语句</param>
4 /// <param name="connectionString">l数据库连接</param>
5 /// <param name="ExcelFileName">Excel文件名</param>
6 ///</summary>
7 public static bool ToExcel(string queryStr,string ExcelFileName)
8 {
9 // TODO: implement
10 DataSet ds = new DataSet();
11 ds=SqlHelper.ExecuteDataset(SqlHelper.conn, CommandType.Text, queryStr);
12 //queryStr语句生成DataSet
13
14 DataTable dt = new DataTable();
15 dt=ds.Tables[0];
16
17 HttpContext context =HttpContext.Current;
18 context.Response.Clear();
19 context.Response.Buffer = true;
20 context.Response.Charset = "GB2312";
21
22 System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
23 //设置区域性信息
24 StringWriter sw = new StringWriter(myCItrad);
25 //定义字符串写入流对象
26
27 string header = "";
28
29 foreach (DataColumn column in dt.Columns)
30 {
31 header += " " + column.Caption.ToString() +"\t";
32 }
33
34 if (header != "")
35 header = header.Remove(0, 1) + " ";
36 sw.WriteLine(header);
37
38 foreach (DataRow dr in dt.Rows)
39 {
40 string record = "";
41 for (int i = 0; i < dt.Columns.Count; i++)
42 {
43 record += " " + dr[i].ToString().Replace(" ", " ").ToString() + "\t";
44 }
45
46 if (record != "")
47 record = record.Remove(0, 1) + " ";
48 sw.WriteLine(record);
49 }
50 sw.Close();
51 context.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(ExcelFileName, System.Text.Encoding.UTF8) + ".xls");
52 context.Response.ContentType = "application/ms-excel";
53 context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
54 context.Response.Write(sw);
55 context.Response.End();
56 return true;
57 }
58 #endregion