【问题标题】:DataStructure from xlsx file来自 xlsx 文件的数据结构
【发布时间】:2017-05-09 07:49:32
【问题描述】:

我正在做一个项目,我必须从 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;
    }

}

【问题讨论】:

  • 树和按层打印有什么问题?
  • 上面的树是作为excel结构的一个例子。

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


【解决方案1】:

这里有一个代码sn-p来帮助你开始

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

}

【讨论】:

  • 感谢您的帮助。我开发了读取excel文件的类,现在我只是想弄清楚如何将整个结构封装到一个数组中。您的想法适用于不依赖于其他列的 excel 文件,但在我看来,保持每个组件的继承在示例结构中的准确方式非常重要。 -- 与父母和孩子 - 等等
【解决方案2】:

在 Java 中,您可以将 HashMap 或 TreeMap 数据结构与 ArrayList 结合使用,并对您的需求建模,如下所示。

Map<String, Map<String, List<String>>>map = new HashMap<String, Map<String, List<String>>>();

然后,当您阅读文件夹的名称时,您可以将它们添加到我建议的数据结构中。 哈希图是您的文件夹。 hashmap 映射的第一级键将包含“ Subfolder01 , Subfolder02 , Subfolder03 , Subfolder04 ”。相应的值将是已经提到的文件夹中包含的文件夹。 这些值中的每一个都是Map&lt;String, List&lt;String&gt;&gt;mi,而Map&lt;String, List&lt;String&gt;&gt;mi 又将包含每个级别的子文件夹“Subfolder02.01 ...”的名称,依此类推。 当您到达最后一层并找到包含在最后一个文件夹中的文件时,您可以将这些名称插入到 ArrayList 中。您可以根据自己的级别尽可能多地扩展或分解数据结构的级别。

【讨论】:

  • 这会让子子文件夹尊重他们的父文件夹? - Subfolder04.01.01 和 Subfolder04.01.02 与 Subfolder02.01.01 和 Subfolder02.01.02 位于同一列,但它们属于不同的父级。
  • 这个结构是一棵树。没有列。文件夹的名称将按照根文件夹的结构插入到数据结构中。 Java 代码当然必须实现。我的回答旨在提供一种方法来引导 OP 找到解决方案。
  • Excel 中的列 - 这就是我按行和单元格提取它们的方式 感谢您提供帮助 - 现在我正在使用 HashMap 结构,但在正确排序结构时仍然遇到困难.
猜你喜欢
  • 2011-03-26
  • 1970-01-01
  • 2021-02-17
  • 2017-03-31
  • 2022-11-24
  • 2016-06-09
  • 1970-01-01
  • 2018-12-09
  • 2021-04-19
相关资源
最近更新 更多