这篇文章主要为大家展示了java如何实现excel的导入、导出操作,内容简而易懂,希望大家可以学习一下,学习完之后肯定会有收获的,下面让小编带大家一起来看看吧。一、Excel的导入导入可采用两种方式,一种是JXL,另一种是POI,但前者不能
这篇文章主要为大家展示了java如何实现excel的导入、导出操作,内容简而易懂,希望大家可以学习一下,学习完之后肯定会有收获的,下面让小编带大家一起来看看吧。
一、Excel的导入
导入可采用两种方式,一种是JXL,另一种是POI,但前者不能读取高版本的Excel(07以上),后者更具兼容性。由于对两种方式都进行了尝试,就都贴出来分享(若有错误,请给予指正)
方式一、JXL导入 所需jar包 JXL.jar
publicstaticList<PutStorageInfo> readExcelByJXL(String filePath){List<PutStorageInfo> infoList =newArrayList<PutStorageInfo>();Map<String,List<String>> map =newHashMap<String,List<String>>(); infoList.clear();try{InputStream is =newFileInputStream(filePath);Workbook workbook =Workbook.getWorkbook(is);//获取第1张表Sheet sheet = workbook.getSheet(0);//获取总的列数int columns = sheet.getColumns();//获取总的行数int rows = sheet.getRows();//先列后行(j,i)for(int i =1; i < rows; i++){List<String> contentList =newArrayList<String>(); contentList.clear();for(int j =1; j < columns; j++){ contentList.add(sheet.getCell(j,i).getContents());} map.put("StorageInfo"+i, contentList);}//遍历map集合,封装成beanfor(Map.Entry<String,List<String>> entry : map.entrySet()){List<String> list = entry.getValue();PutStorageInfo storageInfo =newPutStorageInfo(); storageInfo.setProductcode(list.get(0)); storageInfo.setProductsort(list.get(1)); storageInfo.setProductbrand(list.get(2)); storageInfo.setProductname(list.get(3)); storageInfo.setProductquantity(list.get(4)); storageInfo.setProductcontent(list.get(5)); storageInfo.setProductnetweight(list.get(6)); storageInfo.setProductcountry(list.get(7)); storageInfo.setProductpdate(list.get(8)); storageInfo.setProductprice(list.get(9)); storageInfo.setProductmark(list.get(10)); infoList.add(storageInfo);} is.close();}catch(Exception e){ e.printStackTrace();}return infoList;}
--结束END--
本文标题: java如何实现Excel的导入、导出操作
本文链接: https://www.lsjlt.com/news/225155.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0