zoukankan      html  css  js  c++  java
  • 以HTML格式导出Excel报表

    Hi man, welcome to to this example of creating a sample report using C#.

    In this artical, it will cover how to export a report to a excel file and how to farmat the data came from a sql database.

    Below pic is the screen shot of this example.

    As what u are seeing. I wil use a GridView to produce this report. Here is the HTML Code I used in this sample.

    View Code
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
    CellPadding
    ="4" ForeColor="#333333" GridLines="None" Width="317px">
    <AlternatingRowStyle BackColor="White"/>
    <Columns>
    <asp:BoundField DataField="ID" HeaderText="ID"/>
    <asp:BoundField DataField="EMPNO" HeaderText="EMPNO"/>
    <asp:BoundField DataField="NAME" HeaderText="NAME"/>
    <asp:BoundField DataField="AGE" HeaderText="AGE"/>
    </Columns>
    <EditRowStyle BackColor="#7C6F57"/>
    <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White"/>
    <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White"/>
    <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center"/>
    <RowStyle BackColor="#E3EAEB" HorizontalAlign ="Center"/>
    <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333"/>
    <SortedAscendingCellStyle BackColor="#F8FAFA"/>
    <SortedAscendingHeaderStyle BackColor="#246B61"/>
    <SortedDescendingCellStyle BackColor="#D4DFE1"/>
    <SortedDescendingHeaderStyle BackColor="#15524A"/>
    </asp:GridView>
    <asp:Button ID="btnExport" runat="server" onclick="btnExport_Click"
    Text
    ="Export to Excel"/>

    How to bind the data to GirdView? Here is the source code if you are interested.

    View Code
    protectedvoid Page_Load(object sender, EventArgs e)
    {
    LoadReport();
    }
    protectedvoid LoadReport() {
    string cmdText ="";
    string connString = ConfigurationManager.ConnectionStrings["testConnectionString"].ToString();

    StringBuilder sbuilder
    =new StringBuilder("Select ");
    sbuilder.AppendLine(
    "ID");
    sbuilder.AppendLine(
    ",EMPNO");
    sbuilder.AppendLine(
    ",NAME");
    sbuilder.AppendLine(
    ",AGE");
    sbuilder.AppendLine(
    "From TEST");

    cmdText
    = sbuilder.ToString();
    DataSet ds
    =new DataSet();
    using (SqlConnection conn =new SqlConnection(connString))
    {
    SqlCommand cmd
    =new SqlCommand(cmdText,conn);
    SqlDataAdapter da
    =new SqlDataAdapter(cmd);
    da.Fill(ds);
    }
    GridView1.DataSource
    = ds.Tables[0];
    GridView1.DataBind();
    }

    What's the next? Right, we need to export it to an excel. I directly write the data as Html to excel file, and also you can include the data format. You can using CSS to format your report. In the following source code, I provided the simplest and most brife code snap.

    View Code
    protectedvoid btnExport_Click(object sender, EventArgs e)
    {
    string attachment ="attachment; filename= Test Report"+ DateTime.Now +".xls";
    if (GridView1.Rows.Count +1<65536){
    GridView1.AllowPaging
    =false;
    StringWriter sw
    =new StringWriter();
    HtmlTextWriter htw
    =new HtmlTextWriter(sw);
    Response.ContentType
    ="";
    HtmlForm frm
    =new HtmlForm();

    //start the excel output
    Response.ContentType ="application/vnd.ms-excel";
    Response.AddHeader(
    "content-disposition", attachment);
    Response.Charset
    ="";

    Controls.Add(frm);
    frm.Controls.Add(GridView1);
    frm.RenderControl(htw);

    Response.Write(sw.ToString());
    Response.End();
    }
    else {
    Response.Write(
    "<script>alert('Too many rows!')</script>");
    }
    }

    Now, have you got the correct result? Help this article will help you. :)

    If you want to know how to format your data, pls go to this link: http://www.cnblogs.com/Dannier/archive/2011/03/09/1978632.html

  • 相关阅读:
    负载均衡,分布式,集群的理解,多台服务器代码如何同步
    浅谈控制反转与依赖注入
    Jetbrains系列产品重置试用方法
    讲述一下一个还没毕业的科班程序猿的求职历程
    一个在求职路上挣扎的萌新
    用C#操作文件/文件夹(删除,复制,移动)
    随笔-关于公网IP无法访问服务器的解决办法
    C/s从文件(TXT)中读取数据插入数据库
    Sql Server + ADO.NET
    进程。线程与线程池
  • 原文地址:https://www.cnblogs.com/Dannier/p/excel.html
Copyright © 2011-2022 走看看