繁体   English   中英

JXLS-Reader loopbreakcondition 中的复杂条件

[英]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 需要以下内容:

  • 下一行的第一个单元格为非空 OR
  • 下一行的第一个单元格为空,当前列的下一个单元格为空(用于打破最后一行)

如何在 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM