0

我需要读取一个包含 3 张工作表的 Excel 文件,并且我需要将数据表示如下:例如,每行跨越所有 3 张工作表

row1 = sheet[0].getRow(1) +  sheet[1].getRow(1) + sheet[2].getRow(1)

以下是我到目前为止所做的,但正如您所看到的,该行是每张纸定义的,这是错误的,我也需要它来从其他纸上获取数据。这怎么能优雅地完成?

LinkedHashMap<String, List<LinkedHashMap<String, Object>>> records = 
             new LinkedHashMap<String, List<LinkedHashMap<String, Object>>>();
for (int sIndex = 0; sIndex < wb.getNumberOfSheets(); sIndex++) {
                // System.out.println(sIndex);
                Sheet sheet = wb.getSheetAt(sIndex);
                String key;
                String value = null;
                List<LinkedHashMap<String, Object>> sheetRecords = new ArrayList<LinkedHashMap<String, Object>>();
                for (Row row : sheet) {

                    LinkedHashMap<String, Object> record = new LinkedHashMap<>();
                    for (Cell cell : row) {

                        switch (cell.getCellType()) {
                        case Cell.CELL_TYPE_STRING:
                            key = sheet.getRow(sheet.getFirstRowNum())
                                    .getCell(cell.getColumnIndex()).toString();
                            value = cell.getStringCellValue();
                            record.put(key, value);
                            sheetRecords.add(record);
                            break;
                        case Cell.CELL_TYPE_NUMERIC:
                            key = sheet.getRow(sheet.getFirstRowNum())
                                    .getCell(cell.getColumnIndex()).toString();
                            double number = cell.getNumericCellValue();

                            record.put(key, number);
                            sheetRecords.add(record);
                        case Cell.CELL_TYPE_BLANK:
                            break;
                        default:
                            break;
                        }

                    }

                }

                records.put(sheet.getSheetName(), sheetRecords);

            }
4

1 回答 1

1

您必须使用外部 for 循环按索引迭代行。在伪代码中:

maxRows := firstSheetInWorkbook->getNumberOfRows();
for (row := 0..maxRows)
   rowResult := new RowResult
   for (sheet := 0..maxSheets)
      rowResult->add(processRowForSheet(row, sheet))
于 2012-12-19T14:12:15.690 回答