2015-11-05 130 views
2

我是新来的碧玉。我的项目使用jasper创建了一个仅包含列名(例如:名称,年龄,部门,位置)的excel模板,该模板使用jrxml进行字体和对齐。 [基本上我们用来显示列名称]如何使用jrxml(jasper api)在excel中显示下拉列表?

用户可以下载模板,他们可以输入他们想要的值。

现在为了避免用户通过输入值手动输入详细信息,我想在模板中给一些硬编码值下拉列表。例如,对于“位置”字段,我可以设置像'Texas','California','FortWorth'等值。我不是从DB查询这些值,我只是想在.jrxml中对这些值进行硬编码。我必须创建一个行,其中位置列单独应该有下拉值,用户可以从中选择一个并上传到我的应用程序

在下载的excel中,我想要一个包含上述值的下拉列表,以便用户可以选择而不是打字自己。

有什么办法可以把它放在.jrxml中。如果这是不可能的,那么给出可以在Excel中下拉的代码。

我对一个领域样品的.jrxml是

<staticText> 
    <reportElement mode="Opaque" x="684" y="0" width="114" height="20" backcolor="#808080"> 
    </reportElement> 
    <box leftPadding="10"> 
     <pen lineColor="#000000" /> 
     <topPen lineWidth="0.5" /> 
     <leftPen lineWidth="0.5" /> 
     <bottomPen lineWidth="0.5" /> 
     <rightPen lineWidth="0.5" /> 
    </box> 
    <textElement> 
     <font size="10" isBold="true" /> 
    </textElement> 
    <text><![CDATA[Location]]></text> 
</staticText> 

请让我知道更多的细节需要

+0

嗨罗伯特,我读到了那篇文章,但我不使用碧玉或iReports在我的应用程序。我的应用程序中有jasperreport.jar和jrxml。所以我无法获得jrxml中的等效代码,该代码将显示excel中的组合框 – user1912882

+0

Petter Friberg如果您可以共享模板示例,我可以将带有硬编码值的组合框放在模板本身中,这将非常棒。我急切地等待着模板 – user1912882

回答

1

我不认为目前的碧玉API 6.0,这是不可能性,但与POI(已经在你的类路径中)在导出后添加这个很容易。

首先,我们出口或JasperPrintprint,到Excel(包括自其最好的代码以删除空行,我们将与POI以后再处理,Exception处理超出了本示例)

JRXlsExporter exporter = new JRXlsExporter(); 
File outputFile = new File("excelTest.xls"); 
exporter.setExporterInput(new SimpleExporterInput(print)); 
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outputFile)); 
SimpleXlsReportConfiguration configuration = new SimpleXlsReportConfiguration(); 
configuration.setOnePagePerSheet(false); 
configuration.setDetectCellType(true); 
configuration.setCollapseRowSpan(false); 
configuration.setWhitePageBackground(false); 
configuration.setRemoveEmptySpaceBetweenColumns(true); 
configuration.setRemoveEmptySpaceBetweenRows(true); 
exporter.setConfiguration(configuration); 
exporter.exportReport(); 

既然报告出口到excel,我们用POI重新打开它并且我们的数据验证

  1. 查询数据库再次
  2. 使用计数beforeDetailEval()多少次叫
  3. 使用POI找到最后一个小脚本:

    int rowCount = 50; //This will be discussed at end. 
    int cellWithDV = 1; 
    
    HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(outputFile)); 
    HSSFSheet sheet = workbook.getSheetAt(0); 
    CellRangeAddressList addressList = new CellRangeAddressList(1,rowCount, cellWithDV, cellWithDV); 
    DVConstraint dvConstraint = DVConstraint.createExplicitListConstraint(
            new String[]{ "Texas" , "California", "FortWorth"}); 
    DataValidation dataValidation = new HSSFDataValidation(addressList, dvConstraint); 
    dataValidation.setSuppressDropDownArrow(false); 
    sheet.addValidationData(dataValidation); 
    workbook.write(new FileOutputStream(outputFile)); 
    workbook.close(); 
    

    rowCount可以通过achived行。在Excel(2003)

  4. 使用最大值为65536

希望这有助于

+0

它回答了我的问题。谢谢 – user1912882

相关问题