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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
38
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
39
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
40
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
42
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
43
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
45
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
47
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
48
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
49
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
50
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
51
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
52
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
53
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
54
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
55
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
56
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
57
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
58
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
59
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
60
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
61
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
62
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
63
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
64
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
65
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
66
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
67
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
68
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
69
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
70
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
71
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
72
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
73
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
74
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
75
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
76
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
77
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
78
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
79
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
80
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
81
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
82
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
83
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
84
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
85
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
86
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
87
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
88
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
89
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
90
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
91
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
92
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
93
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
94
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
95
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
96
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
97
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
98
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
99
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
100
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
101
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
102
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
103
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
104
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
105
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)