我需要在最后一个现有列之后设置一个名为Status
的新列,但我的代码之前是一些列,并且使用getLastColumn什么都不显示。
如何设置新单元之后最后一栏(“链接especificação”)?
private Workbook setStatus(Workbook wb, Map<String, Integer> header, List<ValidacaoResultado> validation, Planilha planilha) {
Sheet sheet = wb.getSheetAt(0);
CellStyle style;
int coluna = header.size() + 1;
List<BasicDBObject> lista = planilha.getSpec();
Map<Integer, String> items = new HashMap<>();
Map<String, String> itemsValidacao = new HashMap<>();
for (BasicDBObject itemsNome : lista) {
Map extras = (Map) itemsNome.get("extras");
items.put(Integer.parseInt(extras.get("linha").toString()) - 1, itemsNome.get("peca_nome").toString());
}
try {
for (ValidacaoResultado aValidation : validation) {
if (aValidation.getSpec() != null) {
if (aValidation.getArquivo() != null) {
String nomeOriginal = aValidation.getSpec().get("peca_nome").toString();
itemsValidacao.put(nomeOriginal, aValidation.getResultadoConsolidado());
} else {
String nomeOriginal = aValidation.getSpec().get("peca_nome").toString();
itemsValidacao.put(nomeOriginal, aValidation.getResultadoConsolidado());
}
}
}
} catch (Exception e) {
Log.error(this, "erro");
}
for (Row row : sheet) {
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
if (row.getRowNum() == LINHA_CABECALHO && cell.getColumnIndex() == header.size()) {
style = cell.getCellStyle();
Cell status = row.createCell(coluna);
status.setCellValue("Status");
status.setCellStyle(style);
}
if (cell.getColumnIndex() == coluna && row.getRowNum() > LINHA_CABECALHO) {
String peca = items.get(row.getRowNum());
String status = itemsValidacao.get(peca);
if (status != null) {
if (status.toLowerCase().contains("spec") || status.toLowerCase().contains("media")) {
status = "Não contém mídia.";
}
}
cell.setCellValue(status);
}
}
}
return wb;
}
这是结果代码
这是我将新单元格设置为row.getLastColumn + 1
时的结果
答案 0 :(得分:2)
Cell status = row.createCell(row.getLastCellNum());
这应该可以解决问题,getLastCellNum()
已经返回最后一个索引+1(等于基于1的单元格数)。
在此之后,有必要调整单元格大小和可见性:
sheet.setColumnHidden(row.getLastCellNum(), false);
sheet.autoSizeColumn(row.getLastCellNum());