2013-12-23 26 views
0

我正在開發帶擴展符的JSF發生我是moficando excel我生成的默認報告,我只需要在生成的單元格中詳細應用樣式,我只能對樣式頭進行樣式設置。將方法getPhysicalNumberOfCells應用於使用POI生成的單元格?

private void columnsCustomers() { 
    this.setColumnCustomer(new ArrayList<ValidColumnKey>());   
    this.getColumnCustomer().add(new ValidColumnKey(1, "Codigo", "code")); 
    this.getColumnCustomer().add(new ValidColumnKey(2, "Nombre", "name")); 
    this.getColumnCustomer().add(new ValidColumnKey(3, "Nombre Comercial", "comercialName")); 
    this.getColumnCustomer().add(new ValidColumnKey(4, "Estado", "isActive")); 
} 

public void postProcessXLS(Object document) { 
    HSSFWorkbook wb = (HSSFWorkbook) document; 
    HSSFSheet sheet = wb.getSheetAt(0); 
    //HSSFSheet sheet = wb.createSheet(getCustomer().getName()); 
    HSSFRow header = sheet.getRow(0); 

    HSSFRow rowUser0 = sheet.createRow((short) 0); 

    HSSFCellStyle styleHeader = (HSSFCellStyle) wb.createCellStyle(); 
    styleHeader.setAlignment(HSSFCellStyle.ALIGN_CENTER); 
    HSSFFont fontHeader = (HSSFFont) wb.createFont(); 
    fontHeader.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); 
    fontHeader.setColor(HSSFColor.WHITE.index); 
    styleHeader.setFillForegroundColor(HSSFColor.DARK_BLUE.index);   
    styleHeader.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);   
    styleHeader.setFont(fontHeader); 
    styleHeader.setBorderBottom((short) 1); 
    styleHeader.setBorderLeft((short) 1);   
    styleHeader.setBorderRight((short) 1); 
    styleHeader.setBorderTop((short) 1); 

    HSSFCell indice = rowUser0.createCell((short) 0); 
    indice.setCellValue("N°"); 
    indice.setCellStyle(styleHeader); 

    int nro = 1; 
    for(ValidColumnKey column : this.getColumnCustomer()){ 
     HSSFCell hnro = rowUser0.createCell((short) nro); 
     hnro.setCellValue(column.getDescripcion()); 
     hnro.setCellStyle(styleHeader); 
     nro++;   
    } 

    HSSFCellStyle styleCellWhite = (HSSFCellStyle) wb.createCellStyle(); 
    HSSFFont fontCellWhite = (HSSFFont) wb.createFont(); 
    fontCellWhite.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); 
    styleCellWhite.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); 
    styleCellWhite.setBorderBottom((short) 1); 
    styleCellWhite.setBorderLeft((short) 1);    
    styleCellWhite.setBorderRight((short) 1); 
    styleCellWhite.setBorderTop((short) 1); 

    for(int i=0; i < header.getPhysicalNumberOfCells();i++) { 
     HSSFCell cell = header.getCell(i);  
     cell.setCellStyle(styleHeader); 
    } 
} 

圖片:http://s2.subirimagenes.com/otros/previo/thump_8748813excelpoi.jpg

正如你在圖像中看到的只是缺少在具體應用練成邊緣的風格,我申請的風格,但顯然不起作用,該方法將header.getPhysicalNumberOfCells

有人能指導我,他們非常感激。

+0

我也想念你表示,也想把名稱放到Excel電子表格中,想用這個代碼做,但我是個例外:HSSFSheet sheet = wb.createSheet(「Customer」); – user3123910

+0

你想做什麼? header.getPhysicalNumberOfCells循環遍歷頭部的每個單元格(在Excel中第一行),並強制樣式'styleHeader'到每個單元格。這不是必需的,因爲您在創建標題單元格時已經這樣做了。你也不用styleCellWhite。請澄清。 –

回答

1

對於您的圖紙問題:在此步驟中,PF已經創建了工作簿和圖紙。你可能想簡單地(重新)命名錶:

wb.setSheetName(0, getCustomer().getName()); 

您的其他問題(不清楚所有在這一刻),我想你想格式化的數據值,使用你的風格styleCellWhite。這裏是一個辦法做到這一點:

//iterates lines, then iterate each column giving style to each cell 
for (int r=1; r<sheet.getLastRowNum(); r++) { 
    HSSFRow row = sheet.getRow(r); 
    for (int i=0; i<row.getPhysicalNumberOfCells(); i++) { 
     row.getCell(i).setCellStyle(styleCellWhite); 
    } 
} 

另一句話

你是一個在後處理方法。這意味着文檔,工作表和單元格已經被創建和填充。你不應該創建行或單元格,而只是在這裏修改它們(獲取它們並給出樣式,修正值或標題)。

0

感謝您的幫助,當然不是你評我生成行和列的動態表格形式:

<p:dataTable id="listCust" var="cust" value="#{mantClienteMB.customers}" rows="5" 
    rowIndexVar="rowIndex" paginatorPosition="top" resizableColumns="true" emptyMessage=""> 

    <p:column headerText="N°" width="auto"> 
     <h:outputText value="#{rowIndex+1}" /> 
    </p:column> 
    <c:forEach items="#{mantClienteMB.columnCustomer}" var="colum"> 
     <p:column headerText="#{colum.descripcion}" width="auto">   
      <h:outputText value="#{cust[colum.entidadBean]}" />         
     </p:column> 
    </c:forEach>    
</p:dataTable> 

這意味着你真的有兩個列表,我決定無論如何這樣的:

public void postProcessXLS(Object document) { 
    HSSFWorkbook wb = (HSSFWorkbook) document; 
    wb.setSheetName(0, this.getCustomer().getClass().getSimpleName()); 
    HSSFSheet sheet = wb.getSheetAt(0); 

    sheet.setColumnWidth(0, 1500); 
    sheet.setColumnWidth(1, 4500); 
    sheet.setColumnWidth(2, 6500); 
    sheet.setColumnWidth(3, 6500); 
    sheet.setColumnWidth(4, 6500);  

    HSSFRow rowUser0 = sheet.createRow((short) 0); 

    HSSFCellStyle styleHeader = (HSSFCellStyle) wb.createCellStyle(); 
    styleHeader.setAlignment(HSSFCellStyle.ALIGN_CENTER); 
    HSSFFont fontHeader = (HSSFFont) wb.createFont(); 
    fontHeader.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); 
    fontHeader.setColor(HSSFColor.WHITE.index); 
    styleHeader.setFillForegroundColor(HSSFColor.DARK_BLUE.index);   
    styleHeader.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);   
    styleHeader.setFont(fontHeader); 
    styleHeader.setBorderBottom((short) 1); 
    styleHeader.setBorderLeft((short) 1);   
    styleHeader.setBorderRight((short) 1); 
    styleHeader.setBorderTop((short) 1); 

    HSSFCell indice = rowUser0.createCell((short) 0); 
    indice.setCellValue("N°"); 
    indice.setCellStyle(styleHeader); 

    int nro = 1; 
    for(ValidColumnKey column : this.getColumnCustomer()){ 
     HSSFCell hnro = rowUser0.createCell((short) nro); 
     hnro.setCellValue(column.getDescripcion()); 
     hnro.setCellStyle(styleHeader); 
     nro++;   
    } 

    HSSFCellStyle styleCellWhite = (HSSFCellStyle) wb.createCellStyle(); 
    HSSFFont fontCellWhite = (HSSFFont) wb.createFont(); 
    fontCellWhite.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); 
    styleCellWhite.setBorderBottom((short) 1); 
    styleCellWhite.setBorderLeft((short) 1);    
    styleCellWhite.setBorderRight((short) 1); 
    styleCellWhite.setBorderTop((short) 1); 

    for (int r=1; r<=this.getCustomers().size(); r++) { 
     HSSFRow row = sheet.getRow(r); 
     for (int i=0; i<row.getPhysicalNumberOfCells(); i++) { 
      row.getCell(i).setCellStyle(styleCellWhite); 
     } 
    } 

} 
相關問題