gpt4 book ai didi

java - xlsx 文件的数据结构

转载 作者:行者123 更新时间:2023-11-30 02:32:42 25 4
gpt4 key购买 nike

我正在开发一个项目,我必须从 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com