导入Excel一直都是信息系统的一项基本功能,它让用户的录入工作变得轻松快捷,然而,对于程序员来说,为了实现这一功能却需要花很多的精力去实现,如果Excel的表格字段够多,那肯定够你忙的了!
下面这个工具能帮助你轻松方便灵活的帮你实现Excel导入的功能。它是我自己使用过程中总结完成的,请高手多指正!
jar文件里面包含有java源代码!
下面这个工具能帮助你轻松方便灵活的帮你实现Excel导入的功能。它是我自己使用过程中总结完成的,请高手多指正!
jar文件里面包含有java源代码!
代码片段(2)
[代码] [Java]代码
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 |
} |
来源:oschina
链接:https://my.oschina.net/u/103544/blog/39122