简体   繁体   English

c#将数据库导出为HTML格式的表

[英]c# Export Database to HTML formatted table

I am writing a simple application in C# and WPF for reporting. 我正在用C#和WPF编写一个简单的报表应用程序。 I have following table in a database of sqlite: 我在sqlite数据库中有下表:

Table1:
    id
    name
    amntOne
    amntTwo
    entryDate

Table2:
    id
    name
    amntOne
    amntTwo
    location
    entryDate

Table3:
    id
    name
    amntOne
    amntTwo
    location
    entryDate

First i want to retrive data from Table1 and Table2. 首先,我想从表1和表2中检索数据。 2nd, i want to accumulate each columns that has same name(For example Table1.amntOne+Table2+amntOne). 第二,我要累积具有相同名称的每个列(例如Table1.amntOne + Table2 + amntOne)。 3rd, After accumulation insert all columns to the table 3. And finally, I need to create html table to fill with Table3 data. 3,累加后,将所有列插入表3。最后,我需要创建html表以填充Table3数据。

I don't have any idea where i should start with c# 我不知道我应该从C#开始

Okay, so I think I have enough to go on now. 好吧,我想我现在有足够的机会继续。 Your SQL query would look like this: 您的SQL查询如下所示:

SELECT
  ids.nameId as id,
  t1.name,
  IFNULL(t1.amntOne, 0) + IFNULL(t2.amntOne,0) as amntOne,
  IFNULL(t1.amntTwo, 0) + IFNULL(t2.amntTwo,0) as amntTwo,
  t2.location
FROM
  NameIdTable as ids LEFT JOIN Table1 as t1 ON t1.Id = ids.nameId
                     LEFT JOIN Table2 as t2 ON t2.Id = ids.nameId
WHERE
  t1.Id is not null or t2.Id is not null

This will return results where there's at least one value in Table1 (t1) or Table2 (t2). 如果表1(t1)或表2(t2)中至少有一个值,则将返回结果。 It will add the amounts. 它将添加金额。 Table2 (t2) is the only table capable of having a location so the value is taken from there. Table2(t2)是唯一能够定位的表,因此该值是从那里获取的。

The results will be your Table3. 结果将是您的Table3。 You could then use the results to generate your HTML. 然后,您可以使用结果生成HTML。 Let's say you put your results in a DataTable. 假设您将结果放入DataTable中。 That could look like this: 可能看起来像这样:

public string GetHtml(DataTable table3)
{
    var sb = new StringBuilder();
    sb.AppendLine("<table>");
    foreach(var row in table3.Rows) 
    {
        sb.AppendLine("    <tr>");
        sb.AppendLine($"        <td>{row["id"]}</td>");
        sb.AppendLine($"        <td>{row["name"]}</td>");
        sb.AppendLine($"        <td>{row["amntOne"]}</td>");
        sb.AppendLine($"        <td>{row["amntTwo"]}</td>");
        sb.AppendLine($"        <td>{row["location"]}</td>");
        sb.AppendLine("    </tr>");
    }
    sb.AppendLine("</table>");
    return sb.ToString();
}

Modify according to your need. 根据需要修改。 The values for name and location may also need to be escaped for HTML with the above approach. 使用上述方法,HTML可能还需要转义名称和位置的值。 You may also want to use an XmlDocument object to build your HTML. 您可能还需要使用XmlDocument对象来构建HTML。 This answer is just to point you in the right direction. 这个答案只是为您指出正确的方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM