我正在使用extender primefaces开发JSF我发生了moficando excel我生成了默认报告,我只需要在生成的单元格中详细应用样式,我只能设置标题样式。
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 :(得分: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);
}
}
另一个评论
您正处于后处理方法中。这意味着文档,表格和单元格已经创建并填充。你不应该创建行或单元格,而只是在这里修改它们(获取它们并给出样式,修复值或标题。)。
答案 1 :(得分: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);
}
}
}