VS2005 ASP.NET C# DataSet导入Excel

新建一个按钮Button1,即可实现DataSet导入Excel功能:

ContractedBlock.gif ExpandedBlockStart.gif Code
 1protected void Button1_Click(object sender, EventArgs e)
 2ExpandedBlockStart.gifContractedBlock.gif    {
 3        string sql_excel = "select * from Web_Processing";
 4        string mystring = "Provider=sqloledb;Data Source=hz;Initial Catalog=Web;User Id=sa;Password=123";
 5        OleDbConnection cnn = new OleDbConnection(mystring);
 6        OleDbDataAdapter myDa = new OleDbDataAdapter(sql_excel, cnn);
 7        DataSet ds = new DataSet();
 8        myDa.Fill(ds);
 9        cnn.Close(); 
10
11        String filename = "";
12        ExportDataSetToExcel(ds, filename);
13    }
 
14
15    public void ExportDataSetToExcel(DataSet ds, string filename)
16ExpandedBlockStart.gifContractedBlock.gif    {
17        HttpResponse response = HttpContext.Current.Response; 
18
19        // first let's clean up the response.object
20        response.Clear();
21        response.Charset = ""
22
23        // set the response mime type for excel
24        response.ContentType = "application/vnd.ms-excel";
25        response.AddHeader("Content-Disposition""attachment;filename=\"" + filename + "\""); 
26
27        // create a string writer
28        using (System.IO.StringWriter sw = new StringWriter())
29ExpandedSubBlockStart.gifContractedSubBlock.gif        {
30            using (HtmlTextWriter htw = new HtmlTextWriter(sw))
31ExpandedSubBlockStart.gifContractedSubBlock.gif            {
32                // instantiate a datagrid
33                DataGrid dg = new DataGrid();
34                dg.DataSource = ds.Tables[0];
35                dg.DataBind();
36                dg.RenderControl(htw);
37                response.Write(sw.ToString());
38                response.End();
39            }

40        }

41    }

42

 

转载于:https://www.cnblogs.com/Zoya/archive/2009/08/25/1553501.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值