0

我正在使用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";
}
4

2 回答 2

1

编辑:以下解决方案仅适用于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();
于 2013-03-15T04:54:03.367 回答
0

将报告带高度调整为每个带的行的高度,确保行之间没有任何空白空间。如果您想拥有“空行”,请创建一个空文本字段,其宽度为相应波段。将报告视为关于网格的。

于 2013-03-15T12:10:58.210 回答