Apache POI 在读取 xlsx 文件时获取单元格颜色

2023-11-29

大家好,我正在读一本xlsx文件使用XSSF of Apche POI。现在我想读取单元格的颜色并在新单元格上应用相同的颜色xlsx文件。我该怎么做呢。我的代码是:

public void readXLSXFile(String filePath) throws FileNotFoundException, IOException
    {
        XSSFRow row;
        XSSFRow new_row;
        XSSFSheet sheet;
        XSSFCell cell;
        XSSFCell new_cell;
        XSSFCellStyle cellStyle;
        XSSFDataFormat dataFormat;
        XSSFColor color;

        XSSFWorkbook xssfWorkbook = new XSSFWorkbook(new FileInputStream(filePath));
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet new_sheet = (XSSFSheet) workbook.createSheet();
        for(int i = 0; i < xssfWorkbook.getNumberOfSheets(); i++ )
        {
            sheet = xssfWorkbook.getSheetAt(i);
            for(int j =0; j<sheet.getLastRowNum(); j++)
            {
                row = (XSSFRow) sheet.getRow(j);
                new_row = new_sheet.createRow(j);
                for(int k = 0; k<row.getLastCellNum(); k++)
                {
                    cell = row.getCell(k);
                    new_cell = new_row.createCell(k);
                    cellStyle = workbook.createCellStyle();
                    dataFormat = workbook.createDataFormat();
                    cellStyle.setDataFormat(dataFormat.getFormat(cell.getCellStyle().getDataFormatString()));
                    color = cell.getCellStyle().getFillBackgroundColorColor();
                    cellStyle.setFillForegroundColor(color);
                    new_cell.setCellStyle(cellStyle);
                    System.out.println(cell.getCellStyle().getFillForegroundColor()+"#");
                    switch (cell.getCellType()) {
                    case 0:
                        new_cell.setCellValue(cell.getNumericCellValue());
                        break;
                    case 1:
                        new_cell.setCellValue(cell.getStringCellValue());
                        break;
                    case 2:
                        new_cell.setCellValue(cell.getNumericCellValue());
                        break;
                    case 3:
                        new_cell.setCellValue(cell.getStringCellValue());
                        break;
                    case 4:
                        new_cell.setCellValue(cell.getBooleanCellValue());
                        break;
                    case 5:
                        new_cell.setCellValue(cell.getErrorCellString());
                        break;
                    default:
                        new_cell.setCellValue(cell.getStringCellValue());
                        break;
                    }
                }
            }
        }
        workbook.write(new FileOutputStream("G:\\lalit.xlsx"));
    }

我正在使用 Apache POI 3.8。


我对 vikiiii 的回答发表了评论。我想我应该进一步扩展它。他的答案特定于 HSSF (.xls),但 HSSF 和 XSSF 类都源自同一接口,因此代码是相同的,您只需使用 XSSF 而不是 HSSF。鉴于您想重复使用我建议使用的颜色:

XSSFColor bgColor = xssfCell.getCellStyle().getFillBackgroundColorColor();

See here对于 Javadoc。现在要将新单元格设置为该颜色,您可以使用this.

secondCell.getCellStyle().setFillBackgroundColor(bgColor);

我建议查看 XSSF 和 HSSF 类派生的接口,并看看如何使您的代码能够处理 xls 和 xlsx 文件。据我所知,唯一的区别是您设置工作簿的方式,使用工作簿工厂.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Apache POI 在读取 xlsx 文件时获取单元格颜色 的相关文章

随机推荐