如何创建没有硬编码的HTML HTML报告? [英] How do I create an html report without hardcoding the html?

查看:182
本文介绍了如何创建没有硬编码的HTML 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....)

我真的不喜欢这种方式,我已经做到了这一点。它的工作原理和做这项工作现在...但我不认为这是维护......特别是如果该报告成为任何更复杂在需要创建的HTML条款。更糟糕的是,在我的球队其他开发人员一定要复制和粘贴我的代码为他们的应用程序生成一个HTML报告,并有可能创造一个可怕的混乱。 (我已经看到产生了这样恐怖!想象一下,有数百个硬编码SQL的行检索报告...它足以让一个成年男子哭泣!的详细信息的报告功能)

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. 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. (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!)

不过,虽然我不喜欢这样在所有...我只是不能想用不同的方式来做到这一点。

However, while I don't like this at all...I just can't think of a different way to do it.

当然必须有办法做到这一点。我敢肯定它。不久以前,我在生成aspx页面表时,直到有人亲切地向我表明我可以只绑定对象的控制,让.NET采取的渲染照顾在做同样的事情。原来,可怕的代码,类似于上面的代码,为善良的两个或三个优雅的线条。

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. It turned horrible code, similar to the code above, into two or three elegant lines of goodness.

有谁知道创造了本报告​​中的HTML无以类似的方式硬编码的HTML?

Does anyone know of a similar way of creating the html for this report without hard-coding the html?

推荐答案

让您的应用产生原始数据的XML文件。然后施加外部XSLT把它这将包含HTML

Make your app to produce XML file with raw data. Then apply an external XSLT to it which would contain HTML.

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

这篇关于如何创建没有硬编码的HTML HTML报告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆