我正在使用JasperReportBuilder从 JDBC 数据源构建报告,然后通过使用JasperHtmlExporterBuilder对象参数调用toHtml方法将其导出为 HTML。我一直在尝试消除每一行、列标题行甚至我的标题中文本上方存在的额外空间。我已经尝试了我能想到的一切,并且一直在网上搜索无济于事。
似乎大多数人都有相反的问题:他们需要增加行以适应他们的文本。我想强制我的行成为其内容的高度。如果那不可能,我会很高兴限制我的行的高度。
HTML 导出器似乎通过插入图像并设置其高度来控制我的行的高度。
任何帮助,将不胜感激。谢谢!
编辑:这是我的代码。
String imageServletUrl = "images?image=";
StringWriter writer = new StringWriter();
JasperHtmlExporterBuilder htmlExporter = Exporters.htmlExporter(writer);
htmlExporter.setImagesURI(imageServletUrl);
SqlRowSet rowSet = getData(databaseIpAddr, portNumber, databaseName, databaseUser, databasePassword);
JRDataSource ds = createDataSource(rowSet);
JasperReportBuilder builder = DynamicReports.report();
IntegerType intType = DynamicReports.type.integerType();
DateType dateType = DynamicReports.type.dateType();
int rowHeightPx = 20;
TextColumnBuilder<Integer> col1 = col
.column(COL_TITLE_RESULT_NUM, RESULTS_COL_TITLE_RESULT_NUM, intType)
.setWidth(35)
.setFixedHeight(rowHeightPx)
.setStretchWithOverflow(true);
... create other eight columns ...
StyleBuilder titleStyle = stl.style()
.bold()
.setFontSize(22)
.setHorizontalAlignment(HorizontalAlignment.CENTER);
StyleBuilder columnTitleStyle = stl.style()
.bold()
.setAlignment(HorizontalAlignment.CENTER, VerticalAlignment.MIDDLE)
.setBorder(stl.pen1Point());
StyleBuilder columnStyle = stl.style()
.setAlignment(HorizontalAlignment.CENTER, VerticalAlignment.MIDDLE)
.setLineSpacing(LineSpacing.SINGLE)
.setBorder(stl.pen1Point());
TextFieldBuilder<String> titleBuilder = DynamicReports.cmp
.text(selectedAgency)
.setStyle(titleStyle)
.setFixedHeight(20);
TextFieldBuilder<String> subtitleBuilder = DynamicReports.cmp
.text("Start Time: " + startDate + " " + getStartTime() + ", End Time: " + endDate + " " + getEndTime())
.setFixedHeight(20);
TextFieldBuilder<String> noDataMsgBuilder = DynamicReports.cmp
.text(NO_DATA_MSG)
.setStyle(columnStyle);
builder
.title(titleBuilder)
.addPageHeader(subtitleBuilder)
.setColumnTitleStyle(columnTitleStyle)
.columns(col1, col2, col3, col4, col5, col6, col7, col8, col9)
.setColumnStyle(columnStyle)
.highlightDetailOddRows()
.noData(titleBuilder, subtitleBuilder, noDataMsgBuilder)
.setWhenNoDataType(WhenNoDataType.NO_DATA_SECTION);
String reportHtml;
try {
// this seems to be required to get to the DynamicReports images
getContext().getRequest().getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, builder.toJasperPrint());
builder.toHtml(htmlExporter);
reportHtml = writer.toString();
}
catch (DRException e)
{
e.printStackTrace();
reportHtml = "There was an error generating the report";
}