以C#编程方式生成HTML,定位打印报告

时间:2021-06-24 08:18:57

I've taken over a C# (2.0) code base that has the ability to print information. The code to do this is insanely tedious. Elements are drawn onto each page, with magic constants representing positioning. I imagine the programmer sitting with a ruler, designing each page by measuring and typing in the positions. And yes, one could certainly come up with some nice abstractions to make this approach rational. But I am looking at a different method.

我已经接管了一个能够打印信息的C#(2.0)代码库。执行此操作的代码非常繁琐。元素被绘制到每个页面上,魔术常量代表定位。我想程序员坐着一把尺子,通过测量和输入位置来设计每一页。是的,人们当然可以提出一些很好的抽象来使这种方法合理化。但我正在寻找一种不同的方法。

The idea is that I'll replace the current code that prints with code that generates static HTML pages, saves them to a file, and then launches the web browser on that file. The most obvious benefit is that I don't have to deal with formatting-- I can let the web browser do that for me with tags and CSS.

我的想法是,我将使用生成静态HTML页面的代码替换当前打印的代码,将它们保存到文件中,然后在该文件上启动Web浏览器。最明显的好处是我不需要处理格式化 - 我可以让网页浏览器为我做标记和CSS。

So what I am looking for is a very lightweight set of classes that I can use to help generate HTML. I don't need anything as heavyweight as HTMLTextWriter. What I'm looking for is something to avoid fragments like this:

所以我正在寻找的是一组非常轻量级的类,我可以用来帮助生成HTML。我不需要像HTMLTextWriter那样重量级的东西。我正在寻找的东西是避免像这样的碎片:

String.Format("<tr><td>{0}</td><td>{1}</td></tr>", foo, bar);

And instead take have this kind of feel:

而是采取这种感觉:

...
table().
    tr().
        td(foo).
        td(bar)

Or something like that. I've seen lightweight classes like that for other languages but can't find the equivalent (or better) for C#. I can certainly write it myself, but I'm a firm believer in not reinventing wheels.

或类似的东西。我见过其他语言的轻量级类,但找不到C#的等价(或更好)。我当然可以自己写,但我坚信不要重新发明*。

Know anything like this? Know anything better than this?

知道这样的事吗?知道什么比这更好吗?

2 个解决方案

#1


1  

Just as an idea: why do you want to assemble the HTML in your applications code? Sounds a bit tedious to me. You could aggregate the data needed for the report and pass this on to one of the template engines (that are "normally" used for web apps) existing for C#. Then you save the result of the template engine to a html file.

同样的想法:为什么要在应用程序代码中组装HTML?对我来说听起来有点乏味。您可以聚合报告所需的数据,并将其传递给C#现有的模板引擎(通常用于Web应用程序)。然后将模板引擎的结果保存到html文件中。

The main benefits I see with this approach:

我看到这种方法的主要好处:

  • separates view from business logic
  • 将视图与业务逻辑分开
  • html templates can be edited by non-C# developers, html/css knowledge is enough
  • html模板可以由非C#开发人员编辑,html / css知识就足够了
  • no need to recompile the application if the html changes
  • 如果html发生更改,则无需重新编译应用程序

I havent used it yet, but I heard that the Spark View Engine is OK: http://sparkviewengine.com/ (not sure if it is for C# 2.0 though)

我还没有使用它,但我听说Spark View引擎是可以的:http://sparkviewengine.com/(不确定它是否适用于C#2.0)

Some time ago I experimented (in PHP) with Gagawa ( http://code.google.com/p/gagawa/ ), where you can do stuff like:

前段时间我用Gagawa(http://code.google.com/p/gagawa/)试验(在PHP中),你可以在这里做以下事情:

$div = new Div();
$div->setId("mydiv")->setCSSClass("myclass");

$link = new A();
$link->setHref("http://www.example.com")->setTarget("_blank");

$div->appendChild( $link );

But soon dropped such an approach in favor of an template engine.

但很快就采用了这种方法来支持模板引擎。

#2


0  

Another approach is converting the data to XML and applying an XSL stylesheet. In order to change the HTML formating you just need to replace the stylesheet.

另一种方法是将数据转换为XML并应用XSL样式表。要更改HTML格式,您只需要替换样式表。

#1


1  

Just as an idea: why do you want to assemble the HTML in your applications code? Sounds a bit tedious to me. You could aggregate the data needed for the report and pass this on to one of the template engines (that are "normally" used for web apps) existing for C#. Then you save the result of the template engine to a html file.

同样的想法:为什么要在应用程序代码中组装HTML?对我来说听起来有点乏味。您可以聚合报告所需的数据,并将其传递给C#现有的模板引擎(通常用于Web应用程序)。然后将模板引擎的结果保存到html文件中。

The main benefits I see with this approach:

我看到这种方法的主要好处:

  • separates view from business logic
  • 将视图与业务逻辑分开
  • html templates can be edited by non-C# developers, html/css knowledge is enough
  • html模板可以由非C#开发人员编辑,html / css知识就足够了
  • no need to recompile the application if the html changes
  • 如果html发生更改,则无需重新编译应用程序

I havent used it yet, but I heard that the Spark View Engine is OK: http://sparkviewengine.com/ (not sure if it is for C# 2.0 though)

我还没有使用它,但我听说Spark View引擎是可以的:http://sparkviewengine.com/(不确定它是否适用于C#2.0)

Some time ago I experimented (in PHP) with Gagawa ( http://code.google.com/p/gagawa/ ), where you can do stuff like:

前段时间我用Gagawa(http://code.google.com/p/gagawa/)试验(在PHP中),你可以在这里做以下事情:

$div = new Div();
$div->setId("mydiv")->setCSSClass("myclass");

$link = new A();
$link->setHref("http://www.example.com")->setTarget("_blank");

$div->appendChild( $link );

But soon dropped such an approach in favor of an template engine.

但很快就采用了这种方法来支持模板引擎。

#2


0  

Another approach is converting the data to XML and applying an XSL stylesheet. In order to change the HTML formating you just need to replace the stylesheet.

另一种方法是将数据转换为XML并应用XSL样式表。要更改HTML格式,您只需要替换样式表。