我正在使用来自JDBC数据源的JasperReportBuilder构建报告,然后通过使用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";
}

最佳答案

编辑:下面的解决方案仅适用于DynamicJasper库,而不适用于DynamicReports。我相信DR API中也必须有类似的操作(虽然不确定-我对DR的了解仅是非常有限的:()。

必须将DynamicReportBuilder :: setHeaderHeight(..),setDetailHeight(..)和JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN参数设置组合在一起吗?

// build dynamicreport instance
DynamicReportBuilder dynamicReportBuilder = new DynamicReportBuilder();

dynamicReportBuilder.setHeaderHeight(50); //sets the column header height
dynamicReportBuilder.setDetailHeight(50); //sets the detail band rows height

DynamicReport dynamicReport = dynamicReportBuilder.build();

// build jasperprint..
JasperPrint jasperPrint = DynamicJasperHelper.generateJasperPrint(
    dynamicReport, new ClassicLayoutManager(), dataSource, new HashMap<String, Object>());

// export as html
JRExporter exporter = new JRHtmlExporter();

// tell jasper not to use images for aligning
exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE);

exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, targetFile);

exporter.exportReport();

关于java - JasperReportBuilder HTML行高,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15369083/

10-09 09:35