1

我正在處理一個項目,我必須從excel文件中獲取文件夾和子文件夾名稱,保留層次結構並將它們存儲在數組中,以使用該數組創建目錄結構。xlsx文件中的DataStructure

我使用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; 

在這裏,我outputing與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; 
    } 

} 
+0

什麼是錯的樹和水平打印呢? –

+0

上面的樹是作爲excel結構的例子給出的。 –

回答

1

下面的代碼片段,以幫助您開始

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

} 
+0

感謝您的幫助。我開發了讀取excel文件的類,現在我只是想弄清楚如何將整個結構封裝到數組中。你的ideea適合於不依賴於其他列的excel文件,但對我來說,維護每個組件的繼承非常重要,它究竟如何在示例結構中。 - 與父母和孩子 - 等等 –

1

在Java中,您可以將HashMap或TreeMap數據結構與ArrayList結合使用,並按如下方式建模您的需求。

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

然後,當你讀取文件夾的名稱,你可以將它們添加到我建議的數據結構中。 散列圖是你的文件夾。 散列映射圖的第一級鍵將包含「Subfolder01,Subfolder02,Subfolder03,Subfolder04」。相應的值將是包含在已經提到的文件夾中的文件夾。 這些值中的每一個都將是一個Map<String, List<String>>mi,其中將包含每個級別的子文件夾「Subfolder02.01 ...」的名稱等等。 當您到達最後一級並找到最後一個文件夾中包含的文件時,您可以在ArrayList中插入這些名稱。根據您擁有的級別,您可以根據需要擴展或縮小您的數據結構級別。

+0

這使得子子文件夾尊重他們的父文件夾? - Subfolder04.01.01和Subfolder04.01.02與Subfolder02.01.01和Subfolder02.01.02位於同一個Collumn,但它們屬於不同的父母。 –

+0

這個結構是一棵樹。沒有列。文件夾的名稱將按照根文件夾的結構插入到數據結構中。 Java代碼當然必須實現。我的答案旨在提供一種方法來指導OP實現解決方案。 – AlketCecaj

+0

在Excel中的列 - 這就是我如何提取它們,行和單元格 感謝您的幫助 - 現在我正在HashMap結構工作,但我仍然遇到困難,以正確的結構順序。 –