C# 要使用ASP.NET c下载Excel工作表中的HTML表吗#

C# 要使用ASP.NET c下载Excel工作表中的HTML表吗#,c#,html,asp.net,excel,download,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

我已经创建了一个
.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文件的形式下载这个表格,并使用相同的表格格式。 我找不到任何具体的解决办法,请帮助我


提前感谢。

您可以使用csv导出类,该类具有内置方法,可将数据从表格导出到excel工作表


在自述文件中,您可以找到这些步骤。

将值保存在数据表中,然后将其导出到excel(例如使用EPPlus)我读过EPPlus这个名称,但不知道如何使用它,在项目中使用第三方应用程序是否值得?如果是,请解释一下。谢谢@Vikas,我以前也看到过这个线程,但由于我使用的是html表格,它没有在c#代码中检测到我的表格id。我不想批评你的方法,但我个人也以其他方式做了同样的事情。aspx有一个奇妙的分离:aspx用于标记,aspx.cs用于代码。使用OnPageLoad(etc)事件将数据绑定到html。如果您能够编写带有代码隐藏部分的页面(其中发生了%get data from database%事件),那么您就可以轻松地利用EPPlus。这是一个很棒的库,非常容易使用,若你们有原始的收集数据(例如列表),我必须显示来自数据库的头和数据单元格,正如你们在我上面的代码中看到的。CSV对我有什么帮助?CSV只是Excel支持的一种文件格式。最好使用csv格式导出表,因为可以使用csv格式轻松还原表,只需将它们输入到正确的工具即可。@Azemshaikh只导出从数据库获得的数据,而不是html表。导出表不需要此步骤。谢谢大家的帮助,刚才我使用stringwriter实现了我的目标。:)