GridView数据导入Excel/Excel数据读入GridView

来源:互联网 发布:三个矩阵乘法结合律 编辑:程序博客网 时间:2024/04/20 10:13
 
  1. 解决方案:   
  2.   
  3. 页面增加一个按钮,单击事件添加如下方法:   
  4.   
  5.   
  6.   
  7.  1protected void Button1_Click(object sender, EventArgs e)   
  8.   
  9.  2{   
  10.   
  11.  3    Export("application/ms-excel""学生成绩报表.xls");   
  12.   
  13.  4}   
  14.   
  15.  5private void Export(string FileType, string FileName)   
  16.   
  17.  6{   
  18.   
  19.  7    Response.Charset = "GB2312";   
  20.   
  21.  8    Response.ContentEncoding = System.Text.Encoding.UTF7;   
  22.   
  23.  9    Response.AppendHeader("Content-Disposition""attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());   
  24.   
  25. 10    Response.ContentType = FileType;   
  26.   
  27. 11    this.EnableViewState = false;   
  28.   
  29. 12    StringWriter tw = new StringWriter();   
  30.   
  31. 13    HtmlTextWriter hw = new HtmlTextWriter(tw);   
  32.   
  33. 14    GridView1.RenderControl(hw);   
  34.   
  35. 15    Response.Write(tw.ToString());   
  36.   
  37. 16    Response.End();   
  38.   
  39. 17}   
  40.   
  41. 18//如果没有下面方法会报错类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内   
  42.   
  43. 19public override void VerifyRenderingInServerForm(Control control)   
  44.   
  45. 20{   
  46.   
  47. 21}   
  48.   
  49.   
  50.   
  51. 还有由于是文件操作所以要引入名称空间IO和Text   
  52.   
  53. 后台代码:   
  54.   
  55.   
  56.   
  57.  1using System;   
  58.   
  59.  2using System.Data;   
  60.   
  61.  3using System.Configuration;   
  62.   
  63.  4using System.Web;   
  64.   
  65.  5using System.Web.Security;   
  66.   
  67.  6using System.Web.UI;   
  68.   
  69.  7using System.Web.UI.WebControls;   
  70.   
  71.  8using System.Web.UI.WebControls.WebParts;   
  72.   
  73.  9using System.Web.UI.HtmlControls;   
  74.   
  75. 10using System.Data.SqlClient;   
  76.   
  77. 11using System.Drawing;   
  78.   
  79. 12using System.IO;   
  80.   
  81. 13using System.Text;   
  82.   
  83. 14public partial class Default7 : System.Web.UI.Page   
  84.   
  85. 15{   
  86.   
  87. 16    SqlConnection sqlcon;   
  88.   
  89. 17    SqlCommand sqlcom;   
  90.   
  91. 18    string strCon = "Data Source=(local);Database=北风贸易;Uid=sa;Pwd=sa";   
  92.   
  93. 19    protected void Page_Load(object sender, EventArgs e)   
  94.   
  95. 20    {   
  96.   
  97. 21        if (!IsPostBack)   
  98.   
  99. 22        {   
  100.   
  101. 23            bind();   
  102.   
  103. 24        }   
  104.   
  105. 25    }   
  106.   
  107. 26       
  108.   
  109. 27    public void bind()   
  110.   
  111. 28    {   
  112.   
  113. 29        string sqlstr = "select top 5 * from 飞狐工作室";   
  114.   
  115. 30        sqlcon = new SqlConnection(strCon);   
  116.   
  117. 31        SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);   
  118.   
  119. 32        DataSet myds = new DataSet();   
  120.   
  121. 33        sqlcon.Open();   
  122.   
  123. 34        myda.Fill(myds, "飞狐工作室");   
  124.   
  125. 35        GridView1.DataSource = myds;   
  126.   
  127. 36        GridView1.DataKeyNames = new string[] { "身份证号码" };   
  128.   
  129. 37        GridView1.DataBind();   
  130.   
  131. 38        sqlcon.Close();   
  132.   
  133. 39    }   
  134.   
  135. 40    protected void Button1_Click(object sender, EventArgs e)   
  136.   
  137. 41    {   
  138.   
  139. 42        Export("application/ms-excel""学生成绩报表.xls");   
  140.   
  141. 43    }   
  142.   
  143. 44    private void Export(string FileType, string FileName)   
  144.   
  145. 45    {   
  146.   
  147. 46        Response.Charset = "GB2312";   
  148.   
  149. 47        Response.ContentEncoding = System.Text.Encoding.UTF7;   
  150.   
  151. 48        Response.AppendHeader("Content-Disposition""attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());   
  152.   
  153. 49        Response.ContentType = FileType;   
  154.   
  155. 50        this.EnableViewState = false;   
  156.   
  157. 51        StringWriter tw = new StringWriter();   
  158.   
  159. 52        HtmlTextWriter hw = new HtmlTextWriter(tw);   
  160.   
  161. 53        GridView1.RenderControl(hw);   
  162.   
  163. 54        Response.Write(tw.ToString());   
  164.   
  165. 55        Response.End();   
  166.   
  167. 56    }   
  168.   
  169. 57    public override void VerifyRenderingInServerForm(Control control)   
  170.   
  171. 58    {   
  172.   
  173. 59    }   
  174.   
  175. 60}   
  176.   
  177.     
  178.   
  179. 前台:   
  180.   
  181.   
  182.   
  183.  1<asp:GridView ID="GridView1" runat="server"    AutoGenerateColumns="False" CellPadding="3"     
  184.   
  185.  2     BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" Font-Size="12px"   >   
  186.   
  187.  3    <FooterStyle BackColor="White" ForeColor="#000066" />   
  188.   
  189.  4    <Columns>   
  190.   
  191.  5        <asp:BoundField DataField="身份证号码" HeaderText="编号" ReadOnly="True" />   
  192.   
  193.  6        <asp:BoundField DataField="姓名" HeaderText="姓名"  />   
  194.   
  195.  7        <asp:BoundField DataField="出生日期" HeaderText="邮政编码"  />   
  196.   
  197.  8        <asp:BoundField DataField="家庭住址" HeaderText="家庭住址"  />   
  198.   
  199.  9        <asp:BoundField DataField="邮政编码" HeaderText="邮政编码" />   
  200.   
  201. 10        <asp:BoundField DataField="起薪" HeaderText="起薪"  />   
  202.   
  203. 11          
  204.   
  205. 12    </Columns>   
  206.   
  207. 13    <RowStyle ForeColor="#000066" />   
  208.   
  209. 14    <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />   
  210.   
  211. 15    <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left"  CssClass="ms-formlabel DataGridFixedHeader"/>   
  212.   
  213. 16    <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />   
  214.   
  215. 17</asp:GridView>   
  216.   
  217. 18<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="导出" />   
  218.   
  219.     
  220.   
  221. 读取Excel数据的代码:这个很简单的   
  222.   
  223.   
  224.   
  225.  1private DataSet CreateDataSource()   
  226.   
  227.  2{   
  228.   
  229.  3    string strCon;   
  230.   
  231.  4    strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("excel.xls") + "; Extended Properties=Excel 8.0;";   
  232.   
  233.  5    OleDbConnection olecon = new OleDbConnection(strCon);   
  234.   
  235.  6    OleDbDataAdapter myda = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strCon);   
  236.   
  237.  7    DataSet myds = new DataSet();   
  238.   
  239.  8    myda.Fill(myds);   
  240.   
  241.  9    return myds;   
  242.   
  243. 10}   
  244.   
  245. 11protected void Button1_Click(object sender, EventArgs e)   
  246.   
  247. 12{   
  248.   
  249. 13    GridView1.DataSource = CreateDataSource();   
  250.   
  251. 14    GridView1.DataBind();   
  252.   
  253. 15}  
原创粉丝点击