今天要做一个功能 导出录取学生信息 费了半天劲结果被老师教育了一番 好了不抱怨了
DataSet数据类型导出Excel
好了不说了上源码
try {
System.Web.HttpContext curContext = System.Web.HttpContext.Current;
DataSet dsExcel = xqgl.Select_DCXSXXALL();
DataGrid dg = new DataGrid();
dg.DataSource = dsExcel;
dg.DataBind();
for (int i = 0; i < dg.Items.Count; i++) {
dg.Items[i].Cells[0].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
dg.Items[i].Cells[1].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
dg.Items[i].Cells[2].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
dg.Items[i].Cells[3].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
}
curContext .Response.Clear();
curContext . Response.Buffer = true;
curContext .Response.Charset = "GB2312";
curContext .Response.AppendHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(myXianQuState.XianQuMC+"优质录取学 生.xls"));
curContext .Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
curContext .Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
this.EnableViewState = false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
dg.RenderControl(oHtmlTextWriter);
curContext .Response.Write(oStringWriter.ToString());
curContext .Response.End();
}
catch (Exception ex) {
string msg = ex.Message;
curContext .Response.Write("<script>javascript:alert(‘导出失败,请重试!‘)</script>");
return;
}
{
20 // 设置编码和附件格式
21 curContext.Response.ContentType = "application/vnd.ms-excel";
22 curContext.Response.ContentEncoding =System.Text.Encoding.UTF8;
23 curContext.Response.Charset = "";
24
25 // 导出excel文件
26 strWriter = new System.IO.StringWriter();
27 htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);
28
29 // 为了解决dgData中可能进行了分页的情况,需要重新定义一个无分页的DataGrid
30 dgExport = new System.Web.UI.WebControls.DataGrid();
31 dgExport.DataSource = dtData.DefaultView;
32 dgExport.AllowPaging = false;
33 dgExport.DataBind();
34
35 // 返回客户端
36 dgExport.RenderControl(htmlWriter);
37 curContext.Response.Write(strWriter.ToString());
38 curContext.Response.End();
39 }
{
11 System.Web.UI.WebControls.DataGrid dgExport = null;
12 // 当前对话
13 System.Web.HttpContext curContext = System.Web.HttpContext.Current;
14 // IO用于导出并返回excel文件
15 System.IO.StringWriter strWriter = null;
16 System.Web.UI.HtmlTextWriter htmlWriter = null;
17
18 if (dtData != null)
19 {
20 // 设置编码和附件格式
21 curContext.Response.ContentType = "application/vnd.ms-excel";
22 curContext.Response.ContentEncoding =System.Text.Encoding.UTF8;
23 curContext.Response.Charset = "";
24
25 // 导出excel文件
26 strWriter = new System.IO.StringWriter();
27 htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);
28
29 // 为了解决dgData中可能进行了分页的情况,需要重新定义一个无分页的DataGrid
30 dgExport = new System.Web.UI.WebControls.DataGrid();
31 dgExport.DataSource = dtData.DefaultView;
32 dgExport.AllowPaging = false;
33 dgExport.DataBind();
34
35 // 返回客户端
36 dgExport.RenderControl(htmlWriter);
37 curContext.Response.Write(strWriter.ToString());
38 curContext.Response.End();
39 }
40 }
Asp.net中由DataSet数据类型导出Excel