使用GridView将数据导入或导出Excel中

 
  1. <pre class="csharp" name="code">using System;  
  2. using System.Collections;  
  3. using System.Configuration;  
  4. using System.Data;  
  5. using System.Linq;  
  6. using System.Web;  
  7. using System.Web.Security;  
  8. using System.Web.UI;  
  9. using System.Web.UI.HtmlControls;  
  10. using System.Web.UI.WebControls;  
  11. using System.Web.UI.WebControls.WebParts;  
  12. using System.Xml.Linq;  
  13. using System.IO;  
  14. using System.Data.OleDb;  
  15.   
  16. public partial class Default3 : System.Web.UI.Page  
  17. {  
  18.     protected void Page_Load(object sender, EventArgs e)  
  19.     {  
  20.         if (!IsPostBack) {  
  21.             Bind();     //窗体加载时向GridView中填充数据  
  22.         }  
  23.     }  
  24.     PosDataContext pos = new PosDataContext();     //使用Linq to sql进行操作数据库的数据  
  25.     //自定义向GridView控件填充数据方法  
  26.     private void Bind()  
  27.     {  
  28.         GridView1.DataSource = pos.Product.ToList();   //填充数据  
  29.         GridView1.DataKeyNames = new string[] {"Id" }; //设置主键字段  
  30.         GridView1.DataBind();                       //将数据绑定到控件中  
  31.   
  32.     }  
  33.     //导出数据事件  
  34.     protected void btnExcel_Click(object sender, EventArgs e)  
  35.     {  
  36.         Export("application/ms-excel","商品信息报表.xls");  
  37.     }  
  38.     //自定义导出数据的方法  
  39.     private void Export(string FileType, string FileName)  
  40.     {  
  41.         Response.Charset = "GB2312";                 //设置获取输出数据的类型  
  42.         Response.ContentEncoding = System.Text.Encoding.UTF7;  
  43.         //将Http头添加到输出流  
  44.         Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString());  
  45.         Response.ContentType = FileType;  
  46.         this.EnableViewState = false;  
  47.         StringWriter tw = new StringWriter();  
  48.         HtmlTextWriter hw = new HtmlTextWriter(tw);  
  49.         GridView1.RenderControl(hw); //将GridView中的内容输出到指定字符串中  
  50.         Response.Write(tw.ToString());  
  51.         Response.End();           //将当前面的所有缓冲发送到客户端  
  52.     }  
  53.     //如果没有下面方法会报错类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内  
  54.     public override void VerifyRenderingInServerForm(Control control)  
  55.     {  
  56.         //base.VerifyRenderingInServerForm(control);  
  57.     }  
  58.     //将Excel中的数据导入到指定控件中  
  59.     public DataSet CreateDataSource() {  
  60.         string strCon = "provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("Excel.xls")+"; Extended PropertiesExcle 8.0";  
  61.             OleDbConnection olecon=new OleDbConnection(strCon);  
  62.         OleDbDataAdapter myda=new OleDbDataAdapter("select * from [Sheet1$]",strCon);  
  63.         DataSet myds=new DataSet();  
  64.          myda.Fill(myds);  
  65.         return myds;  
  66.   
  67.   
  68.     }  
  69.     //将数据从Excel导入到指定控件中  
  70. protected void  btnExcelInfo_Click(object sender, EventArgs e)  
  71. {  
  72.      GridView1.DataSourceCreateDataSource();  
  73.     GridView1.DataBind();  
  74. }  
  75. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值