01 |
Excel2EntityConfig config = new Excel2EntityConfig(); |
02 |
String[] columns = { "name" , "password" , "birthday" }; |
03 |
config.setColumns(columns); |
04 |
// //設置日期的格式,和Excel裏的日期格式一至 |
05 |
// config |
06 |
// .setFormater(new SimpleDateFormat( |
07 |
// "yyyy.MM.dd")); |
08 |
// //設置從第行開始讀,忽略前4行 |
09 |
// config.setCurrPosittion(5); |
10 |
// //設置從第二列開始讀取,忽略第一列的數序號列 |
11 |
// config.setColStartPosittion(2); |
12 |
ExcelReader<TestEntity> excel = new ExcelReader<TestEntity>(); |
13 |
excel.setExcel2EntityConfig(config); |
14 |
|
15 |
File file = new File( "d:\\testEntity.xls" ); //把testEntity.xls文件複製到d: |
16 |
InputStream input = new FileInputStream(file); |
17 |
//若是現現EXCEl編碼問題引發的讀取問題,請將InputStream換成 ByteArrayInputStream 可解決問題 |
18 |
//ByteArrayInputStream input = new ByteArrayInputStream(byte[]) |
19 |
excel.InitExcelReader(input); |
20 |
try { |
21 |
TestEntity entity = new TestEntity(); |
22 |
excel.setEntity(entity); |
23 |
entity = excel.readLine(); |
24 |
|
25 |
while (entity != null ) { |
26 |
System.out.print(entity.getName()+ " " ); |
27 |
System.out.print(entity.getPassword()+ " " ); |
28 |
System.out.println(entity.getBirthday().toLocaleString()); |
29 |
///保存實體代碼 |
30 |
entity = new TestEntity(); |
31 |
excel.setEntity(entity); |
32 |
entity = excel.readLine(); |
33 |
} |
34 |
} catch (IOException e) { |
35 |
e.printStackTrace(); |
36 |
} catch (Exception e) { |
37 |
e.printStackTrace(); |
38 |
} finally { |
39 |
input.close(); |
40 |
} |