zk中用jxls导出excel

//在zk中获得session

static HttpSession session = (HttpSession) Sessions.getCurrent()
   .getNativeSession();
//tomcat

 /**
  * 輸入excel文件地址
  */
 private static String templateFileName =  session.getServletContext().getRealPath("/") + "form/form.xls";
/**
 * 輸出excel文件地址
 */
private static String destFileName = session.getServletContext().getRealPath("/")    + "form_output/output.xls";

//weblogic

 /**
  * 輸入excel文件地址
  */
 private static String templateFileName =  session.getServletContext().getResource("/").getPath()+ "form/form.xls";
/**
 * 輸出excel文件地址
 */
private static String destFileName = session.getServletContext().getResource("/").getPath()+ "form_output/output.xls";

 

//导出excel

Map map = new HashMap();
  map.put("key", value);

 

--第一种方式--

 // 執行導入excel
  XLSTransformer transformer = new XLSTransformer();
 try {
 transformer.transformXLS(templateFileName, map, destFileName);
} catch (ParsePropertyException e) {
  e.printStackTrace();
} catch (IOException e) {
  e.printStackTrace();
 }
 try {
   File f = new File(destFileName );
  Filedownload.save(f, null);
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  }

 

--第二种方式--
  InputStream in = null;
  HSSFWorkbook workbook = null;
  ByteArrayOutputStream bao = null;
  try {
    in = new BufferedInputStream(new FileInputStream(templateFileName));
    workbook = transformer.transformXLS(in,map);
    bao = new ByteArrayOutputStream();
    workbook.write(bao);
    Filedownload.save(bao.toByteArray(), "application/vnd.ms-excel", "output.xls");
  } catch (ParsePropertyException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
      }finally{
       try {
        if(in!=null){
     in.close();
        }
    if(bao!=null){
         bao.close();
       }
   } catch (IOException e) {
    e.printStackTrace();
   }
      }

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值