简体   繁体   English

如何在不对HTML进行硬编码的情况下创建HTML报告?

[英]How do I create an html report without hardcoding the html?

I'm currently refactoring a console application whose main responsibility is to generate a report based on values stored in the database. 我目前正在重构一个控制台应用程序,其主要职责是根据数据库中存储的值生成报告。

The way I've been creating the report up til now is as follows: 直到现在,我一直在创建报告的方式如下:

const string format = "<tr><td>{0, 10}</td><td>
                       {1}</td><td>{2, 8}</td><td>{3}</td><td>{4, -30}</td>
                       <td>{5}</td><td>{6}</td></tr>";

if(items.Count > 0)
{
    builder.AppendLine(
        String.Format(format, "Date", "Id", "WorkItemId", 
                      "Account Number", "Name", "Address", "Description"));
}

foreach(Item item in items)
{
    builder.AppendLine(String.Format(format, item.StartDate, item.Id,
                       item.WorkItemId, item.AccountNumber, 
                       String.Format("{0} {1}", 
                                     item.FirstName, item.LastName), 
                       item.Address, item.Description));
}

string report = String.Format("<html><table border=\"1\">{0}
                                     </table></html>",
                              builder.ToString());

(The above is just a sample...and sorry about the formatting...I tried to format it so it wouldn't require horizontal scrolling....) (以上只是一个示例...对格式感到抱歉...我试图对其进行格式化,因此不需要水平滚动...。)

I really don't like that way I've done this. 我真的不喜欢这种方式。 It works and does the job for now...but I just don't think it is maintainable...particularly if the report becomes any more complex in terms of the html that needs to be created. 它现在可以工作并且可以完成工作...但是我只是认为它是不可维护的...尤其是如果该报表在需要创建的html方面变得更加复杂。 Worse still, other developers on my team are sure to copy and paste my code for their applications that generate an html report and are likely to create a horrible mess. 更糟糕的是,我们团队中的其他开发人员一定会为他们的应用程序复制并粘贴我的代码,这些应用程序会生成html报告,并可能造成可怕的混乱。 (I've already seen such horrors produced! Imagine a report function that has hundreds of lines of hard coded sql to retrieve the details of the report...its enough to make a grown man cry!) (我已经看到这种恐怖的产生!想象一下一个报表函数,其中包含数百行硬编码的sql来检索报表的详细信息……足以使一个成年男子哭泣!)

However, while I don't like this at all...I just can't think of a different way to do it. 但是,尽管我一点都不喜欢这个……我只是想不出另一种方式来做。

Surely there must be a way to do this...I'm certain of it. 当然,必须有一种方法可以做到...我确定。 Not too long ago I was doing the same thing when generating tables in aspx pages until someone kindly showed me that I can just bind the objects to a control and let .NET take care of the rendering. 不久前,在aspx页面中生成表时,我做过同样的事情,直到有人友好地告诉我,我可以将对象绑定到控件上,然后让.NET负责呈现。 It turned horrible code, similar to the code above, into two or three elegant lines of goodness. 类似于上面的代码,它把可怕的代码变成了两三行优雅的代码。

Does anyone know of a similar way of creating the html for this report without hard-coding the html? 有谁知道为该报告创建html的类似方法而无需对html进行硬编码?

Make your app to produce XML file with raw data. 使您的应用程序生成具有原始数据的XML文件。 Then apply an external XSLT to it which would contain HTML. 然后对其应用包含HTML的外部XSLT。

More info: http://msdn.microsoft.com/en-us/library/14689742.aspx 更多信息: http : //msdn.microsoft.com/en-us/library/14689742.aspx

You could use a template engine like NVelocity to separate your report view and your code. 您可以使用诸如NVelocity之类的模板引擎来分离报告视图和代码。 There are probably other decent template engines out there... 可能还有其他不错的模板引擎。

meziod - Another avenue to peruse is extension methods to the HtmlTextWriter object. 方法-细读的另一种途径是HtmlTextWriter对象的扩展方法。 I found a brilliant stab at just this on this very site. 我在这个网站上发现了一个绝妙的突破。

HtmlTextWriter extension HtmlTextWriter扩展

I'm certain that you could leverage great potential from that... 我敢肯定,您可以从中发挥巨大的潜力...

regards - coola 问候-Coola

Well, you could use one of the report frameworks (Crystal, MS RDL, etc) and export as html - however, I suspect that for simple data your current approach is less overhead. 好吧,您可以使用一种报告框架(Crystal,MS RDL等)并将其导出为html-但是,我怀疑对于简单数据而言,当前的方法开销较小。 I might use an XmlWriter or LINQ-to-XML (rather than string.Format , which won't handle escaping)... 我可能会使用XmlWriter或LINQ-to-XML(而不是无法处理转义的string.Format )...

        new XElement("tr",
            new XElement("td", item.StartDate),
            new XElement("td", item.Id),
            new XElement("td", item.WorkItemId),

etc. Escaping is especially important for text values (name, description, etc). 转义对于文本值(名称,描述等)尤其重要。

You might consider using a simple template engine such as http://www.stefansarstedt.com/templatemaschine.html and separate your template from the content. 您可以考虑使用简单的模板引擎(例如http://www.stefansarstedt.com/templatemaschine.html) ,并将模板与内容分开。

This is quite practical, allows template modification without recompiling and you still got C# power in your templates. 这非常实用,允许修改模板而无需重新编译,并且模板中仍然具有C#功能。

As coolashaka already mentioned, using the HtmlTextWriter is a good option, certainly if you add some useful extension methods, for example: 正如coolashaka已经提到的,使用HtmlTextWriter是一个不错的选择,当然,如果您添加了一些有用的扩展方法,例如:

Simple example: 简单的例子:

public static void WriteNav(this
     HtmlTextWriter writer, List<String> navItems)
    {
        writer.RenderBeginTag("nav");
        writer.RenderBeginTag(HtmlTextWriterTag.Ul);
        foreach (var item in navItems)
        {
            writer.RenderBeginTag(HtmlTextWriterTag.Ul);
            writer.AddAttribute(HtmlTextWriterAttribute.Href, "~/" + item + ".html");
            writer.RenderBeginTag(HtmlTextWriterTag.A);
            writer.Write(item);
            writer.RenderEndTag();
            writer.RenderEndTag();
        }
        writer.RenderEndTag();
        writer.RenderEndTag();

    }

I know this is an old question, but Google will keep directing people here for years to come. 我知道这是一个老问题,但是Google会在未来几年内继续指导人们。

Microsoft SQL Reporting Services does this quite well, and can do multiple formats. Microsoft SQL Reporting Services可以很好地做到这一点,并且可以执行多种格式。

My company uses it to create PDF reports and since we have HIPAA requirements, we automatically put a password to it via a third party PDF control... 我的公司使用它来创建PDF报告,并且由于我们有HIPAA要求,因此我们会通过第三方PDF控件自动为其输入密码...

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

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