Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

导入时获取单元格样式 #3981

Open
2368390177 opened this issue Sep 9, 2024 · 1 comment
Open

导入时获取单元格样式 #3981

2368390177 opened this issue Sep 9, 2024 · 1 comment
Assignees
Labels
help wanted Extra attention is needed

Comments

@2368390177
Copy link

Excel导入的时候怎么获取sheet内单元格的样式,比如水平居中、垂直居中这种

@2368390177 2368390177 added the help wanted Extra attention is needed label Sep 9, 2024
@jieyangxchen
Copy link
Collaborator

jieyangxchen commented Sep 11, 2024

目前读的时候没法获取单元格样式,
可以配合POI进行读取,但不保证性能,
参考Demo如下:

        ...
        FileInputStream fis = new FileInputStream(fileName);
        Workbook workbook = new XSSFWorkbook(fis);
        Sheet sheet = workbook.getSheetAt(0);

        EasyExcel.read(fileName, DemoData.class, new ReadListener<DemoData>() {
            @Override
            public void invoke(DemoData data, AnalysisContext context) {
                // 获取行号
                int rowIndex = context.readRowHolder().getRowIndex();
                System.out.println("读取到数据: " + data);

                // 通过 POI 获取对应行和列的样式
                XSSFRow row = (XSSFRow) sheet.getRow(rowIndex);
                if (row != null) {
                    int cellNumCount = row.getPhysicalNumberOfCells();
                    int cellIndex = 0;
                    XSSFCell cell = row.getCell(cellIndex);
                    if (cell != null) {
                        CellStyle cellStyle = cell.getCellStyle();
                        HorizontalAlignment horizontalAlignment = cellStyle.getAlignment();
                        VerticalAlignment verticalAlignment = cellStyle.getVerticalAlignment();

                        // 输出样式信息
                        System.out.println("共有 " + cellNumCount + " 列");
                        System.out.println("第 " + (rowIndex + 1) + " 行第" + (cellIndex + 1) + "列的水平对齐方式: " + horizontalAlignment);
                        System.out.println("第 " + (rowIndex + 1) + " 行第" + (cellIndex + 1) + "列的垂直对齐方式: " + verticalAlignment);
                    }
                }
            }

            /**
             * if have something to do after all analysis
             *
             * @param context
             */
            @Override
            public void doAfterAllAnalysed(AnalysisContext context) {

            }
        }).sheet().doRead();

        workbook.close();
        fis.close();
        ...

@jieyangxchen jieyangxchen self-assigned this Sep 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants