poi导入导出excel

   /**
     * 导入excel方法调用
     */
    public void importExcel(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        try {
            DiskFileItemFactory factory = new DiskFileItemFactory();  
            ServletFileUpload upload = new ServletFileUpload(factory);  
            List<FileItem> items = upload.parseRequest(request);
            if(items.size()>0&&!"".equals(inspectTypeId)) {
                FileItem fi = items.get(0);  
                InputStream is=fi.getInputStream();
                 BufferedInputStream in = new BufferedInputStream(is);
            }
            JsonUtil.ajaxJsonMessage(response, "导入成功",Message.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
        } 
        
    }

   /**
     * 导出excel

     * 先循环创建表头所有的单元格列,并设置边框样式,再根据行号,列号设置特定单元的值,

    * 最后才是合并,保证边框不** 丢失
     */
    public void exportExcel(HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        HSSFWorkbook wb = new HSSFWorkbook(); 
        HSSFCellStyle cellStyle = wb.createCellStyle();  
        cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
        cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
        cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
        cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
        cellStyle.setWrapText(true);//指定当单元格内容显示不下时自动换行 
        cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//指定单元格居中对齐    
        cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); //指定单元格垂直居中对齐  
        HSSFSheet sheet = wb.createSheet(“模板”); 
        //创建行
        HSSFRow row=sheet.createRow(0);

        HSSFCell cell = row.createCell(0);//创建单元格

        cell.setCellValue(filedDec);//赋值

       
        //合并单元格,起始行  起始列  结束行 结束列
        CellRangeAddress callRangeAddress = new CellRangeAddress(0,0,1,3);
        sheet.addMergedRegion(callRangeAddress);
        this.setBorderForMergeCell(1, callRangeAddress, sheet,wb) ;
        
        cell.setCellStyle(cellStyle);
      

        String filename=URLEncoder.encode(String.valueOf(System.currentTimeMillis()), "UTF-8")+".xls";
        response.reset();  
        response.setContentType("application/vnd.ms-excel");  
        response.setHeader("Content-Disposition","attachment;filename=\"" + filename + "\"");
         
        response.setHeader("Pragma", "no-cache");   
        response.setHeader("Cache-Control", "no-cache"); 
        response.setDateHeader("Expires", 0);  
        OutputStream output = response.getOutputStream();   
        try {
            wb.write(output); 
            output.flush();
        }  finally{
            if(output!=null){
                output.close();
            }
        }
    }
    /**
    合并单元格加边框
    **/
    private  void setBorderForMergeCell(int i,
            CellRangeAddress callRangeAddress,
            HSSFSheet sheet, 
            HSSFWorkbook workBook){
        RegionUtil.setBorderBottom(1, callRangeAddress, sheet,workBook);
        RegionUtil.setBorderLeft(1, callRangeAddress, sheet,workBook);
        RegionUtil.setBorderRight(1, callRangeAddress, sheet,workBook);
        RegionUtil.setBorderTop(1, callRangeAddress, sheet,workBook);
   }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值