本文介绍了阅读XLSX文件时apche的POI获取单元格颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好所有我读使用一个 XLSX 文件 XSSF 的apche的POI 。现在,我想读的单元格颜色和新的 XLSX 文件应用相同的颜色。我将如何做到这一点。我的code是:

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

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"));
    }

我的使用apche的POI 3.8。

I an using Apche POI 3.8.

推荐答案

我发布的注释vikiiii的答案。我想我会在其上展开多一点。他的回答是特定于HSSF(.xls的),但无论是HSSF和XSSF类都是从同一个界面下,以使code是一样的,你只需要使用XSSF代替HSSF。眼看你想重用我推荐使用的颜色:

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:

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

请参阅的Javadoc。我们设置为颜色,您可以使用这个。

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

secondCell.getCellStyle().setFillBackgroundColor(bgColor);

我建议在看的XSSF和HSSF类从下降并看看让你的code能够同时处理XLS和XLSX文件的接口。据我所知,唯一的区别是你设置的工作簿,使用方式 WorkbookFactory 。

这篇关于阅读XLSX文件时apche的POI获取单元格颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 15:44
查看更多