java - xlsx 文件的数据结构

标签 java excel multidimensional-array data-structures apache-poi

我正在开发一个项目,我必须从 Excel 文件中获取文件夹和子文件夹名称,保留层次结构并将它们存储在数组中,以使用该数组创建目录结构。

我正在使用 Apache POI 读取 Excel 文件。

考虑到我有 8 个树级别,如何将它们存储到数组中?

例如:

Folder                                  
    Subfolder01                             
    Subfolder02                             
        Subfolder02.01                          
            Subfolder02.01.01                                       
                Subfolder02.01.01.01                                
            Subfolder02.01.02                                       
    Subfolder03                             
    Subfolder04                                     
        Subfolder04.01                              
            Subfolder04.01.01                                       
                Subfolder04.01.01.01                                
            Subfolder04.01.02                       
                Subfolder04.01.02.01                            
        Subfolder04.02                              

以下是我如何使用 Apache POI 库读取 excel 文件:

public class ExcelReadClass {

    private static final String FILE_NAME = "D:/Work-Space/TESTExcel.xlsx";

    public void readExcel(String fileName) {

        try {

            // XSSFWorkbook  = XML SpreadSheet Format
            // is for Excel 2007 or above  


            Workbook workbook = new XSSFWorkbook(fileName); 

            // Accessing the particular sheet
            // here the parameter indicates the sheet number. 0 means first sheet, 1 means the second and so on
            /// accessing first sheet - which is " Components " 
            Sheet sheet = workbook.getSheetAt(0);

            /*
             * Sheet can also be accessed using the sheet name like shown below
             * Sheet sheet = workbook.getSheet("Components");
             */

            // geting the rows

            // following code will work with empty cells
            Row row = null;
            Cell cell = null;

            // Returns: the number of physically defined rows in the selected sheet
            //int noOfRows = sheet.getPhysicalNumberOfRows();

            //Returns: last row contained n this sheet (0-based)
            int noOfRows = sheet.getLastRowNum();

            // starting from  0 - which is the first row
            for(int i = 2; i <= noOfRows; i++) {

                row = sheet.getRow(i);

                //int noOfCells = row.getPhysicalNumberOfCells(); // returns the total number of cells in the selected row
                //int noOfCells = row.getLastCellNum(); // returns the number of the last cell in the row
                int noOfCells = 11;

这里我用Sysout输出文件结构 我必须将整个结构存储到数组中的地方

                // starting from 0 - which is the first column ( aka cell )
                for(int j = 1; j < noOfCells; j++) {

                    cell = row.getCell(j) ; // if there's no more cells, it returns null


                    if(cell != null ) {
                        System.out.print(getCellValue(cell) + "\t");
                    } else {
                        Cell blanckCell = row.createCell(j);
                        blanckCell.setCellValue("");
                        System.out.print(getCellValue(blanckCell) + "\t");
                    }
                }
                System.out.println();
            }

            workbook.close();

        } catch (FileNotFoundException e) {

            System.out.println("File is not available.");
            e.printStackTrace();

        } catch (IOException e) {

            System.out.println("Problem reading file from directory.");
            e.printStackTrace();

        } catch (NullPointerException e){

            System.err.println("Last part of Excel");
            e.printStackTrace();

        }


    }

    public Object getCellValue(Cell cell){
        Object cellValue = null;
        if(cell.getCellTypeEnum() == CellType.STRING){
            cellValue = cell.getStringCellValue();
        }else if(cell.getCellTypeEnum() == CellType.NUMERIC){
            cellValue = cell.getNumericCellValue();
        }else if(cell.getCellTypeEnum() == CellType.BOOLEAN){
            cellValue = cell.getBooleanCellValue();
        }else if(cell.getCellTypeEnum() == CellType.BLANK){
            cellValue = "";
        }
        return cellValue;
    }

}

最佳答案

这是一个可以帮助您开始的代码片段

try {

            final String FILENAME = "c:\\Rest\\Test\\data.txt"; //change to your file location
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    new FileInputStream(FILENAME), "UTF-8")); //change it to your reader
            String line;
            //read file line by line
            while ((line = br.readLine()) != null) {
                System.out.println(line);
                Node root = null;
                if (line == "Folder") {
                    root = new Node(null);
                } else {
                    String indexs = line.substring(9, line.length());
                    if (indexs.length() == 2) {
                        // insert to root
                    } else if (indexs.length() == 4) {
                        // create node and use readLine to all sub nodes
                    }
                }

            }
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

以及 Node 类:

import java.util.ArrayList;
import java.util.List;

public class Node {
    private String id;
    private final List<Node> children = new ArrayList<>();
    private final Node parent;

    public Node(Node parent) {
        this.parent = parent;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public List<Node> getChildren() {
        return children;
    }

    public Node getParent() {
        return parent;
    }

}

关于java - xlsx 文件的数据结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43864119/

相关文章:

java - 一个文件java中的两个公共(public)类

java - SWT中如何控制工具栏Item的位置?

java - 我想使用java打印excel中的异常计数总数

excel - 在 VBA 中重新调整多维数组的两个维度(为什么这里的解决方案不起作用)

php - 使用键值对和类似 '%LIKE%' 构造的 sql 搜索 php 多维关联数组

PHP表单使用多维数组提交到数据库

java - 运行应用程序时出错 : No JDK specified

java - 将 AWS SageMaker 机器学习模型转换为 Java 库

excel - 如何从 Excel 中的整数列中过滤字符串以在 Python 中处理

Excel - 数字通配符有助于对值进行分组?