Java easyreport Import Excel, TXT data composite properties (ii)

Source: Internet
Author: User
Tags string format

When you import a report, you often encounter object properties that are collections or arrays, such as when a student has more than one teacher, which is how a collection or array of reports is set up and imported.

Teacher Class

Package model;


public class Teacher {

private String name;

Private String sex;

Public String GetName () {

return name;

}

public void SetName (String name) {

THIS.name = name;

}

Public String Getsex () {

return sex;

}

public void Setsex (String sex) {

This.sex = sex;

}

@Override

Public String toString () {

return name+ "|" +sex;

}

}

Student class

Package model;


Import java.util.List;


public class Student {

private String name;

private int age;

Private Course Course;

Teacher Collection

Private list<teacher> teachers;

Private teacher[] teachers;

Public String GetName () {

return name;

}

public void SetName (String name) {

THIS.name = name;

}

public int getage () {

return age;

}

public void Setage (int.) {

This.age = age;

}

Public Course GetCourse () {

return course;

}

public void Setcourse (Course Course) {

This.course = course;

}

Public list<teacher> getteachers () {

return teachers;

}

public void Setteachers (list<teacher> teachers) {

This.teachers = teachers;

}

}

Import data

name age account teacher name teacher sex
Bachelor 12 Chinese 80 Zhang Sanfeng Man

Excel Import Demo Testexcelcompro

package example;


Import Java.io.File;

Import Java.io.FileInputStream;

Import java.util.List;

Import Java.util.Map;


Import model. Student;

Import Test. Abstractexceltest;


Import com.easyReport.read.temp.PropertyTemp;

Import com.easyReport.read.temp.ReadTemp;


public class Testexcelcompro extends Abstractexceltest<student> {

@Override

public void Test () {

FileInputStream FileInputStream;

try {

FileInputStream = new FileInputStream (New File ("Testfile/test2.xls"));

Testreadbean (FileInputStream);

Testreadmap (FileInputStream);

Fileinputstream.close ();

} catch (Exception e) {

E.printstacktrace ();

}

}


@Override

Protected Readtemp bulidreadtemp () {

Readtemp readtemp = new Readtemp (1);

Readtemp.addpropertytemp (New Propertytemp ("name", "name"));

Readtemp.addpropertytemp (New Propertytemp ("Age", "ages");

Readtemp.addpropertytemp (New Propertytemp ("Account", "Course.name"));

Readtemp.addpropertytemp (New Propertytemp ("Score", "Course.score"));

Teachers settings and Course object property settings are the same

Readtemp.addpropertytemp (New Propertytemp ("Teacher's name", "Teachers.name"));

Readtemp.addpropertytemp (New Propertytemp ("Teacher Gender", "Teachers.sex"));

return readtemp;

}


@Override

protected void Writebeanlist (list<student> modellists) {

System.out.println ("--------------bean---------------");

for (Student s:modellists) {

System.out.println (S.getname () + "|"

+ s.getage () +s.getcourse (). GetName () + "|" +s.getcourse (). Getscore () + "|" +s.getteachers (). Get (0));

}

}


@Override

protected void Writemaplist (list<map<string, object>> maplists) {

SYSTEM.OUT.PRINTLN ("--------------map---------------");

For (map<string, object> m:maplists) {

System.out.println (M.get ("name") + "|" + m.get ("age") + "|" + M.get ("course.name") + "|" + M.get ("Course.score") +

"|" +m.get ("teachers.name") + "|" +m.get ("Teachers.sex"));

}

}


}

Operation Result:

--------------Bean---------------

Bachelor |12 Chinese |80.0| Zhang Sanfeng | male

Attention:

The collection and array contents must be user-defined objects, and the resulting array or collection content has only one.

If you need to add multiple objects, you need to implement them manually, as explained in the next section.

Teacher Sex When we deposit the time is often f,m form of expression, then need to import gender data format conversion.

We can implement the format conversion by teacher Setsex. Here we mainly explain the implementation of the Idataformater interface to achieve data formatting.

Package formater;


Import Com.easyReport.dataformater.IDataFormater;


public class Sexformater implements idataformater{

public static final String MALE = "M";

public static final String FEMALE = "F";

@Override

public Object Format (object value) {

return format (value, NULL);

}


@Override

public Object Format (object value, String format) {

if (value = = null)

Return "";

String sex = value.tostring ();

if (Sex.contains ("male"))

return MALE;

if (Sex.contains ("female"))

return FEMALE;

return value;

}


}

Excel import demo Testexcelcompro modified as follows

Protected Readtemp bulidreadtemp () {

Readtemp readtemp = new Readtemp (1);

Readtemp.addpropertytemp (New Propertytemp ("name", "name"));

Readtemp.addpropertytemp (New Propertytemp ("Age", "ages");

Readtemp.addpropertytemp (New Propertytemp ("Account", "Course.name"));

Readtemp.addpropertytemp (New Propertytemp ("Score", "Course.score"));

Readtemp.addpropertytemp (New Propertytemp ("Teacher's name", "Teachers.name"));

Readtemp.addpropertytemp (New Propertytemp ("Teacher Gender", "Teachers.sex", New Sexformater ()));

return readtemp;

}

Operation Result:

--------------Bean---------------

Bachelor |12 Chinese |80.0| Zhang Sanfeng | M


This article is from "Rookie Bobo" blog, please be sure to keep this source http://zhangxiao2.blog.51cto.com/6342789/1585861

Java easyreport Import Excel, TXT data composite properties (ii)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.