在jasper报告中,在excel中命名dyamically生成的表名

我有一个要求,我需要在Excel格式生成报告。 Excel表格将会dynamic生成。 我想知道是否有一种方法来命名这些dynamic生成的工作表。使用net.sf.jasperreports.export.xls.sheet.names。{arbitrary_name}属性,我们只能命名我们知道的工作表。对于其他表jasper给出默认名称为“Page X”等等。 谢谢

您可以通过使用页眉或任何地方的线元素dynamic地给名称分隔。

注意:我们可以在元素级别设置这个属性,而不是在报表级别,这就是为什么我在我的例子中使用了行元素。

例如:

<line> <reportElement x="140" y="17" width="50" height="1"> <propertyExpression name="net.sf.jasperreports.export.xls.sheet.name"><![CDATA[$F{TERRITORY}]]></propertyExpression> </reportElement> </line> 

您可以准备一个报告列表并将其添加到JasperPrint列表中。

最后,您可以将列表中所有图纸的名称设置为:

 exporterXLS.setParameter(JRXlsAbstractExporterParameter.SHEET_NAMES, new String[] {"sheet one", "sheet two", "sheet three"}); 

其中exporterXLS是JRXlsExporter类的一个实例。

下面是我上面描述的一个更完整的例子:

 Connection con = this.jdbcTemplate.getDataSource().getConnection(); Map<String, String> hashmap = new HashMap<String, String>(); hashmap.put("ReportQuery", this.ReportQuery); JasperReport myJasperReport = JasperCompileManager.compileReport(this.reportJRXML); JasperPrint myJasperPrint = JasperFillManager.fillReport(myJasperReport, hashmap, con); List<JasperPrint> jprintList = new ArrayList<JasperPrint>(); jprintList.add(myJasperPrint); OutputStream outputfile = new FileOutputStream(new File(this.outputExcel)); JRXlsExporter exporterXLS = new JRXlsExporter(); exporterXLS.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jprintList); exporterXLS.setParameter(JRExporterParameter.OUTPUT_STREAM, outputfile); exporterXLS.setParameter(JRXlsAbstractExporterParameter.IS_COLLAPSE_ROW_SPAN, Boolean.TRUE); exporterXLS.setParameter(JRXlsAbstractExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_COLUMNS, Boolean.TRUE); exporterXLS.setParameter(JRXlsAbstractExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE); exporterXLS.setParameter(JRXlsAbstractExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE); exporterXLS.setParameter(JRXlsAbstractExporterParameter.IS_DETECT_CELL_TYPE, Boolean.FALSE); exporterXLS.setParameter(JRXlsAbstractExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE); exporterXLS.setParameter(JRXlsAbstractExporterParameter.IS_IGNORE_GRAPHICS, Boolean.TRUE); exporterXLS.setParameter(JRXlsAbstractExporterParameter.SHEET_NAMES, new String[] {"first report"}); exporterXLS.exportReport(); 

在iReportdevise器中

使用iReportdevise器,你需要遵循以下步骤:

  1. 去你的元素的属性
  2. 滚动到名为“属性”的属性
  3. 点击3个点button打开属性窗口
  4. 点击添加
  5. 在名称中input“net.sf.jasperreports.export.xls.sheet.name”
  6. 勾选“使用expression式”
  7. 按下值字段右侧的button
  8. select你的领域,variables或参数。

在JRXML中

如果您想直接在JRXML文件中进行更改,则需要查找元素并将以下propertyExpression添加到该元素中(以将字段用作表名的input):

  <propertyExpression name="net.sf.jasperreports.export.xls.sheet.name"> <![CDATA[$F{FieldName}]]> </propertyExpression>