【问题标题】:Read merge cell in excel using Apache POI with Spring使用带有 Spring 的 Apache POI 在 Excel 中读取合并单元格
【发布时间】:2020-07-04 15:18:16
【问题描述】:

我正在构建一个读取 excel 文件中可用数据的函数。

我在 Spring 项目中使用 Apache POI。

但是,我在阅读合并单元格时遇到了问题。我需要的是使内容以红色突出显示,如图所示。

你能给我推荐一下代码吗?

谢谢!

更新:

`

public void importClassroomByExcel(MultipartFile file) {
        try {
            Workbook workbook = new HSSFWorkbook(file.getInputStream());
            Iterator<Sheet> sheetIterator = workbook.iterator();
            while (sheetIterator.hasNext()) {
                Sheet sheet = sheetIterator.next();
                String title = sheet.getRow(4).getCell(5).toString();

            }
            workbook.close();
        } catch (Exception e) {
            throw new BadRequestAlertException(ErrorConstants.DEFAULT_TYPE, "", e.getMessage(), "filestructerror",
                    Status.BAD_REQUEST);
        }
    }

`

【问题讨论】:

  • 你试过什么?你到底卡在哪里了?合并单元格的单元格值存储在合并区域的左上角单元格中。所以第一个红色文本在F5sheet.getRow(4).getCell(5)。第一个红色60在G5:sheet.getRow(4).getCell(6)
  • sheet.getRow(4).getCell(5)我得到了第一个信息。但是,我无法判断合并区域何时结束以继续阅读下一个信息?你了解我吗 ?我还想在 N 列和 O 列中获取信息,我用红色突出显示。
  • 合并区域中除左上角单元格外的所有单元格均为空。因此,合并区域在找到下一个单元格值的行或该行是工作表的最后一个行号时结束。并且列N 的列索引为13,列O 的列索引为14:所以N5sheet.getRow(4).getCell(13)
  • 你能推荐一个来源吗?我刚刚更新了包含我的源代码的问题。

标签: java excel spring apache-poi


【解决方案1】:

不确定我是否理解您的要求是否正确。这就是我认为您想要的:在您的工作表中,数据范围从第 5 行开始,A 列确定分组。现在需要获取每个组的 F 和 G 列的值。然后需要获取 N 列中每个组的第一个日期和 O 列中每个组的最后一个日期。

因此需要获取每个组的第一行和每个组的最后填充行。以下示例显示了这一点。主要是它使用int getNextFilledRow(Sheet sheet, int columnNum, int startRowNum) 方法从给定行号开始获取工作表给定列中的下一个填充行。

代码应该只获取给定工作表中带有红色标记内容的单元格。

import java.io.FileInputStream;
import org.apache.poi.ss.usermodel.*;

class ReadExcelMergedCells {

 static int getNextFilledRow(Sheet sheet, int columnNum, int startRowNum) {
  int lastRow = sheet.getLastRowNum();
  int nextFilledRow = lastRow;
  for (int r = startRowNum; r <= lastRow; r++) {
   nextFilledRow = r + 1; 
   Row row = sheet.getRow(nextFilledRow);
   Cell cell = (row != null)?cell = row.getCell(columnNum):null;
   if (cell != null && cell.getCellType() != CellType.BLANK) {
    break;
   }
  }
  return nextFilledRow;
 }

 public static void main(String[] args) throws Exception {
  Workbook workbook = WorkbookFactory.create(new FileInputStream("./Excel.xlsx"));
  Sheet sheet = workbook.getSheetAt(0);
  int lastRow = sheet.getLastRowNum();
  int startRow = 4;
  Row row = null;
  Cell cell = null;
  int columnNum = -1;
  int columNumA = 0; //column A determines the grouping
  for (int r = startRow; r <= lastRow; r = getNextFilledRow(sheet, columNumA, r)) {
   //r is the first row in this group
System.out.println("Group starting in row " + r);

   //get cell in column F of first row in this group
   columnNum = 5;
   row = sheet.getRow(r);
   cell = (row != null)?cell = row.getCell(columnNum):null;
System.out.println(cell);

   //get cell in column G of first row in this group
   columnNum = 6;
   row = sheet.getRow(r);
   cell = (row != null)?cell = row.getCell(columnNum):null;
System.out.println(cell);

   //get cell in column N of first row in this group
   columnNum = 13;
   row = sheet.getRow(r);
   cell = (row != null)?cell = row.getCell(columnNum):null;
System.out.println(cell);

   //get cell in column O of last filled row in this group
   columnNum = 14;
   int lastRowInGroup = getNextFilledRow(sheet, columNumA, r) -1;
   // get last filled cell in this group
   for (int rl = lastRowInGroup; rl >= r; rl--) {
    row = sheet.getRow(rl);
    cell = (row != null)?cell = row.getCell(columnNum):null;
    if (cell != null && cell.getCellType() != CellType.BLANK) {
     break;
    }
   }
System.out.println(cell);

System.out.println("Group ending in row " + lastRowInGroup);

  }

  workbook.close();
 }
}

【讨论】:

    【解决方案2】:

    将单元格颜色更改为红色的代码:

        CellStyle cellStyle = cell.getCellStyle();
        Font font = workbook.createFont();
    
        font.setFontName("Times New Roman");
        font.setColor(IndexedColors.RED.index);
    
        cellStyle.setFont(font);
        cellStyle.setFillBackgroundColor(IndexedColors.BLUE.index);
    
        cell.setCellStyle(cellStyle);
    

    从 B2 合并到 E2:

    sheet.addMergedRegion(new CellRangeAddress(1,1,1,4));
    

    查看更多:

    【讨论】:

    • 对不起,我想获取内容,以红色突出显示内容。不得设置样式。
    猜你喜欢
    • 1970-01-01
    • 2015-06-22
    • 2023-03-12
    • 2011-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多