java apache poi(第 1 部分) [英] java apache poi (part 1)
问题描述
excel文件(staff.xls)
身份证 姓名
1 阿里
2 阿布
3 艾哈迈德
java 代码
FileInputStream inputFile = new FileInputStream("staff.xls");XSSFWorkbook 工作簿 = new XSSFWorkbook(inputFile);XSSFSheet 电子表格 = workbook.getSheetAt(0);XSSFRow 行;细胞细胞;迭代器<行>rowIterator = 电子表格.迭代器();while(rowIterator.hasNext()){row = (XSSFRow)rowIterator.next();迭代器<Cell>cellIterator = row.cellIterator();while(cellIterator.hasNext()){cell = cellIterator.next();cell.setCellType(Cell.CELL_TYPE_STRING);开关(cell.getCellType()){案例 Cell.CELL_TYPE_STRING:System.out.print(cell.getStringCellValue()+"|");休息;案例 Cell.CELL_TYPE_NUMERIC:System.out.print(cell.getNumericCellValue()+"|");休息;}}System.out.println();}
我的问题是:
(1) 如何将记录放入数组或数组列表中?
(2) 创建后,如何拆分|"?
只需在第一个while之前创建一个List of List变量,在每次迭代开始时创建一个新的List,放入元素到此列表并在迭代结束时将此列表添加到主列表列表中.你应该得到这样的东西:
<预><代码>...列表<列表<字符串>>record = new ArrayList- >();while(rowIterator.hasNext()){列表<字符串>record = new ArrayList
excel file (staff.xls)
ID name
1 ali
2 abu
3 ahmad
java code
FileInputStream inputFile = new FileInputStream("staff.xls"); XSSFWorkbook workbook = new XSSFWorkbook(inputFile); XSSFSheet spreadsheet = workbook.getSheetAt(0); XSSFRow row; Cell cell; Iterator<Row> rowIterator = spreadsheet.iterator(); while(rowIterator.hasNext()){ row = (XSSFRow)rowIterator.next(); Iterator<Cell> cellIterator = row.cellIterator(); while(cellIterator.hasNext()){ cell = cellIterator.next(); cell.setCellType(Cell.CELL_TYPE_STRING); switch(cell.getCellType()){ case Cell.CELL_TYPE_STRING: System.out.print(cell.getStringCellValue()+"|"); break; case Cell.CELL_TYPE_NUMERIC: System.out.print(cell.getNumericCellValue()+"|"); break; } } System.out.println(); }
My question is:
(1) How to put the record into the array or arraylist?
(2) After create, how to split the "|"?
另请注意,您不需要添加 |不再是符号,因此最终无需拆分.但通常要拆分字符串,有一个方法 String#split() 接受正则表达式.你需要像这样使用它来分割|"(您需要将 \ 放在 | 之前,因为它是一个特殊的正则表达式字符):
for(字符串记录:记录){System.out.println(记录);String[] 元素 = record.split("\\|");for(字符串元素:元素){System.out.println(" -> " + element);}}
Just create a List of List variable before the first while, in the beginning of every iteration create a new List, put the elements to this list and add this list to the main List of Lists at the end of the iteration. You should get something like that:
...
List<List<String>> records = new ArrayList<List<String>>();
while(rowIterator.hasNext()){
List<String> record = new ArrayList<String>();
row = (XSSFRow)rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
while(cellIterator.hasNext()){
cell = cellIterator.next();
cell.setCellType(Cell.CELL_TYPE_STRING);
switch(cell.getCellType()){
case Cell.CELL_TYPE_STRING:
record.add(cell.getStringCellValue());
break;
case Cell.CELL_TYPE_NUMERIC:
record.add(Double.toString(cell.getNumericCellValue()));
break;
}
}
records.add(record);
}
for (List<String> record : records) {
for (String s : record) {
System.out.print(" " + s);
}
System.out.println();
}
...
Also notice that you don't need to add | symbol anymore, so no need to split eventually. But in general to split the string there is a method String#split() which accepts a regular expression. You need to use it like that to split by "|" (you need to put \ before | as it is a special regexp character):
for (String record : records) {
System.out.println(record);
String[] elements = record.split("\\|");
for (String element : elements) {
System.out.println(" -> " + element);
}
}
这篇关于java apache poi(第 1 部分)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!