java poi方式的excel的 导入导出demo

由于项目中实际的代码比较复杂,这里就参考下简单版的导入导出函数

  • 导出excel文件

其中

response : 响应对象,用于直接返回给浏览器。

list: 内容数据,遍历填充单元格。

filename: 文件名。

title: excel第一行的标题数组。 

public void exportXml2007(HttpServletResponse response, List<List<Object>> list,String filename,String[] title){
        String[] header = title;
        /***生成Excel***/
			
        
        // 第一步,创建一个workbook,对应一个Excel文件  
        XSSFWorkbook wb = new XSSFWorkbook();
        // 第二步,在workbook中添加一个sheet,对应Excel文件中的sheet  
        XSSFSheet sheet = wb.createSheet(filename); 
        // 第三步,在sheet中添加表头第0行
        XSSFRow row = sheet.createRow((int) 0); 
        // 第四步,创建单元格,并设置 字体相关配置  
        XSSFCellStyle style = wb.createCellStyle(); 
        
        XSSFFont font = wb.createFont();
        font.setFontHeightInPoints((short) 11);
        font.setFontName("宋体");
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        style.setFillForegroundColor(HSSFColor.GREY_80_PERCENT.index);
        style.setFont(font);
        //依次0行中在添加cell,并设置对应字段名
        XSSFCell cell = null;
        for(int i=0;i<header.length;i++){
            cell = row.createCell((short) i);  
            cell.setCellValue(header[i]);  
            cell.setCellStyle(style);
        }
        
        if(list == null){
            return;
        }
  
        for (int i = 0; i < list.size(); i++)  
        {  
            row = sheet.createRow((int) i + 1);
            //依次从clist中读取相关数据,并写入excel
            List<Object> clist = list.get(i);
            for(int n=0;n<clist.size();n++) {
                Object value = clist.get(n);
                if(value instanceof Date){
                    row.createCell((short)n).setCellValue(fmt.format(value));
                }else{
                    row.createCell((short)n).setCellValue(clist.get(n).toString());
                }
            }
        }  
       
        try  
        {  
            //设置文件内容下载方式
            response.setContentType("application/force-download");
            response.setHeader("Content-Disposition", "attachment;filename=\"" +         
            java.net.URLEncoder.encode(filename, "UTF-8") + ".xlsx" + "\" ");
            wb.write(response.getOutputStream());  
            response.getOutputStream().close();  
        }  
        catch (Exception e)  
        {  
            e.printStackTrace();  
        }  
    }
  • 导入excel 文件

protected void readXls(InputStream is) throws IOException, InvalidFormatException {
        Workbook hssfWorkbook =  WorkbookFactory.create(is);
        
        for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {
            Sheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
            if (hssfSheet == null) {
                continue;
            }
            for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
                Row hssfRow = hssfSheet.getRow(rowNum);
                if (hssfRow != null) {
                    /**已经直接取数据行,无需判定
                    if(hssfRow.getCell(0) == null ){
                        continue;
                    }else if(hssfRow.getCell(0).getCellType() == Cell.CELL_TYPE_STRING){
                        String value = hssfRow.getCell(0).getStringCellValue();
                        try{
                            Integer.parseInt(value);
                        }catch(Exception e){
                            continue;
                        }
                        
                    }
                    */
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("jgId", getValue(hssfRow.getCell(1)));
                    map.put("name", getValue(hssfRow.getCell(3)));
                    map.put("cardType", getValue(hssfRow.getCell(4)).split("-")[0]);
                    map.put("cardNo", getValue(hssfRow.getCell(5)));
                    map.put("sex", getValue(hssfRow.getCell(6)).split("-")[0]);
                    map.put("birth", getValue(hssfRow.getCell(7)));
                    
                    
                }
            }
        }
    }

 

 

猜你喜欢

转载自blog.csdn.net/kevin_loving/article/details/81136055