在表格单元格中构建和显示HTML页面

本文关键字:显示 HTML 页面 构建 表格 单元格 | 更新日期: 2023-09-27 18:03:30

我必须为我的项目创建一些报告。对于桌面应用程序,我的解决方案是使用带有"web浏览器"组件的表单,用html构建报表,并使用html字符串作为显示文本。这工作得非常好,允许我在没有任何报告工具的情况下创建自定义报告(我使用的是VS.的express版本,不包括报告工具)。但是,我不知道如何在aspx页面中做到这一点。我可以构建报告,只需要知道如何在目标单元格中获得它,以及如何确保报告正确显示,而不是作为持有它的原始字符串。

下面的代码:

<table style="width: 100%; height: 690px;">
   <tr>
      <td class="auto-style2">Select Report Type</td>
      <td class="auto-style3">
         <asp:Button ID="btnTodayReport" runat="server" OnClick="btnTodayReport_Click" Text="Today's Orders" />
      </td>
      <td class="auto-style4">
         <asp:Button ID="btWeeklyReport" runat="server" Text="Weekly Report" OnClick="btnRunReport_Click" />
      </td>
      <td class="auto-style5">
         &nbsp;
      </td>
      <td class="auto-style1">
         &nbsp;&nbsp;
      </td>
   </tr>
   <tr>
      <td colspan="5" style="border-style: solid">**This is where the report should display**</td>
   </tr>
</table>

按照要求,下面是构建html字符串的代码。这是从旧的代码,但我将使用相同的方法为这个项目,或者至少想使用这个。wb是用于WinForms应用程序的WebBrowser组件。

private void btnGetReport_Click(object sender, EventArgs e)
    {
        // get the data for the report
        wb.DocumentText = RepBuild.BuildAverageReport(dtStart.Value, dtEnd.Value);
    }    
public static string BuildReview(review rev)
    {
        int questID = 0;
        string Body = "";
        Body = "<HTML><HEAD><META HTTP-EQUIV='"Content-Type'" CONTENT='"text/html;charset=windows-1252'">" +
            "<TITLE>Ticket Review for " + rev.RevEmpName + "</TITLE></HEAD><BODY>";
        Body += "<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 ><TR HEIGHT=40 >" +
            "<TD ALIGN=LEFT BGCOLOR=#FFFFFF ><FONT style=FONT-SIZE:18pt FACE='"Segoe UI'" COLOR=#204d89>Ticket Review for " + rev.RevEmpName + "</FONT></TD>" +
            "</TR></TABLE><TABLE HEIGHT=150></TABLE>";
        // show the questions in order
        Body += "<table border='1' width = '800px' align = 'center'> <tr>" +
            "<td rowspan = '3' width = '80px'><b>Number </b></td><td rowspan = '3' width = '80px'><b>Score </b></td>" +
            "<td width = '600px'><b>Question </b></td></tr><tr><td><b>Comments</b></td></tr> </Table>";
        while (questID <= rev.RevQuestions.Count - 1)
        {
            if ((questID % 2) == 0)//the number is even
            {
                Body += "<table border='1' width = '800px' align = 'center'><tr>" +
                "<td rowspan = '3' width = '80px'>" + rev.RevQuestions[questID].QuestID.ToString() + " </td> <td rowspan = '3' width = '80px'> " +
                rev.RevQuestions[questID].QuestScore + " </td> " +
                " <td width = '600px'> " + rev.RevQuestions[questID].QuestText + " </td> </tr> <tr> <td><b> " +
                rev.RevQuestions[questID].QuestComment + " </b></td> </tr> </table> ";
            }
            else
            {
                Body += "<table border='1' width = '800px' align = 'center' bgcolor='#cccccc'><tr>" +
                "<td rowspan = '3' width = '80px'>" + rev.RevQuestions[questID].QuestID.ToString() + " </td> <td rowspan = '3' width = '80px'> " +
                rev.RevQuestions[questID].QuestScore + " </td> " +
                " <td width = '600px'> " + rev.RevQuestions[questID].QuestText + " </td> </tr> <tr> <td><b> " +
                rev.RevQuestions[questID].QuestComment + " </b></td> </tr> </table> ";
            }
            questID++;
        }
        Body += "<BR><BR>";
        Body += "<Table width = '400px' align = 'left'>";
        Body += "<TR>";
        Body += "<TD><b>Reviewed By</b></TD>";
        Body += "<TD> " + rev.RevLeadName + "</TD>";
        Body += "</TR>";
        Body += "<TR>";
        Body += "<TD><b>Review Date</b></TD>";
        Body += "<TD> " + rev.RevDate.ToShortDateString() + "</TD>";
        Body += "</TR>";
        Body += "<TR>";
        Body += "<TD><b>Overall Score</b></TD>";
        Body += "<TD> " + rev.RevScore + " out of 100</TD>";
        Body += "</TR>";
        Body += "<TR>";
        Body += "<TD><b>Ticket Date</b></TD>";
        Body += "<TD> " + rev.RevTickDate + "</TD>";
        Body += "</TR>";
        Body += "<TR>";
        Body += "<TD><b>Ticket Number</b></TD>";
        Body += "<TD> " + rev.RevTickNum + "</TD>";
        Body += "</TR>";
        Body += "</TABLE>";
        Body += "</HTML>";
        return Body;
    }

在表格单元格中构建和显示HTML页面

我找到了解决这个问题的方法。首先,我必须更改标记,以便可以在后面的代码中引用它。我把它设置为在服务器上运行,并给了它一个ID。

<td id="ReportCell" runat="server" colspan="5" style="border-style: solid">

之后,我可以使用ReportCell。InnerHtml成员来设置单元格的html字符串。然后,我只需要从报告数据构建html并显示它。这可能不是最好的方法,而且html不是制表符格式的。我考虑使用htmltextwwriter,但我想保持它的简单,它看起来像我将不得不添加一堆代码来完成同样的事情。

我添加这一点,以防其他人有类似的情况,需要答案。

下面的代码:

// added runat="server" and id="ReportCell"
<tr style="text-align: left; vertical-align: top">
            <td id="ReportCell" runat="server" colspan="5" style="border-style: solid">
                <br />
                <br />
            </td>
        </tr>

背后的代码:我创建了一个新的字符串,并开始用报告的html加载它。启动一个表,并将第一个单元格填充为标题。

protected void btnTodayReport_Click(object sender, EventArgs e)
    {
        // show the orders that were completed today
        string Body = "";
        DateTime yesterday = DateTime.Now.AddDays(-1);
        Body = "<font size='"6'">Orders Completed Yesterday: " + yesterday.ToShortDateString() + "</font><br />";
        Body += "<br />";
        Body += "<table style='"width: 100%; height: 100%;'" >";
        // Table colomn header
        Body += "<tr>";
        Body += "<td><b>Order#</b></td>";
        Body += "<td><b>Site ID</b></td>";
        Body += "<td><b>Location</b></td>";
        Body += "<td><b>VAR</b></td>";
        Body += "<td><b>Server</b></td>";
        Body += "<td><b>Install Tech</b></td>";
        Body += "<td><b>QA Tech</b></td>";
        Body += "<td><b>Notes</b></td>";
        Body += "</tr>";
        // fill the table with data 
        int count = 0;
        Body += Data.ReportYesterdysOrders(yesterday,ref count);            
        Body += "</tabl>";
        Body += "Number of Orders Completed: " + count.ToString() + "<br />";
        Body += "<br />";
        ReportCell.InnerHtml = Body;
    }

来自静态Data类:

我连接到数据库并为报告拉回数据。当DataReader循环遍历返回的行时,我将每一行作为一行添加到html表中,并返回字符串。这样,所有的数据都被添加到字符串中正确的位置。

public static string ReportYesterdysOrders(DateTime yesterday, ref int count)
    {
        try
        {
            string result = "";
            MySqlConnection conn = new MySqlConnection(constr);
            conn.Open();
            string qry = "Select OrderNumber, SiteID, Location, Customer, Server, InstallTech, QATech, Notes from SalesOrders" +
                " where DateCompleted = @yesterday";
            MySqlCommand cmd = new MySqlCommand(qry, conn);
            cmd.Parameters.AddWithValue("@yesterday", yesterday.ToString("yyyy-MM-dd"));
            MySqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                result += "<tr>";
                result += "<td>" + dr["OrderNumber"].ToString() + "</td>";
                result += "<td>" + dr["SiteID"].ToString() + "</td>";
                result += "<td>" + dr["Location"].ToString() + "</td>";
                result += "<td>" + dr["Customer"].ToString() + "</td>";
                result += "<td>" + dr["Server"].ToString() + "</td>";
                result += "<td>" + dr["InstallTech"].ToString() + "</td>";
                result += "<td>" + dr["QATech"].ToString() + "</td>";
                result += "<td>" + dr["Notes"].ToString() + "</td>";
                result += "</tr>";
                count++;
            }
            conn.Close();
            return result;
        }
        catch (MySqlException e)
        {
            return e.Message;
        }
    }

输出是一个格式化的html报告,可以从html表中的目标单元格中选择、复制或打印。