簡體   English   中英

想要使用ASP.NET C#在Excel工作表中下載HTML表

[英]Want to download HTML table in Excel Sheet using ASP.NET c#

我創建了一個.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工作表。

https://github.com/jitbit/CsvExport

在自述文件中,您將找到步驟。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM