从数据库导入Excel的方法总结

要在ASP.Net中把数据库中的数据导入到Excel中有如下的几种方法:
一.   RenderControl的方法
 1 None.gif *
 2 None.gif *      //  by XiaoYin [10/22/2006]
 3 None.gif */
 4 None.gif using  System;
 5 None.gif using  System.Data;
 6 None.gif using  System.Configuration;
 7 None.gif using  System.Web;
 8 None.gif using  System.Web.Security;
 9 None.gif using  System.Web.UI;
10 None.gif using  System.Web.UI.WebControls;
11 None.gif using  System.Web.UI.WebControls.WebParts;
12 None.gif using  System.Web.UI.HtmlControls;
13 None.gif using  System.Data.SqlClient;
14 None.gif using  System.Xml;
15 None.gif
16 None.gif public  partial  class  _Default : System.Web.UI.Page 
17 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
18InBlock.gif
19ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//**//**//// <summary>
20InBlock.gif    /// 链接字符串
21ExpandedSubBlockEnd.gif    /// </summary>

22InBlock.gif    public string ConnectString
23ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
24InBlock.gif        get
25ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
26InBlock.gif            return ConfigurationManager.AppSettings["ConnectionString"];
27ExpandedSubBlockEnd.gif        }

28ExpandedSubBlockEnd.gif    }

29InBlock.gif
30ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//**//**//// <summary>
31InBlock.gif    /// 重载VerifyRenderingInServerForm方法
32InBlock.gif    /// 确认在运行时为指定的 ASP.NET 服务器控件呈现 HtmlForm 控件。
33InBlock.gif    /// </summary>
34ExpandedSubBlockEnd.gif    /// <param name="control">ASP.NET 服务器控件,它必须位于 HtmlForm 控件中</param>

35InBlock.gif    public override void VerifyRenderingInServerForm(Control control)
36ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
37InBlock.gif        //base.VerifyRenderingInServerForm(control);
38ExpandedSubBlockEnd.gif    }

39InBlock.gif
40InBlock.gif
41InBlock.gif    protected void Page_Load(object sender, EventArgs e)
42ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
43InBlock.gif        if (!Page.IsPostBack)
44ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
45InBlock.gif            BindData();
46ExpandedSubBlockEnd.gif        }

47ExpandedSubBlockEnd.gif    }

48InBlock.gif
49ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//**//**//// <summary>
50InBlock.gif    /// 绑定数据
51ExpandedSubBlockEnd.gif    /// </summary>

52InBlock.gif    public void BindData()
53ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
54InBlock.gif        // 查询
55InBlock.gif        string query = "SELECT * FROM Categories";
56InBlock.gif        SqlConnection myConnection = new SqlConnection(ConnectString);
57InBlock.gif        SqlDataAdapter ad = new SqlDataAdapter(query, myConnection);
58InBlock.gif        DataSet ds = new DataSet();
59InBlock.gif        ad.Fill(ds, "Categories");
60InBlock.gif        GridView1.DataSource = ds;
61InBlock.gif        GridView1.DataBind();
62ExpandedSubBlockEnd.gif    }

63InBlock.gif
64ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//**//**//// <summary>
65InBlock.gif    /// 内存分页
66InBlock.gif    /// </summary>
67InBlock.gif    /// <param name="sender"></param>
68ExpandedSubBlockEnd.gif    /// <param name="e"></param>

69InBlock.gif    protected void Paging(object sender, GridViewPageEventArgs e)
70ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
71InBlock.gif        GridView1.PageIndex = e.NewPageIndex;
72InBlock.gif        BindData();
73ExpandedSubBlockEnd.gif    }

74InBlock.gif
75InBlock.gif    protected void Button1_Click(object sender, EventArgs e)
76ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
77InBlock.gif        Response.Clear();
78InBlock.gif        Response.Buffer = true;
79InBlock.gif        Response.Charset = "GB2312";
80InBlock.gif        Response.AppendHeader("Content-Disposition""attachment;filename=FileName.xls");
81InBlock.gif        //gaoyang [10/21/2006] 经测试如果设置为 GetEncoding("GB2312"),导出的文件将会出现乱码。
82InBlock.gif        Response.ContentEncoding = System.Text.Encoding.UTF7;
83InBlock.gif
84InBlock.gif        //设置输出文件类型为excel文件。 
85InBlock.gif        Response.ContentType = "application/ms-excel";
86InBlock.gif        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
87InBlock.gif        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
88InBlock.gif        this.GridView1.RenderControl(oHtmlTextWriter);
89InBlock.gif        Response.Output.Write(oStringWriter.ToString());
90InBlock.gif        Response.Flush();
91InBlock.gif        Response.End();
92ExpandedSubBlockEnd.gif    }

93ExpandedBlockEnd.gif}

94 None.gif
注意以下几点:
1、如果出现下面的错误提示可用重载VerifyRenderingInServerForm方法解决。
错误提示:
类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内
在后台文件中重载VerifyRenderingInServerForm方法,如:
public override void VerifyRenderingInServerForm(Control control)
{
     //base.VerifyRenderingInServerForm(control);
}
2、如果设置为 GetEncoding("GB2312"),导出的文件将会出现乱码。
可用Response.ContentEncoding = System.Text.Encoding.UTF7;
或者Encoding.UTF8等来解决,不过导入格式和字体上个人感觉UTF7比UTF8效果好些;
因人而异了:)
3. 在导出数据项前加上'&nbsp;'
导入Excel 时, 数字字符的数据会因Excel中的科学计数法, 导致数值过大的数据丢失后几位数.

二. 逐行写入法
None.gif private   void  ExportToExcel() 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
//设置文件格式为Excel
InBlock.gif

InBlock.gif    Response.ContentType 
= "application/ms-excel";
InBlock.gif
InBlock.gif    
//设置导出的文件名,要替换你自己的文件
InBlock.gif

InBlock.gif    Response.AddHeader(
"Content-Disposition",  "inline;filename=Filename.xls");
InBlock.gif
InBlock.gif    
//写第一行的表格头(Grid的表头)
InBlock.gif

InBlock.gif     
forint iCol=0; iCol< 列数; iCol++)
InBlock.gif
InBlock.gif    Response.Write( “表格头的内容” 
+ Convert.ToChar(9));
InBlock.gif
InBlock.gif    Response.Write(
"\n"); 
InBlock.gif
InBlock.gif    
foreach (每行)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
forint iCol=0; iCol< 列数; iCol++)
InBlock.gif            Response.Write( “实际的数据,即Grid的内容” 
+ Convert.ToChar(9));
InBlock.gif
InBlock.gif        Response.Write(
"\n"); 
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    Response.Write(
"\n"); 
InBlock.gif    Response.End();  
InBlock.gif
ExpandedBlockEnd.gif    }

None.gif
None.gif}
None.gif
None.gif

转载于:https://www.cnblogs.com/brusehht/archive/2006/12/15/593218.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值