使用HTML作報表

來源:互聯網
上載者:User

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();
        }
    }

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.