C# 如何在不硬编码 html 的情况下创建 html 报告?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/695646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I create an html report without hardcoding the html?
提问by mezoid
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. 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!)
我真的不喜欢我这样做的方式。它现在可以工作并完成工作......但我认为它不可维护......特别是如果报告在需要创建的html方面变得更加复杂。更糟糕的是,我团队中的其他开发人员肯定会为他们的应用程序复制和粘贴我的代码,这些代码会生成 html 报告,并且可能会造成可怕的混乱。(我已经看到了这种恐怖的产生!想象一个报告函数,它有数百行硬编码的 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. It turned horrible code, similar to the code above, into two or three elegant lines of goodness.
当然必须有办法做到这一点......我很确定。不久前,我在 aspx 页面中生成表格时也在做同样的事情,直到有人向我展示我可以将对象绑定到控件并让 .NET 处理渲染。它将类似于上面的代码的可怕代码变成了两三行优雅的代码。
Does anyone know of a similar way of creating the html for this report without hard-coding the html?
有没有人知道在不硬编码 html 的情况下为该报告创建 html 的类似方法?
采纳答案by GSerg
Make your app to produce XML file with raw data. Then apply an external XSLT to it which would contain HTML.
使您的应用程序使用原始数据生成 XML 文件。然后对其应用一个包含 HTML 的外部 XSLT。
More info: http://msdn.microsoft.com/en-us/library/14689742.aspx
回答by Marc Gravell
Well, you could use one of the report frameworks (Crystal, MS RDL, etc) and exportas html - however, I suspect that for simple data your current approach is less overhead. I might use an XmlWriter
or LINQ-to-XML (rather than string.Format
, which won't handle escaping)...
好吧,您可以使用其中一种报告框架(Crystal、MS RDL 等)并导出为 html - 但是,我怀疑对于简单数据,您当前的方法开销较小。我可能会使用 anXmlWriter
或 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).
等。转义对于文本值(名称、描述等)尤其重要。
回答by markt
回答by dr. evil
You might consider using a simple template engine such as http://www.stefansarstedt.com/templatemaschine.htmland 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# 功能。
回答by irperez
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 控件自动为其设置密码...
回答by irperez
meziod - Another avenue to peruse is extension methods to the HtmlTextWriter object. I found a brilliant stab at just this on this very site.
meziod - 另一种研究方法是对 HtmlTextWriter 对象的扩展方法。我在这个网站上发现了这一点。
I'm certain that you could leverage great potential from that...
我确信你可以从中发挥巨大的潜力......
regards - coola
问候 - 酷乐
回答by NotImplementedException
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.
我知道这是一个老问题,但谷歌将在未来几年继续引导人们来到这里。