标签:
一、Excel的导入
1 publicstaticList<PutStorageInfo> readExcelByJXL(String filePath){ 2 List<PutStorageInfo> infoList =newArrayList<PutStorageInfo>(); 3 Map<String,List<String>> map =newHashMap<String,List<String>>(); 4 infoList.clear(); 5 try{ 6 InputStream is =newFileInputStream(filePath); 7 Workbook workbook =Workbook.getWorkbook(is); 8 //获取第1张表 9 Sheet sheet = workbook.getSheet(0); 10 //获取总的列数 11 int columns = sheet.getColumns(); 12 //获取总的行数 13 int rows = sheet.getRows(); 14 //先列后行(j,i) 15 for(int i =1; i < rows; i++){ 16 List<String> contentList =newArrayList<String>(); 17 contentList.clear(); 18 for(int j =1; j < columns; j++){ 19 contentList.add(sheet.getCell(j,i).getContents()); 20 } 21 map.put("StorageInfo"+i, contentList); 22 } 23 24 //遍历map集合,封装成bean 25 for(Map.Entry<String,List<String>> entry : map.entrySet()){ 26 List<String> list = entry.getValue(); 27 PutStorageInfo storageInfo =newPutStorageInfo(); 28 storageInfo.setProductcode(list.get(0)); 29 storageInfo.setProductsort(list.get(1)); 30 storageInfo.setProductbrand(list.get(2)); 31 storageInfo.setProductname(list.get(3)); 32 storageInfo.setProductquantity(list.get(4)); 33 storageInfo.setProductcontent(list.get(5)); 34 storageInfo.setProductnetweight(list.get(6)); 35 storageInfo.setProductcountry(list.get(7)); 36 storageInfo.setProductpdate(list.get(8)); 37 storageInfo.setProductprice(list.get(9)); 38 storageInfo.setProductmark(list.get(10)); 39 40 infoList.add(storageInfo); 41 } 42 is.close(); 43 }catch(Exception e){ 44 e.printStackTrace(); 45 } 46 return infoList; 47 }
方式二、POI导入
1 publicstaticList<PutStorageInfo> readExcelByPOI(String filePath){ 2 List<PutStorageInfo> infoList =newArrayList<PutStorageInfo>(); 3 Map<String,List<String>> map =newHashMap<String,List<String>>(); 4 infoList.clear(); 5 try{ 6 InputStream is =newFileInputStream(filePath); 7 8 int index = filePath.lastIndexOf("."); 9 String postfix = filePath.substring(index+1); 10 11 Workbook workbook =null; 12 if("xls".equals(postfix)){ 13 workbook =newHSSFWorkbook(is); 14 }elseif("xlsx".equals(postfix)){ 15 workbook =newXSSFWorkbook(is); 16 } 17 //获取第1张表 18 Sheet sheet = workbook.getSheetAt(0); 19 //总的行数 20 int rows = sheet.getLastRowNum(); 21 //总的列数--->最后一列为null则有问题,读取不完整,将表头的数目作为总的列数,没有的则补为null 22 int columns = sheet.getRow(0).getLastCellNum(); 23 //先列后行 24 for(int i =1; i <= rows; i++){ 25 Row row = sheet.getRow(i); 26 if(null!= row && row.getFirstCellNum()==-1){//这一行是空行,不读取 27 continue; 28 } 29 //这一行的总列数 30 // columns = row.getLastCellNum(); 31 List<String> contentList =newArrayList<String>(); 32 contentList.clear(); 33 for(int j =1; j < columns; j++){ 34 if(row.getCell(j)!=null){ 35 row.getCell(j).setCellType(Cell.CELL_TYPE_STRING); 36 contentList.add(row.getCell(j).getStringCellValue()); 37 }else{ 38 contentList.add(""); 39 } 40 } 41 map.put("StorageInfo"+i, contentList); 42 } 43 44 //遍历map集合,封装成bean 45 for(Map.Entry<String,List<String>> entry : map.entrySet()){ 46 List<String> list = entry.getValue(); 47 PutStorageInfo storageInfo =newPutStorageInfo(); 48 storageInfo.setProductcode(list.get(0)); 49 storageInfo.setProductsort(list.get(1)); 50 storageInfo.setProductbrand(list.get(2)); 51 storageInfo.setProductname(list.get(3)); 52 storageInfo.setProductquantity(list.get(4)); 53 storageInfo.setProductcontent(list.get(5)); 54 storageInfo.setProductnetweight(list.get(6)); 55 storageInfo.setProductcountry(list.get(7)); 56 storageInfo.setProductpdate(list.get(8)); 57 storageInfo.setProductprice(list.get(9)); 58 storageInfo.setProductmark(list.get(10)); 59 60 infoList.add(storageInfo); 61 } 62 is.close(); 63 }catch(Exception e){ 64 e.printStackTrace(); 65 } 66 67 return infoList; 68 }
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; }
标签:
原文地址:http://www.cnblogs.com/cczheng-666/p/4998107.html