java實(shí)現(xiàn)Excel的導(dǎo)入、導(dǎo)出
一、Excel的導(dǎo)入
導(dǎo)入可采用兩種方式,一種是JXL,另一種是POI,但前者不能讀取高版本的Excel(07以上),后者更具兼容性。由于對(duì)兩種方式都進(jìn)行了嘗試,就都貼出來分享(若有錯(cuò)誤,請(qǐng)給予指正)
方式一、JXL導(dǎo)入 所需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); //獲取總的列數(shù) int columns = sheet.getColumns(); //獲取總的行數(shù) 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集合,封裝成bean for(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; }
方式二、POI導(dǎo)入
所需jar包
poi-3.6-20091214.jar
poi-ooxml-3.6-20091214.jar
poi-ooxml-schemas-3.6-20091214.jar
xmlbeans-2.3.0.jar
dom4j-1.6.1.jar
jdom-2.0.6.jar
publicstaticList<PutStorageInfo> readExcelByPOI(String filePath){ List<PutStorageInfo> infoList =newArrayList<PutStorageInfo>(); Map<String,List<String>> map =newHashMap<String,List<String>>(); infoList.clear(); try{ InputStream is =newFileInputStream(filePath); int index = filePath.lastIndexOf("."); String postfix = filePath.substring(index+1); Workbook workbook =null; if("xls".equals(postfix)){ workbook =newHSSFWorkbook(is); }elseif("xlsx".equals(postfix)){ workbook =newXSSFWorkbook(is); } //獲取第1張表 Sheet sheet = workbook.getSheetAt(0); //總的行數(shù) int rows = sheet.getLastRowNum(); //總的列數(shù)--->最后一列為null則有問題,讀取不完整,將表頭的數(shù)目作為總的列數(shù),沒有的則補(bǔ)為null int columns = sheet.getRow(0).getLastCellNum(); //先列后行 for(int i =1; i <= rows; i++){ Row row = sheet.getRow(i); if(null!= row && row.getFirstCellNum()==-1){//這一行是空行,不讀取 continue; } //這一行的總列數(shù) // columns = row.getLastCellNum(); List<String> contentList =newArrayList<String>(); contentList.clear(); for(int j =1; j < columns; j++){ if(row.getCell(j)!=null){ row.getCell(j).setCellType(Cell.CELL_TYPE_STRING); contentList.add(row.getCell(j).getStringCellValue()); }else{ contentList.add(""); } } map.put("StorageInfo"+i, contentList); } //遍歷map集合,封裝成bean for(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; }
二、Excel導(dǎo)出
采用JXL實(shí)現(xiàn)
publicstaticvoid creatExcel(List<PutStorageInfo> storageInfoList,String fileName){ try{ OutputStream os =newFileOutputStream(fileName); //創(chuàng)建可寫的工作薄 WritableWorkbook workbook =Workbook.createWorkbook(os); //創(chuàng)建第一張表 WritableSheet sheet = workbook.createSheet("Sheet1",0); //設(shè)置根據(jù)內(nèi)容自動(dòng)寬度 CellView cellView =newCellView(); cellView.setAutosize(true); //在下邊f(xié)or循環(huán)中為每一列設(shè)置 //設(shè)置列寬度,此種方式參數(shù)的意思,i-->對(duì)應(yīng)的行或列 j-->要設(shè)置的寬度 // sheet.setColumnView(0, 100); // sheet.setRowView(0, 300); //設(shè)置字體加粗且背景顏色為黃色 WritableFont boldFont =newWritableFont(WritableFont.ARIAL,10,WritableFont.BOLD);//黑體 WritableCellFormat cellrFormate =newWritableCellFormat(boldFont); cellrFormate.setBackground(Colour.YELLOW); //先添加表頭 List<String> titleList = getTitleList(); //循環(huán)創(chuàng)建單元格,先列后行 for(int i =0; i < titleList.size(); i++){ //sheet.setColumnView(i, cellView); sheet.setColumnView(i,20); Label label =newLabel(i,0, titleList.get(i), cellrFormate); sheet.addCell(label); } LogUtil.logOut(JXLWriteExcel.class,storageInfoList.size()+""); String[][] content = convertToArr(storageInfoList); //設(shè)置content的自適應(yīng)當(dāng)前列的寬度,文本太對(duì)會(huì)自動(dòng)換行 new Label(j, i+1, content[i][j-1],contentFormat); WritableCellFormat contentFormat =newWritableCellFormat(); contentFormat.setWrap(true); //然后添加入庫(kù)信息條目 for(int i =0; i < storageInfoList.size(); i++){ Label labelID =newLabel(0,i+1,(i+1)+""); sheet.addCell(labelID); for(int j =1; j < titleList.size(); j++){ Label label =newLabel(j, i+1, content[i][j-1]); sheet.addCell(label); } } //把創(chuàng)建的內(nèi)容寫入到輸出流中,并關(guān)閉輸出流 workbook.write(); workbook.close(); os.close(); //將存儲(chǔ)了入庫(kù)bean的list清空 storageInfoList.clear(); }catch(Exception e){ e.printStackTrace(); } } privatestaticString[][] convertToArr(List<PutStorageInfo> storageInfoList){ String[][] content =newString[storageInfoList.size()][11]; for(int i =0; i < storageInfoList.size(); i++){ PutStorageInfo info = storageInfoList.get(i); //每個(gè)bean中總項(xiàng)有11項(xiàng) content[i][0]= info.getProductcode(); content[i][1]= info.getProductsort(); content[i][2]= info.getProductbrand(); content[i][3]= info.getProductname(); content[i][4]= info.getProductquantity(); content[i][5]= info.getProductcontent(); content[i][6]= info.getProductnetweight(); content[i][7]= info.getProductcountry(); content[i][8]= info.getProductpdate(); content[i][9]= info.getProductprice(); content[i][10]= info.getProductmark(); } return content; } privatestaticList<String> getTitleList(){ List<String> list =newArrayList<String>(); list.add("Item No."); list.add("Product code"); list.add("Sort"); list.add("Brand"); list.add("Product Name"); list.add("Quantity(Pieces)"); list.add("Content"); list.add("Net Weight"); list.add("Country"); list.add("Best before date"); list.add("Price(EURO)"); list.add("Remarks"); return list; }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Java實(shí)現(xiàn)Excel導(dǎo)入導(dǎo)出數(shù)據(jù)庫(kù)的方法示例
- java實(shí)現(xiàn)Excel的導(dǎo)入導(dǎo)出
- Java中Easypoi實(shí)現(xiàn)excel多sheet表導(dǎo)入導(dǎo)出功能
- java使用EasyExcel導(dǎo)入導(dǎo)出excel
- Java實(shí)現(xiàn)Excel導(dǎo)入導(dǎo)出操作詳解
- java利用easyexcel實(shí)現(xiàn)導(dǎo)入與導(dǎo)出功能
- java操作excel導(dǎo)入導(dǎo)出的3種方式
- Java使用EasyExcel實(shí)現(xiàn)Excel的導(dǎo)入導(dǎo)出
- Java如何使用poi導(dǎo)入導(dǎo)出excel工具類
- java如何在項(xiàng)目中實(shí)現(xiàn)excel導(dǎo)入導(dǎo)出功能
相關(guān)文章
Java基礎(chǔ)之MapReduce框架總結(jié)與擴(kuò)展知識(shí)點(diǎn)
本章,是MapReduce的最終章,我在寫本章的時(shí)候,發(fā)現(xiàn)前面忘記介紹MpaTask與ReduceTask了,所以本章補(bǔ)上哈,另外還有兩個(gè)擴(kuò)展的知識(shí)點(diǎn),講完這些,我會(huì)對(duì)整個(gè)MapReduce進(jìn)行總結(jié)一下,讓大家再次了解MapReduce的工作流程,更加清晰地認(rèn)識(shí)MapReduce ,需要的朋友可以參考下2021-05-05SpringBoot 下集成緩存工具類 CacheManager
這篇文章主要介紹了Springboot下集成緩存工具類CacheManager,想進(jìn)一步了解相關(guān)知識(shí)的同學(xué),可以詳細(xì)閱讀本文2023-03-03Springboot @Configuration @bean注解作用解析
這篇文章主要介紹了springboot @Configuration @bean注解作用解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02SpringBoot整合liquibase及l(fā)iquibase生成初始化腳本的方式
這篇文章主要介紹了SpringBoot整合liquibase的相關(guān)資料,文中給大家介紹了liquibase生成初始化腳本的兩種方式,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-02-02手把手帶你了解Java-Stream流方法學(xué)習(xí)及總結(jié)
這篇文章主要介紹了通過實(shí)例了解JavaStream流的方法學(xué)習(xí)和總結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2021-08-08Servlet+JavaBean+JSP打造Java Web注冊(cè)與登錄功能
比作MVC的話,控制器部分采用Servlet來實(shí)現(xiàn),模型部分采用JavaBean來實(shí)現(xiàn),而大部分的視圖采用Jsp頁(yè)面來實(shí)現(xiàn),接下來我們就來詳細(xì)看看如何用Servlet+JavaBean+JSP打造Java Web注冊(cè)與登錄功能2016-05-05代理模式:JAVA靜態(tài)代理和動(dòng)態(tài)代理的實(shí)例和實(shí)現(xiàn)詳解
這篇文章主要給大家介紹了關(guān)于Java靜態(tài)代理和動(dòng)態(tài)代理的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-08-08