JasperReportBuilder HTML行高

时间:2013-03-12 18:21:26

标签: java html dynamic-reports

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

2 个答案:

答案 0 :(得分: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();

答案 1 :(得分:0)

为每个乐队调整报告带高度到你的行的高度,确保你的行之间没有任何空格。如果你想让“emty row”创建一个宽度为相应波段的emty文本字段。将报告视为关于网格。