我正在开发一个项目,我必须从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;
}
}
答案 0 :(得分:1)
在Java中,您可以将HashMap或TreeMap数据结构与ArrayList结合使用,并按如下方式对需求进行建模。
Map<String, Map<String, List<String>>>map = new HashMap<String, Map<String, List<String>>>();
然后,当您阅读文件夹的名称时,您可以将它们添加到我建议的数据结构中。
hashmap是您的文件夹。
hashmap映射的第一级键将包含“Subfolder01,Subfolder02,Subfolder03,Subfolder04”。相应的值将是已提到的文件夹中包含的文件夹。
这些值中的每一个都是Map<String, List<String>>mi
,而<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<li itemscope="itemscope" itemtype="https://www.schema.org/SiteNavigationElement" id="menu-item-29" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-29 dropdown"><a title="Therapieën" href="#" data-toggle="dropdown" class="dropdown-toggle" aria-haspopup="true">Therapieën <span class="caret"></span></a>
<ul role="menu" class=" dropdown-menu">
<li itemscope="itemscope" itemtype="https://www.schema.org/SiteNavigationElement" id="menu-item-86" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-86"><a title="Logopedie" href="http://localhost:81/wordpress/logopedie/">Logopedie</a></li>
<li itemscope="itemscope" itemtype="https://www.schema.org/SiteNavigationElement" id="menu-item-85" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-85"><a title="Bijlessen" href="http://localhost:81/wordpress/bijlessen/">Bijlessen</a></li>
<li itemscope="itemscope" itemtype="https://www.schema.org/SiteNavigationElement" id="menu-item-84" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-84"><a title="Audiologie" href="http://localhost:81/wordpress/audiologie/">Audiologie</a></li>
<li itemscope="itemscope" itemtype="https://www.schema.org/SiteNavigationElement" id="menu-item-83" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-83"><a title="Stemtherapie" href="http://localhost:81/wordpress/stemtherapie/">Stemtherapie</a></li>
<li itemscope="itemscope" itemtype="https://www.schema.org/SiteNavigationElement" id="menu-item-82" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-82"><a title="Verder ook begeleiding in" href="http://localhost:81/wordpress/verder-ook-begeleiding-in/">Verder ook begeleiding in</a></li>
</ul>
</li>
又包含子文件夹“Subfolder02.01 ...”的名称,依此类推每个级别。
当您到达最后一级并找到最后一个文件夹中包含的文件时,您可以在ArrayList中插入这些名称。根据您所拥有的级别,您可以根据需要扩展或篡改数据结构的级别。
答案 1 :(得分: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;
}
}