【问题标题】:Want to download HTML table in Excel Sheet using ASP.NET c#想要使用 ASP.NET c# 在 Excel 表中下载 HTML 表格
【发布时间】:2016-03-23 07:11:38
【问题描述】:

我创建了一个.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页面上使用&lt;% %&gt;来编写c#代码。

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

提前致谢。

【问题讨论】:

  • 将值保存在数据表中,然后将其导出到 excel(例如使用 EPPlus)
  • 我看过EPPlus这个名字,但是不知道怎么用,在项目中使用第三方app值得吗?如果是,请解释一下。
  • 谢谢@Vikas 我以前也见过那个线程,但是因为我使用的是 html 表,所以它没有在 c# 代码中检测到我的表 id。
  • 我不想批评您的方法,但我个人以其他方式做了同样的事情。 aspx 有一个很好的关注点分离:aspx 用于标记,aspx.cs 用于代码。使用 OnPageLoad (etc) 事件将您的数据绑定到 html。如果您负担得起使用代码隐藏部分(发生 %get data from database% 事情的地方)编写页面,那么您可以轻松利用 EPPlus。如果您有原始集合数据(例如 List 事物),它是一个很棒的库并且非常易于使用

标签: c# html asp.net excel download


【解决方案1】:

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

https://github.com/jitbit/CsvExport

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

【讨论】:

  • 我必须显示来自数据库的标题和数据单元格,正如您在上面的代码中看到的那样。 CSV 将如何帮助我?
  • Csv 只是 Excel 支持的一种文件格式。最好使用 csv 格式导出表格,因为只需输入正确的工具即可使用它们轻松恢复它们。
  • @AzeemShaikh 只是导出从数据库中获取的数据,而不是 html 表。导出表格不需要这一步。
  • 谢谢大家的帮助,刚才我已经使用stringwriter实现了我的目标。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-11
  • 2015-07-18
  • 2014-04-08
  • 1970-01-01
  • 2017-07-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多