gpt4 book ai didi

html - 导出自定义 HTML 模板

转载 作者:可可西里 更新时间:2023-11-01 13:33:10 26 4
gpt4 key购买 nike

背景

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

<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>

问题

报表在页面上居中,但应该左对齐。

原解

使用 JRHtmlExporter 的 HTML_HEADER 参数看起来很有前途,但类已deprecated .这是解决方案:

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.HtmlExporternet.sf.jasperreports.export.SimpleHtmlReportConfiguration 类:

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 参数替换为:

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 内容使用外部资源(例如数据库、文件或网页),而不是硬编码字符串。

关于html - 导出自定义 HTML 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24612975/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com