[转载]使用NVelocity制作HTML作报表

[转载]使用HTML作报表 - 瑞雪年 - 博客园.

NVelocity 可以算上castle一个比较重头的组件了,TemplateEngine Component 是NVelocity的一个默认的应用实现,使用它可以 轻松使用HTML作为模板,生成报表。

组件下载地址:http://www.castleproject.org/castle/download.html

当然,报表都少不了导出PDF,这一还要借助另外一个开源组件:iText, 它的.NET移植版iText.NET 还 有一个C#重制版iTextSharp ,iText.NET的作者在项目介绍中还推荐在新项目中使用iTextSharp了。

示例:

报表模版可以这样写:

代码

<html>
<head>
<link rel="stylesheet" type="text/css" href="$CssName">
</link>
</head>
<body>
<div>Header</div>
<br/>

<table border="0" cellpadding="0" cellspacing="0" style="margin:0;padding:0">

#foreach ( $dr in $Dt.Rows)
#set($name=$dr.get_Item(3).ToString().Trim().Replace(" ", "%20"))

<tr>
<td style='padding:5px'
onmousedown="window.location.href('$name')"
onmouseover
="style.backgroundColor='#DFEEEE';"
onmouseout
="style.backgroundColor='#FFFFFF';">
<span style="font-size: 20px; color: #0000FF">Compound Name: </span><span style="font-size: 20px; color: #FF0000">$dr.get_Item(3)</span>
<br/><b><span>Drug Alias: </span></b><span>$dr.get_Item(4)</span>
<br/><b><span>Category: </span></b><span>$dr.get_Item(2)</span>
</td></tr>

#end
</table>
<br/>

<div>Bottom
</div>
</body>
</html>

代码:

报表预览:

代码


protected void Button2_Click(object sender, EventArgs e)
{
DataTable dataTable
= GetDataTable();
string output = ProcessTemplate(dataTable);
Response.Write(output);
Response.Flush();
Response.Close();
}

private string ProcessTemplate(DataTable dataTable)
{
INVelocityEngine velocityEngine
= NVelocityEngineFactory.CreateNVelocityFileEngine(Server.MapPath("~/Templates"), true);
System.Collections.Hashtable context
= new System.Collections.Hashtable();
context.Add(
"CssName", ResolveAbsoluteUrl("~/css/default.css"));
context.Add(
"Dt", dataTable);
string output = velocityEngine.Process(context, "temp.htm");
return output;
}

private DataTable GetDataTable()
{
DataTable dataTable
= new DataTable();
dataTable.Columns.Add(
"a");
dataTable.Columns.Add(
"b");
dataTable.Columns.Add(
"c");
dataTable.Columns.Add(
"d");
dataTable.Columns.Add(
"e");
for (int i = 0; i < 10; i++)
{
DataRow dataRow
= dataTable.NewRow();
for (int j = 0; j < dataTable.Columns.Count; j++)
{
dataRow[j]
= string.Format("value: {0}, {1}", i, j);
}
dataTable.Rows.Add(dataRow);
}
return dataTable;
}

导出PDF:

代码


protected void Button5_Click(object sender, EventArgs e)
{
System.Reflection.Assembly.Load(
"Apache.Xml.Commons");
System.Reflection.Assembly.Load(
"Apache.Crimson");
System.Reflection.Assembly.Load(
"iTextAsian");

DataTable dataTable = GetDataTable();
string output = ProcessTemplate(dataTable);
string fileName = Server.MapPath("~/Temp/temp.htm");
System.IO.File.WriteAllText(fileName, output, System.Text.ASCIIEncoding.ASCII);

MemoryStream stream = new MemoryStream();
com.lowagie.text.Document document
= new com.lowagie.text.Document(PageSize.A4, 80, 50, 30, 65);
PdfWriter.getInstance(document, stream);
HtmlParser.parse(document, fileName);

document.close();
stream.Flush();
byte[] bytes = stream.ToArray();
if (bytes != null)
{
Page.Response.Clear();
Page.Response.ContentType
= "application/pdf";
Page.Response.AppendHeader(
"Content-Disposition", "attachment;filename=temp.pdf");
Page.Response.BinaryWrite(bytes);
Page.Response.End();
}
}