今天做了個excel的導入導出功能,在這記錄下。
首先現在相關poi的相關jar包,資源鏈接:http://download.csdn.net/detail/opening_world/9663247
具體過程就不多說了,直接上代碼吧。
導出excel代碼:
1 public void export2007(HttpServletResponse response, List<List<Object>> list,String filename,String[] title){ 2 String[] header = title; 3 4 XSSFWorkbook wb = new XSSFWorkbook(); 5 XSSFSheet sheet = wb.createSheet(filename); 6 XSSFRow row = sheet.createRow((int) 0); 7 XSSFCellStyle style = wb.createCellStyle(); 8 9 XSSFFont font = wb.createFont(); 10 font.setFontHeightInPoints((short) 11); 11 font.setFontName("宋體"); 12 font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); 13 14 style.setAlignment(HSSFCellStyle.ALIGN_CENTER); 15 style.setFillForegroundColor(HSSFColor.GREY_80_PERCENT.index); 16 style.setFont(font); 17 18 XSSFCell cell = null; 19 for(int i=0;i<header.length;i++){ 20 cell = row.createCell((short) i); 21 cell.setCellValue(header[i]); 22 cell.setCellStyle(style); 23 } 24 25 if(list == null){ 26 return; 27 } 28 29 for (int i = 0; i < list.size(); i++) 30 { 31 row = sheet.createRow((int) i + 1); 32 33 List<Object> clist = list.get(i); 34 for(int n=0;n<clist.size();n++) { 35 Object value = clist.get(n); 36 if(value instanceof Date){ 37 row.createCell((short)n).setCellValue(fmt.format(value)); 38 }else{ 39 row.createCell((short)n).setCellValue(clist.get(n).toString()); 40 } 41 } 42 } 43 44 try 45 { 46 response.setContentType("application/force-download"); 47 response.setHeader("Content-Disposition", "attachment;filename=\"" + java.net.URLEncoder.encode(filename, "UTF-8") + ".xlsx" + "\" "); 48 wb.write(response.getOutputStream()); 49 response.getOutputStream().close(); 50 } 51 catch (Exception e) 52 { 53 e.printStackTrace(); 54 } 55 }
參數解釋:
response : 響應對象,用於直接返回給瀏覽器。
list: 內容數據,遍歷填充單元格。
filename: 文件名。
title: excel第一行的標題數組。
導出2003版excel文件與上面代碼相似,只需使用另外一套類就行,感覺現在基本都是2007版了吧。
解析excel代碼:
1 protected void readXls(InputStream is) throws IOException, InvalidFormatException { 2 Workbook hssfWorkbook = WorkbookFactory.create(is); 3 4 for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) { 5 Sheet hssfSheet = hssfWorkbook.getSheetAt(numSheet); 6 if (hssfSheet == null) { 7 continue; 8 } 9 for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) { 10 Row hssfRow = hssfSheet.getRow(rowNum); 11 if (hssfRow != null) { 12 /**已經直接取數據行,無需判定 13 if(hssfRow.getCell(0) == null ){ 14 continue; 15 }else if(hssfRow.getCell(0).getCellType() == Cell.CELL_TYPE_STRING){ 16 String value = hssfRow.getCell(0).getStringCellValue(); 17 try{ 18 Integer.parseInt(value); 19 }catch(Exception e){ 20 continue; 21 } 22 23 } 24 */ 25 Map<String, Object> map = new HashMap<String, Object>(); 26 map.put("jgId", getValue(hssfRow.getCell(1))); 27 map.put("name", getValue(hssfRow.getCell(3))); 28 map.put("cardType", getValue(hssfRow.getCell(4)).split("-")[0]); 29 map.put("cardNo", getValue(hssfRow.getCell(5))); 30 map.put("sex", getValue(hssfRow.getCell(6)).split("-")[0]); 31 map.put("birth", getValue(hssfRow.getCell(7))); 32 33 34 } 35 } 36 } 37 }
傳入的參數是文件流InputStream,根據文件類型自動識別解析,因此.xls和.xlsx兩種格式都能解析,示例代碼是將解析的數據存入map,這方面可以根據具體業務進行修改。
操作其實很簡單,有什么問題歡迎留言交流。
