简体   繁体   English

获取 excel 中的所有列记录,而不是使用 apache poi 仅获取最后一列记录

[英]get all column records in excel instead of getting only last column record using apache poi

i am trying to get all column records in excel using apache poi.我正在尝试使用 apache poi 获取 excel 中的所有列记录。 But i am getting only last column data in excel.但我只得到 excel 中的最后一列数据。 Below is my code.下面是我的代码。 In below code i am trying to create new row when it reaches specific number of columns.在下面的代码中,我试图在达到特定列数时创建新行。 and in each row it as to put all column data.并在每一行中放置所有列数据。

public static ByteArrayInputStream export(List<String> records) {
  int TOTAL_COLUMN = 8;
  ByteArrayInputStream output = null;
  XSSFWorkbook workbook = null;
  InputStream inputStream = null;
  try (ByteArrayOutputStream excelFileStream = new ByteArrayOutputStream();) {
     inputStream =
           new ClassPathResource(TEMPLATE_PATH).getInputStream();
     workbook = new XSSFWorkbook(inputStream);
     XSSFSheet sheet = workbook.getSheetAt(DATA_SHEET_INDEX);
     XSSFCellStyle evenRowCellStyle = createCellStyle(workbook, EVEN_ROW_CELL_COLOR);
     XSSFCellStyle oddRowCellStyle = createCellStyle(workbook, ODD_ROW_CELL_COLOR);

     Integer rowIndex = STARTING_ROW;
     int numberOfColumn = 0;
     XSSFCellStyle cellStyle = oddRowCellStyle;

     /**
      * Populates row cell details with list data
      */
     int totalColumn = TOTAL_COLUMN;

     for (String data : records) {
        if (numberOfColumn == totalColumn) {
           rowIndex++;
           numberOfColumn = 0;
        }
        cellStyle = (rowIndex % 2 == 0) ? evenRowCellStyle : oddRowCellStyle;
        CellUtil.createCell(sheet.createRow(rowIndex), numberOfColumn, data, cellStyle);
        numberOfColumn++;
     }

     workbook.write(excelFileStream);
     output = new ByteArrayInputStream(excelFileStream.toByteArray());
  } catch (Exception e) {
     output = null;
     log.info("Error occurred while exporting to excel sheet.");
  } finally {
     if (workbook != null) {
        try {
           workbook.close();
        } catch (IOException e) {
           log.error("Error occurred while closing excel.");
        }
     }
     Utils.closeInputStream(inputStream);
  }
  return output;
}

above code is giving only last column data in each row.上面的代码只给出每行的最后一列数据。

In your code the call sheet.createRow(rowIndex) always creates a new empty row.在您的代码中,调用sheet.createRow(rowIndex)总是创建一个新的空行。 So all formerly set cell values in that row get lost.因此,该行中所有以前设置的单元格值都会丢失。

You are using CellUtil already.您已经在使用CellUtil There is CellUtil.getRow what does the following:CellUtil.getRow执行以下操作:

Get a row from the spreadsheet, and create it if it doesn't exist.从电子表格中获取一行,如果它不存在则创建它。

This not always creates a new empty row.这并不总是会创建一个新的空行。 Instead it tries to get the row at first and only creates a new row if the row does not exists already,相反,它首先尝试获取该行,并且仅在该行不存在时才创建一个新行,

So do using:所以使用:

CellUtil.createCell(CellUtil.getRow(rowIndex, sheet), numberOfColumn, data, cellStyle);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM