2015-11-05 44 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