asp.net导出excel
下面这种方法只能导出页面的Html至Excel:
1 public static void GridExportExcel(ref System.Web.UI.WebControls.GridView dg, string fileName)
2 {
3 System.Web.HttpContext.Current.Response.Clear();
4 System.Web.HttpContext.Current.Response.Buffer = true;
5 System.Web.HttpContext.Current.Response.Charset = "GB2312"; //设置了类型为中文防止乱码的出现
6 System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls"); //定义输出文件和文件名
7 System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
8 System.Web.HttpContext.Current.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
9 dg.Page.EnableViewState = false;
10 System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
11 System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
12 System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
13 dg.RenderControl(oHtmlTextWriter);
14 System.Web.HttpContext.Current.Response.Write(oStringWriter.ToString());
15 System.Web.HttpContext.Current.Response.End();
16 }
2 {
3 System.Web.HttpContext.Current.Response.Clear();
4 System.Web.HttpContext.Current.Response.Buffer = true;
5 System.Web.HttpContext.Current.Response.Charset = "GB2312"; //设置了类型为中文防止乱码的出现
6 System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls"); //定义输出文件和文件名
7 System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
8 System.Web.HttpContext.Current.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
9 dg.Page.EnableViewState = false;
10 System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
11 System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
12 System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
13 dg.RenderControl(oHtmlTextWriter);
14 System.Web.HttpContext.Current.Response.Write(oStringWriter.ToString());
15 System.Web.HttpContext.Current.Response.End();
16 }
当要从DataSet导出至Excel,则用如下方法:
1 protected void btn_out_Click(object sender, EventArgs e)
2 {
3 DataSet ds;
4 if ((bool)ViewState["PageIndexChange"] == false)
5 {
6 Response.Write("<script>alert('对不起,没有查询到任何记录,不能导出数据')</script>");
7 return;
8 }
9 if ((int)Session["userTeam"] > 0)
10 {
11 if (ViewState["toolTip"].ToString().CompareTo("部门") == 0)
12 {
13 ds = rd.GetDeptAttenInfo((string)Session["enterId"], (string)ViewState["strValue"],
14 (DateTime)ViewState["FromTime"], (DateTime)ViewState["ToTime"], (int)ViewState["ShiftResult"], (int)ViewState["DeptChk"], (int)ViewState["AdminChk"]);
15 }
16 else
17 {
18 ds = rd.GetUserAttenInfo(int.Parse(ViewState["strValue"].ToString()),
19 (DateTime)ViewState["FromTime"], (DateTime)ViewState["ToTime"], (int)ViewState["ShiftResult"], (int)ViewState["DeptChk"], (int)ViewState["AdminChk"]);
20 }
21 }
22 else
23 {
24 ds = rd.GetUserAttenInfo(int.Parse(ViewState["strValue"].ToString()),
25 (DateTime)ViewState["FromTime"], (DateTime)ViewState["ToTime"], (int)ViewState["ShiftResult"], (int)ViewState["DeptChk"], (int)ViewState["AdminChk"]);
26 }
27 CreateExcel(ds, "1", "outExcel");
28 //if (this.gvUser.Rows[0].Cells[0].Text == "" || this.gvUser.Rows[0].Cells[0].Text == "没有查到数据")
29 //{
30 // Response.Write("<script>alert('对不起,没有查询到任何记录,不能导出数据')</script>");
31
32 //}
33 //else
34 //{
35 // GridExportExcel(ref this.gvUser, "刷卡记录");
36 //}
37 }
38 public void CreateExcel(DataSet ds, string typeid, string FileName)
39 {
40 HttpResponse resp;
41 resp = Page.Response;
42 resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
43 resp.AppendHeader("Content-Disposition", "attachment;filename=" + FileName + ".xls");
44 resp.ContentType = "application/ms-excel";
45 string colHeaders = "", ls_item = "";
46
47 //定义表对象与行对像,同时用DataSet对其值进行初始化
48 DataTable dt = ds.Tables[0];
49 DataRow[] myRow = dt.Select("");
50 // typeid=="1"时导出为EXCEL格式文件;typeid=="2"时导出为XML格式文件
51 if (typeid == "1")
52 {
53 //取得数据表各列标题,各标题之间以\t分割,最后一个列标题后加回车符
54 //for (i = 0; colHeaders += dt.Columns[i].Caption.ToString() + "\t";
55 //colHeaders += dt.Columns[i].Caption.ToString() + "\n")
56 // //向HTTP输出流中写入取得的数据信息
57 // resp.Write(colHeaders);
58 //逐行处理数据
59 for (int i = 0; i < gvUser.Columns.Count; i++)
60 {
61 if (i != 0 && i != 13 && i != 14 && i != 15)
62 {
63 ls_item += this.gvUser.Columns[i].HeaderText + "\t";
64 //ls_item += row[i].ToString() + "\n";
65 //当前行数据写入HTTP输出流,并且置空ls_item以便下行数据
66 }
67 }
68 ls_item = ls_item.TrimEnd(new char[] { '\t' });
69 ls_item += "\n";
70 resp.Write(ls_item);
71 ls_item = "";
72 foreach (DataRow row in myRow)
73 {
74 //在当前行中,逐列获得数据,数据之间以\t分割,结束时加回车符\n
75 for (int i = 0; i < gvUser.Columns.Count; i++)
76 {
77 if (i != 0 && i != 13 && i != 14 && i != 15)
78 {
79 if (i == 3)
80 {
81 ls_item += ((DateTime)row[((BoundField)this.gvUser.Columns[i]).DataField]).ToString("yyyy年MM月dd日") + "\t";
82 }
83 else
84 {
85 ls_item += row[((BoundField)this.gvUser.Columns[i]).DataField].ToString() + "\t";//this.gvUser.Columns[i].HeaderText
86 }
87 }
88 }
89 ls_item = ls_item.TrimEnd(new char[] { '\t' });
90 ls_item += "\n";
91 resp.Write(ls_item);
92 ls_item = "";
93 }
94 }
95 //else
96 //{
97 // if (typeid == "2")
98 // {
99 // //从DataSet中直接导出XML数据并且写到HTTP输出流中
100 // resp.Write(ds.GetXml());
101 // }
102 //}
103 //写缓冲区中的数据到HTTP头文件中
104 resp.End();
105 }
2 {
3 DataSet ds;
4 if ((bool)ViewState["PageIndexChange"] == false)
5 {
6 Response.Write("<script>alert('对不起,没有查询到任何记录,不能导出数据')</script>");
7 return;
8 }
9 if ((int)Session["userTeam"] > 0)
10 {
11 if (ViewState["toolTip"].ToString().CompareTo("部门") == 0)
12 {
13 ds = rd.GetDeptAttenInfo((string)Session["enterId"], (string)ViewState["strValue"],
14 (DateTime)ViewState["FromTime"], (DateTime)ViewState["ToTime"], (int)ViewState["ShiftResult"], (int)ViewState["DeptChk"], (int)ViewState["AdminChk"]);
15 }
16 else
17 {
18 ds = rd.GetUserAttenInfo(int.Parse(ViewState["strValue"].ToString()),
19 (DateTime)ViewState["FromTime"], (DateTime)ViewState["ToTime"], (int)ViewState["ShiftResult"], (int)ViewState["DeptChk"], (int)ViewState["AdminChk"]);
20 }
21 }
22 else
23 {
24 ds = rd.GetUserAttenInfo(int.Parse(ViewState["strValue"].ToString()),
25 (DateTime)ViewState["FromTime"], (DateTime)ViewState["ToTime"], (int)ViewState["ShiftResult"], (int)ViewState["DeptChk"], (int)ViewState["AdminChk"]);
26 }
27 CreateExcel(ds, "1", "outExcel");
28 //if (this.gvUser.Rows[0].Cells[0].Text == "" || this.gvUser.Rows[0].Cells[0].Text == "没有查到数据")
29 //{
30 // Response.Write("<script>alert('对不起,没有查询到任何记录,不能导出数据')</script>");
31
32 //}
33 //else
34 //{
35 // GridExportExcel(ref this.gvUser, "刷卡记录");
36 //}
37 }
38 public void CreateExcel(DataSet ds, string typeid, string FileName)
39 {
40 HttpResponse resp;
41 resp = Page.Response;
42 resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
43 resp.AppendHeader("Content-Disposition", "attachment;filename=" + FileName + ".xls");
44 resp.ContentType = "application/ms-excel";
45 string colHeaders = "", ls_item = "";
46
47 //定义表对象与行对像,同时用DataSet对其值进行初始化
48 DataTable dt = ds.Tables[0];
49 DataRow[] myRow = dt.Select("");
50 // typeid=="1"时导出为EXCEL格式文件;typeid=="2"时导出为XML格式文件
51 if (typeid == "1")
52 {
53 //取得数据表各列标题,各标题之间以\t分割,最后一个列标题后加回车符
54 //for (i = 0; colHeaders += dt.Columns[i].Caption.ToString() + "\t";
55 //colHeaders += dt.Columns[i].Caption.ToString() + "\n")
56 // //向HTTP输出流中写入取得的数据信息
57 // resp.Write(colHeaders);
58 //逐行处理数据
59 for (int i = 0; i < gvUser.Columns.Count; i++)
60 {
61 if (i != 0 && i != 13 && i != 14 && i != 15)
62 {
63 ls_item += this.gvUser.Columns[i].HeaderText + "\t";
64 //ls_item += row[i].ToString() + "\n";
65 //当前行数据写入HTTP输出流,并且置空ls_item以便下行数据
66 }
67 }
68 ls_item = ls_item.TrimEnd(new char[] { '\t' });
69 ls_item += "\n";
70 resp.Write(ls_item);
71 ls_item = "";
72 foreach (DataRow row in myRow)
73 {
74 //在当前行中,逐列获得数据,数据之间以\t分割,结束时加回车符\n
75 for (int i = 0; i < gvUser.Columns.Count; i++)
76 {
77 if (i != 0 && i != 13 && i != 14 && i != 15)
78 {
79 if (i == 3)
80 {
81 ls_item += ((DateTime)row[((BoundField)this.gvUser.Columns[i]).DataField]).ToString("yyyy年MM月dd日") + "\t";
82 }
83 else
84 {
85 ls_item += row[((BoundField)this.gvUser.Columns[i]).DataField].ToString() + "\t";//this.gvUser.Columns[i].HeaderText
86 }
87 }
88 }
89 ls_item = ls_item.TrimEnd(new char[] { '\t' });
90 ls_item += "\n";
91 resp.Write(ls_item);
92 ls_item = "";
93 }
94 }
95 //else
96 //{
97 // if (typeid == "2")
98 // {
99 // //从DataSet中直接导出XML数据并且写到HTTP输出流中
100 // resp.Write(ds.GetXml());
101 // }
102 //}
103 //写缓冲区中的数据到HTTP头文件中
104 resp.End();
105 }