采用POI讀取Excel的工具類
導入Excel一直都是信息系統的一項基本功能,它讓用戶的錄入工作變得輕松快捷,然而,對于程序員來說,為了實現這一功能卻需要花很多的精力去實現,如果Excel的表格字段夠多,那肯定夠你忙的了!
下面這個工具能幫助你輕松方便靈活的幫你實現Excel導入的功能。
Excel2EntityConfig config = new Excel2EntityConfig();
String[] columns = {"name", "password", "birthday"};
config.setColumns(columns);
// //設置日期的格式,和Excel里的日期格式一至
// config
// .setFormater(new SimpleDateFormat(
// "yyyy.MM.dd"));
// //設置從第行開始讀,忽略前4行
// config.setCurrPosittion(5);
// //設置從第二列開始讀取,忽略第一列的數序號列
// config.setColStartPosittion(2);
ExcelReader<TestEntity> excel = new ExcelReader<TestEntity>();
excel.setExcel2EntityConfig(config);
File file = new File("d:\\testEntity.xls"); //把testEntity.xls文件復制到d:
InputStream input = new FileInputStream(file);
//如果現現EXCEl編碼問題引起的讀取問題,請將InputStream換成 ByteArrayInputStream 可解決問題
//ByteArrayInputStream input = new ByteArrayInputStream(byte[])
excel.InitExcelReader(input);
try {
TestEntity entity = new TestEntity();
excel.setEntity(entity);
entity = excel.readLine();
while (entity != null) {
System.out.print(entity.getName()+" ");
System.out.print(entity.getPassword()+" ");
System.out.println(entity.getBirthday().toLocaleString());
///保存實體代碼
entity = new TestEntity();
excel.setEntity(entity);
entity = excel.readLine();
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally{
input.close();
} 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!