[英]Complex conditionals in JXLS-Reader loopbreakcondition
我需要一些使用 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 文檔、 Apache POI-HSSF 與 POI-XSSF 。 在以下示例中將 Class 的類名更改為 StudentClass。 因為 class 是 java 中的關鍵字
嘗試使用以下解決方案,
學生網.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
}
學生類
public class StudentClass {
private String name;
private String link;
//getters & setters
}
Excel文件閱讀器
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);
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.