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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10615628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 01:49:51  来源:igfitidea点击:

Apache POI get cell color when reading xlsx file

javaapache-poi

提问by Lalit Chattar

Hello all i am reading one xlsxfile using XSSFof Apche POI. Now i want to read color of the cell and apply same color on new xlsxfile. how will i do it. my code is:

您好所有我读一个xlsx使用文件XSSFApche 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"));
    }

I an using Apche POI 3.8.

我正在使用 Apche POI 3.8。

回答by TikkaBhuna

I posted a comment to vikiiii's answer. I thought I'd expand on it a bit more. His answer is specific to HSSF (.xls) but both the HSSF and XSSF classes descend from the same interface so the code is the same, you just use XSSF instead of HSSF. Seeing as you want to reuse the color I'd recommend using:

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

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

See herefor the Javadoc. Now to set a new cell to that color you can use this.

有关Javadoc,请参见此处。现在要将新单元格设置为该颜色,您可以使用

secondCell.getCellStyle().setFillBackgroundColor(bgColor);

I'd recommend looking at the interfaces that the XSSF and HSSF classes descend from and have a look at making your code be able to handle both xls and xlsx files. As far as I'm aware the only difference is the way you set up the workbook, using WorkbookFactory.

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

回答by vikiiii

You can use this code to get the cell color.

您可以使用此代码获取单元格颜色。

cell.getCellStyle().getFillBackgroundColor();

Try

尝试

HSSFColor.getIndexHash().get(myCell.getCellStyle().getFillBackgroundColor())