• 以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

  • 相关阅读:
    前辈的经验
    ES6 的新特性
    jQuery 事件对象的属性和方法
    ECMAScript 6 入门
    浏览器的加载与页面性能优化
    ajax 基于 jquery 简单的 ajax 请求
    浏览器加载、解析、渲染的过程
    浏览器的加载和解析的过程,以及如何加快 HTML 的加载速度
    【经典问题】当你输入一个网址的时候,实际会发生什么?
    总结 IE 下的一些 BUG
  • 原文地址:https://www.cnblogs.com/Dannier/p/excel.html
Copyright © 2020-2023  润新知