您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了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集合,封裝成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導入
所需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); //總的行數 int rows = sheet.getLastRowNum(); //總的列數--->最后一列為null則有問題,讀取不完整,將表頭的數目作為總的列數,沒有的則補為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; } //這一行的總列數 // 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導出
采用JXL實現
publicstaticvoid creatExcel(List<PutStorageInfo> storageInfoList,String fileName){ try{ OutputStream os =newFileOutputStream(fileName); //創建可寫的工作薄 WritableWorkbook workbook =Workbook.createWorkbook(os); //創建第一張表 WritableSheet sheet = workbook.createSheet("Sheet1",0); //設置根據內容自動寬度 CellView cellView =newCellView(); cellView.setAutosize(true); //在下邊for循環中為每一列設置 //設置列寬度,此種方式參數的意思,i-->對應的行或列 j-->要設置的寬度 // sheet.setColumnView(0, 100); // sheet.setRowView(0, 300); //設置字體加粗且背景顏色為黃色 WritableFont boldFont =newWritableFont(WritableFont.ARIAL,10,WritableFont.BOLD);//黑體 WritableCellFormat cellrFormate =newWritableCellFormat(boldFont); cellrFormate.setBackground(Colour.YELLOW); //先添加表頭 List<String> titleList = getTitleList(); //循環創建單元格,先列后行 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); //設置content的自適應當前列的寬度,文本太對會自動換行 new Label(j, i+1, content[i][j-1],contentFormat); WritableCellFormat contentFormat =newWritableCellFormat(); contentFormat.setWrap(true); //然后添加入庫信息條目 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); } } //把創建的內容寫入到輸出流中,并關閉輸出流 workbook.write(); workbook.close(); os.close(); //將存儲了入庫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); //每個bean中總項有11項 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; }
以上就是關于java如何實現Excel的導入、導出操作的內容,如果你們有學習到知識或者技能,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。