导出自定义HTML模板 [英] Export custom HTML template

查看:269
本文介绍了导出自定义HTML模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

导出为HTML会生成以下内容:

Exporting to HTML generates the following:

<html>
    <head>
        <title></title>
        <meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
        <style type="text/css">
            a {text-decoration: none}
        </style>
    </head>
    <body vlink="#000000" text="#000000" link="#000000" alink="#000000">
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tbody>
                <tr>
                    <td width="50%">&nbsp;</td>
                    <td align="center">
                        <!-- The report -->
                    </td>
                    <td width="50%">&nbsp;</td>
                </tr>
            </tbody>
        </table>
    </body>
</html>



问题



报告以

Problem

The report is centered on the page, but should be left-aligned.

使用JRHtmlExporter的HTML_HEADER参数看起来很有前途,但是这些课程已经已弃用。这是解决方案:

Using JRHtmlExporter's HTML_HEADER parameter looked promising, but the classes have been deprecated. This was the solution:

JRHtmlExporter exporter = new JRHtmlExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRHtmlExporterParameter.HTML_HEADER, 
    "<html>"+
    "<head>"+
    "  <title></title>"+
    "  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>"+
    "  <link rel=\"stylesheet\" type=\"text/css\" href=\"css/jasper.css\" />"+
    "  <style type="text/css">"+
    "    a {text-decoration: none}"+
    "  </style>"+
    "</head>"+
    "<body text="#000000" link="#000000" alink="#000000" vlink="#000000">"+
    "<table width="100%" cellpadding="0" cellspacing="0" border="0">"+
    "<tr><td width="50%">&nbsp;</td><td align="center">");
exporter.exportReport();

现在我必须使用 net.sf.jasperreports.export.HtmlExporter net.sf.jasperreports.export.SimpleHtmlReportConfiguration 类,这样:

Now I have to use the net.sf.jasperreports.export.HtmlExporter and net.sf.jasperreports.export.SimpleHtmlReportConfiguration classes, in this way:

HtmlExporter exporterHTML = new HtmlExporter();
SimpleExporterInput exporterInput = new SimpleExporterInput(report.getJasperPrint());
exporterHTML.setExporterInput(exporterInput);
HtmlExporterOutput exporterOutput = new SimpleHtmlExporterOutput(out);
exporterHTML.setExporterOutput(exporterOutput );

SimpleHtmlReportConfiguration reportExportConfiguration = new SimpleHtmlReportConfiguration();
reportExportConfiguration.setWhitePageBackground(false);
reportExportConfiguration.setRemoveEmptySpaceBetweenRows(true);
exporterHTML.setConfiguration(reportExportConfiguration);

exporterHTML.exportReport(); 

您如何解决这个问题?


  • JasperReports v5.5.2

  • Java v1.6.0_38

推荐答案

JRHtmlExporterParameter的 HTML_HEADER 参数被替换为: p>

The HTML_HEADER parameter of JRHtmlExporterParameter was replaced with:


HtmlExporterConfiguration.getHtmlHeader()

例如:

public byte[] exportHtml(final JasperPrint print) {
    final Exporter exporter = new HtmlExporter();
    final ByteArrayOutputStream out = new ByteArrayOutputStream();

    exporter.setConfiguration(createHtmlConfiguration());
    exporter.setExporterOutput(new SimpleHtmlExporterOutput(out));
    exporter.setExporterInput(new SimpleExporterInput(print));
    exporter.exportReport();

    return out.toByteArray();
}

private HtmlExporterConfiguration createHtmlConfiguration()
        throws IOException {
    SimpleHtmlExporterConfiguration shec
            = new SimpleHtmlExporterConfiguration();

    shec.setHtmlHeader(getHtmlHeader());
    shec.setHtmlFooter(getHtmlFooter());

    return shec;
}

private String getHtmlHeader() {
    StringBuffer sb = new StringBuffer();
    sb.append("<html>");
    sb.append("<head>");
    sb.append("  <title></title>");
    sb.append("  <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>");
    sb.append("  <style type=\"text/css\">");
    sb.append("    a {text-decoration: none}");
    sb.append("  </style>");
    sb.append("</head>");
    sb.append("<body text=\"#000000\" link=\"#000000\" alink=\"#000000\" vlink=\"#000000\">");
    sb.append("<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">");
    sb.append("<tr><td align=\"left\">");

    return sb.toString();
}

private String getHtmlFooter() {
  // Close the opening tags from getHtmlHeader()...
}

更好的是使用HTML内容的外部资源(如数据库,文件或网页),而不是硬编码字符串。

Even better would be to use external resources (such as a database, file, or web page) for the HTML content, rather than hard-coded strings.

这篇关于导出自定义HTML模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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