Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在excel中获取所有列记录,而不是使用ApachePOI仅获取最后一列记录_Java_Apache Poi - Fatal编程技术网

Java 在excel中获取所有列记录,而不是使用ApachePOI仅获取最后一列记录

Java 在excel中获取所有列记录,而不是使用ApachePOI仅获取最后一列记录,java,apache-poi,Java,Apache Poi,我正在尝试使用ApachePOI获取excel中的所有列记录。但我只得到excel中的最后一列数据。 下面是我的代码。在下面的代码中,我试图在达到特定列数时创建新行。并在每一行中,将其视为放置所有列数据 public static ByteArrayInputStream export(List<String> records) { int TOTAL_COLUMN = 8; ByteArrayInputStream output = null; XSSFWorkbook

我正在尝试使用ApachePOI获取excel中的所有列记录。但我只得到excel中的最后一列数据。 下面是我的代码。在下面的代码中,我试图在达到特定列数时创建新行。并在每一行中,将其视为放置所有列数据

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;
}
公共静态ByteArrayInputStream导出(列表记录){
int TOTAL_COLUMN=8;
ByteArrayInputStream输出=null;
XSSF工作簿=空;
InputStream InputStream=null;
try(ByteArrayOutputStream excelFileStream=newbytearrayoutputstream();){
输入流=
新建类路径资源(模板路径).getInputStream();
工作簿=新XSSF工作簿(inputStream);
XSSFSheet sheet=workbook.getSheetAt(数据表索引);
XSSFCellStyle evenRowCellStyle=createCellStyle(工作簿,偶数行单元格颜色);
XSSFCellStyle oddRowCellStyle=createCellStyle(工作簿,奇数行单元格颜色);
整数行索引=起始行;
int numberOfColumn=0;
XSSFCellStyle cellStyle=oddRowCellStyle;
/**
*用列表数据填充行单元格详细信息
*/
int totalColumn=总列;
for(字符串数据:记录){
if(numberOfColumn==totalColumn){
rowIndex++;
numberOfColumn=0;
}
cellStyle=(行索引%2==0)?evenRowCellStyle:oddRowCellStyle;
CellUtil.createCell(sheet.createRow(rowIndex)、numberOfColumn、data、cellStyle);
numberOfColumn++;
}
workbook.write(excelFileStream);
output=newbytearrayinputstream(excelFileStream.toByteArray());
}捕获(例外e){
输出=空;
log.info(“导出到excel工作表时出错”);
}最后{
如果(工作簿!=null){
试一试{
workbook.close();
}捕获(IOE异常){
log.error(“关闭excel时出错”);
}
}
Utils.closeInputStream(inputStream);
}
返回输出;
}

上面的代码只给出每行的最后一列数据。

在您的代码中,调用
工作表。createRow(rowIndex)
总是创建一个新的空行。因此,该行中所有以前设置的单元格值都将丢失

您已经在使用
CellUtil
。有以下几点:

从电子表格中获取一行,如果该行不存在,则创建该行

这并不总是创建一个新的空行。相反,它首先尝试获取该行,并且仅在该行不存在时创建新行

使用以下工具也是如此:

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