基于POI的Excel导入导出(JAVA实现)
今天做了个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,这方面可以根据具体业务进行修改。
操作其实很简单,有什么问题欢迎留言交流。