问题如题
求高手指导下
谢谢!!!!!!
我的代码如下:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Data.SqlClient;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using StudentModels;
using StudentBLL;
using System.IO;
using System.Text;
public partial class StudentInfo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
StuInfoBind();
}
}
public void StuInfoBind()
{
SqlConnection sqlcon = new SqlConnection("server=.;database=MySchool;uid=sa");
SqlDataAdapter da = new SqlDataAdapter("select *from stuInfo", sqlcon);
DataSet ds = new DataSet();
da.Fill(ds, "StuInfoNew");
this.gvStudentInfo.DataSource = ds;
this.gvStudentInfo.DataBind();
}
public void ExportExcel()
{
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("Information.xls", Encoding.UTF8));
Response.Charset = "";
Response.ContentEncoding = System.Text.Encoding.UTF8;
//Response.Write("<meta http-equiv=Content-Type content=text/html;charset=utf-8>");
Response.ContentType = "application/ms-xml";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
StuInfoBind();
gvStudentInfo.AllowPaging = false;
gvStudentInfo.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
gvStudentInfo.AllowPaging = true;
StuInfoBind();
}
public override void VerifyRenderingInServerForm(Control control)
{
}
protected void btnExcel_Click(object sender, EventArgs e)
{
ExportExcel();
}
}
GridView控件实现数据导入Excel(全部数据)
public partial class Demo38 : System.Web.UI.Page
15 {
16 protected void Page_Load(object sender, EventArgs e)
17 {
18 if (Page.IsPostBack == false)
19 {
20 BindData();
21 }
22 }
23
24 public void BindData()
25 {
26 string strSql = "select UserID,C_Name,E_name,UpdataDate,salary,QQ,Zip_Code from Demo_User ";
27 DataTable dt = SqlHelper.ExecuteDataset(SqlHelper.CONN_STRING, CommandType.Text, strSql, null).Tables[0];
28
29 GridView.DataSource = dt;
30 GridView.DataKeyNames = new string[] { "UserID" };//主键
31 GridView.DataBind();
32 }
33
34 protected void GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
35 {
36 GridView.PageIndex = e.NewPageIndex;
37 BindData();
38 }
39
40 protected void Button1_Click(object sender, EventArgs e)
41 {
42 Export("application/ms-excel", "User.xls");
43 }
44
45 private void Export(string FileType, string FileName)
46 {
47 GridView.AllowPaging = false;
48 GridView.AllowSorting = false;
49
50 BindData();
51
52 Response.Clear();
53 Response.Charset = "GB2312";
54 Response.ContentEncoding = System.Text.Encoding.UTF7;
55 Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
56 Response.ContentType = FileType;
57 this.EnableViewState = false;
58 StringWriter tw = new StringWriter();
59 HtmlTextWriter hw = new HtmlTextWriter(tw);
60 GridView.RenderControl(hw);
61 Response.Write(tw.ToString());
62 Response.End();
63 }
64
65 public override void VerifyRenderingInServerForm(Control control)
66 {
67
68 }
69 }
1 <table align="center" bgcolor="#c0de98" border="0" cellpadding="0" cellspacing="1" width="99%">
2 <tr>
3 <th colspan="2">
4 GridView演示</th>
5 </tr>
6 <tr>
7 <td colspan="2" >
8 <asp:GridView ID="GridView" runat="server" Width="100%" AutoGenerateColumns="False" AllowPaging="True" OnPageIndexChanging="GridView_PageIndexChanging" PageSize="12" >
9 <Columns>
10 <asp:BoundField DataField="UserID" HeaderText="UserID" />
11 <asp:BoundField DataField="C_Name" HeaderText="中文名字" />
12 <asp:BoundField DataField="E_Name" HeaderText="英文名字" />
13 <asp:BoundField DataField="salary" HeaderText="薪水" DataFormatString="{0:C}" HtmlEncode ="False"/>
14 <asp:BoundField DataField="QQ" HeaderText="QQ" />
15 <asp:BoundField DataField="Zip_Code" HeaderText="邮政编码" />
16 </Columns>
17 <RowStyle HorizontalAlign="Center" />
18 <PagerStyle HorizontalAlign="Center" />
19 </asp:GridView>
20 </td>
21 </tr>
22
23 <tr>
24 <td colspan="2" align="center">
25 <asp:Button ID="Button1" runat="server" Text="导出Excel" CssClass="Button" OnClick="Button1_Click" /></td>
26 </tr>
27
28 </table>
更多请参考
http://www.aspx58.com.cn/Default_AC_View.aspx?ID=110