问题描述
使用jxls-reader(http://jxls.sourceforge.net/reference/reader.html)需要一些帮助。
我很乐意循环工作表,其中一行代表一个特定的Java对象,但是在嵌套循环的情况下,我很困惑。
我需要将上面的excel工作表映射到以下pojo:
class Student {
private String name;
private String dob;
private List<Class> class;
//getters & setters
}
class Class {
private String name;
private String link;
//getters & setters
}
我的问题是定义lookbreakcondition以读取内部对象。请注意,Class对象的loopbreakcondition需要以下几行内容:
- 下一行的第一个单元格为非空或
- 下一行的第一个单元格为空,当前列的下一个单元格为空(用于中断最后一行)
如何在loopbreakcondition中表达上述布尔条件。我有以下内容,但我知道这是错误的:
<workbook>
<worksheet name="Students">
<section startRow="0" endRow="0">
<!--Empty Header Section-->
</section>
<loop startRow="1" endRow="1" items="students" var="student" varType="com.test.Student">
<section startRow="1" endRow="1">
<mapping row="1" col="0">student.name</mapping>
<mapping row="1" col="1">student.dob</mapping>
</section>
<loopbreakcondition>
<rowcheck offset="0">
<cellcheck offset="0"> <!--Breaks on next empty cell--></cellcheck>
</rowcheck>
</loopbreakcondition>
<loop startRow="1" endRow="1" items="student.class" var="class" varType="com.test.Class">
<section startRow="1" endRow="1">
<mapping row="1" col="2">class.name</mapping>
<mapping row="1" col="3">class.link</mapping>
</section>
<loopbreakcondition>
<rowcheck offset="0">
<cellcheck offset="0"> </cellcheck>
</rowcheck>
</loopbreakcondition>
</loop>
</loop>
</worksheet>
</workbook>
这甚至可能吗? 还有其他库可以帮助我实现这一目标吗? 还是我最好使用apache-poi编写自己的解析器?
谢谢。
解决方法
您可以使用apache-poi读取excel(xls或xlsx(XSSF))文件。有关更多信息,请访问apache-poi documentation,Apache POI-HSSF vs POI-XSSF。在以下示例中,将Class的类名称更改为StudentClass。因为class是Java中的关键字
尝试以下解决方案,
Studnet.java
public class Student {
private String name;
private Date dob; //as per given excel file,dob is a date format
private ArrayList<StudentClass> studentClassList;
//getters & setters
}
StudentClass.java
public class StudentClass {
private String name;
private String link;
//getters & setters
}
ExcelFileReader.java
public class ExcelFileReader() {
private String studentName; //for store the previous student name
private List<Student> listStudent; //for collect the students
public void readData() {
listStudent = new ArrayList<Student>();
studentName = "";
FileInputStream excelFile = new FileInputStream(new File(INPUT_EXCEL_FILE_NAME));
Workbook workbook = new XSSFWorkbook(excelFile); //create the workbook using input excel file
Sheet sheet = workbook.getSheetAt(0); //get the first sheet of excel file
int rowCount = sheet.getPhysicalNumberOfRows(); //get the row count
for(int i = 1; i < rowCount; i++) { //i initialized with 1 for exclude the first row(first row id is 0 and it hold the column names)
if(rowCount>1){ //ignore the first row. because at this time no any student created
listStudent.add(student); //add student into student list
//you can use the created student in here as per your requirement
}
Row currentRow = sheet.getRow(i); //get current row
if(!studentName.equals(currentRow.getCell(0).getStringCellValue())){ //if not equals current student name with previous student name then create new student entry
Student student = new Student();
student.setName(currentRow.getCell(0).getStringCellValue()); //first cell id is 0
studnet.setDob(currentRow.getCell(1).getDateCellValue()); //second cell id is 1
studentName = student.getName(); //assign the current student name to studentName variable for future validation
if(!currentRow.getCell(2).getStringCellValue().equals(null) && !currentRow.getCell(2).getStringCellValue().equals("")){ //check the student has a class at same row(first row with new student entry)
StudentClass studentClass = new StudentClass();
studentClass.setName(currentRow.getCell(2).getStringCellValue()); //third cell id is 2
studentClass.setLink(currentRow.getCell(3).getStringCellValue());
student.getStudentClassList.add(studentClass); //add relevant StudnetClass into StudentClassList of Student. please initialized the StudentClassList in StudentClass
}
}else{ //this code block execute when student has more than one class
StudentClass studentClass = new StudentClass();
studentClass.setName(currentRow.getCell(2).getStringCellValue());
studentClass.setLink(currentRow.getCell(3).getStringCellValue());
student.getStudentClassList.add(studentClass);
}
}
}
}