table of Contents
Maven introduction of the code
excel documents to use the JavaBean
JavaBean to use excel document
JavaBean converted into the red part of Excel document as the key code
Download excel_io-poi3.14 excel_io-poi3.17
excel_io.jar tool is dependent on the development and poi poi-ooxml two apached a jar made of, respectively, 3.14 and 3.17 dependent on both versions, please download the user's attention if the name is dependent excel_io-poi3.14.jar na poi version is 3.14. If the name is excel_io-poi3.17.jar, then that is dependent on the 3.17 version of poi.
Maven introduction of the code
POI introduced 3.14
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.14</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.14</version>
</dependency>
POI introduced 3.17
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
excel_io.jar use Description
-
Setting JavaBean
- Javabean properties required for use on excel document fields and corresponding annotations @ExcelProperty (ZH = "", Sequence = 0);
- @ExcelProperty annotation has three attributes are represented zh Chinese excel in the corresponding field name (required), the sequence shown in Sequence cexcel is, from zero, if not set to 0 by default all fields. whether the requisite set must have a value, the property is not being used in the tool, please pay attention to subsequent updates.
-
excel documents to use the JavaBean
- Create a class object Excel, Excel provides two class constructors are Excel (Class <T> clasz), Excel (Class <T> clasz, String sheetName)
- Excel (Class <T> clasz) incoming and excel documents corresponding JavaBean, but the excel sheet table must be the first. Or the import fails.
- Excel (Class <T> clasz, String sheetName) the difference between the front and a constructor is passed a sheet table name.
- Call readExcel (InputStream fis) method returns a List <T> objects;
-
JavaBean to use excel document
- As before, the class object to create Excel, no longer tired.
- Call createExcel (List <T> objs) , a set of parameters of objects JavaBean object data is not equipped. Returns a Workbook object, a Workbook object directly call the Write ( OutputStream Stream ) to generate excel documents.
JavaBean settings
@ExcelProperty(zh="生日",sequence=3)
private Date birthday;
@ExcelProperty(zh="姓名",sequence=0)
private String name;
@ExcelProperty(zh="手机号码",sequence=2)
private String phoneNum;
@ExcelProperty(zh="年龄",sequence=1)
private int age;
setter\getter……
JavaBean converted into the red part of Excel document as the key code
File file = new File("F:/poi/test/type.xlsx");
FileOutputStream fos = new FileOutputStream(file);
Excel<People> excel = new Excel<>(People.class);
List<People> peoples = new ArrayList<People>();
for(int i=0;i<10;i++) {
People p =new People();
p.setName("Mac"+i);
p.setAge(20+i);
p.setPhoneNum("1366111111"+i);
try {
p.setBirthday(new SimpleDateFormat("yyyy/MM/dd").parse("2018/10/31"));
} catch (ParseException e) {
e.printStackTrace();
}
peoples.add(p);
}
Workbook workbook =excel.createExcel(peoples);
workbook.write(fos);
The following code is converted to lead-in portion that is a JavaBean excel red part of the key codes
File file = new File("F:/poi/test/type.xlsx");
Excel<People> excel = new Excel<>(People.class);
InputStream fis = new FileInputStream(file);
List<People> peoples = excel.readExcel(fis);
for(People p:peoples) {
System.out.println(p);
}