想要使用ASP.NET c#在Excel Sheet中下载HTML表

时间:2016-03-23 07:11:38

标签: c# html asp.net excel download

我创建了一个.aspx页面,我在两个不同的表格中显示摘要报告,并从数据库中检索值。

这是我的aspx代码:

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

<div style="text-align:center;" ><asp:Label ID="Label3" runat="server" Text="Summary Report" style="font-size:x-large;"></asp:Label></div>    
<br />
<table id="tbl1" style="width: 100%;" border="1">
    <tr>
        <th class="auto-style1" colspan="11" style="text-align:center;padding:10px 10px 10px 10px;">Total Players: <asp:Label ID="lblTotPlayers" runat="server" Text="Label"></asp:Label></th>
    </tr>
    <tr>
        <% string connString = ConfigurationManager.ConnectionStrings["dbConnection"].ConnectionString;
           System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(connString);
           System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
           con.Open();
           cmd = new System.Data.SqlClient.SqlCommand("select vchSportName from UserSportMapping left outer join tblPWP_Sports on UserSportMapping.SportsID = tblPWP_Sports.intSportId WHERE vchSportName IN (vchSportName)  group by vchSportName", con);
            System.Data.SqlClient.SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {%>
            <th class="auto-style1" style="padding:10px 10px 10px 10px;"><%=dr["vchSportName"] %></th>
            <%}
                dr.Close(); %>
    </tr>
    <tr>
        <% cmd = new System.Data.SqlClient.SqlCommand("select count(vchSportName) as Counts from UserSportMapping left outer join tblPWP_Sports on UserSportMapping.SportsID = tblPWP_Sports.intSportId WHERE vchSportName IN (vchSportName)  group by vchSportName", con);
            System.Data.SqlClient.SqlDataReader dr1 = cmd.ExecuteReader();
            while (dr1.Read())
            {%>
            <td style="padding:10px 10px 10px 10px;"><%=dr1["Counts"] %></td>
            <%}
            dr1.Close();%>
    </tr>
</table>

<br /><br /><br />

<table id="tbl2" style="width: 100%;" border="1">
    <tr>
        <th class="auto-style1" colspan="12" style="text-align:center;padding:10px 10px 10px 10px;">Total Business Partners: <asp:Label ID="lblTotBPs" runat="server" Text="Label"></asp:Label></th>
    </tr>
    <tr>
        <% cmd = new System.Data.SqlClient.SqlCommand("select UserType, COUNT(CompanyName) as Counts from tblUserDetails full outer join Mst_UserType on tblUserDetails.ServiceId = Mst_UserType.UserTypeId where UserType in (UserType) and UserTypeId <> 1 and IsActive=1 group by UserType order by UserType", con);
            System.Data.SqlClient.SqlDataReader dr2 = cmd.ExecuteReader();
            while (dr2.Read())
            {
            %>
            <th class="auto-style1" style="padding:10px 10px 10px 10px;"><%=dr2["UserType"] %></th>
            <%}
            dr2.Close(); %>
    </tr>
    <tr>
        <% cmd = new System.Data.SqlClient.SqlCommand("select UserType, COUNT(CompanyName) as Counts from tblUserDetails full outer join Mst_UserType on tblUserDetails.ServiceId = Mst_UserType.UserTypeId where UserType in (UserType) and UserTypeId <> 1 and IsActive=1 group by UserType order by UserType", con);
            System.Data.SqlClient.SqlDataReader dr3 = cmd.ExecuteReader();
            while (dr3.Read())
            {
            %>
        <td style="padding:10px 10px 10px 10px;"><%=dr3["Counts"] %></td>
        <%}
            dr3.Close(); %>
    </tr>
</table>
<br /><br />
<asp:Button ID="Button1" runat="server" Text="Back" OnClick="Button1_Click" /> &nbsp; <asp:Button ID="Button2" runat="server" Text="Download as Excel" OnClick="DownExcel"  />

有一点需要注意,我在<% %>页面上使用.aspx来编写c#代码。

现在我想将此表格下载为具有相同表格格式的Excel文件。 我无法找到任何具体的解决方案,请帮助我。

提前致谢。

1 个答案:

答案 0 :(得分:0)

您可以使用具有内置方法的csv导出类将数据从表导出到Excel工作表。

https://github.com/jitbit/CsvExport

在自述文件中,您将找到步骤。