首页 > 其他 > 详细

POI做报表

时间:2016-06-12 04:10:40      阅读:304      评论:0      收藏:0      [点我收藏+]

反复查阅了jxl方式的文档,个别丢失格式问题,不能解决。只好切换到POI方式。
POI一直开源并升级更新着。
?
[align=center][b]根据模板导出excel[/b][/align]

XXXAction :

public ActionForward exportExcle(ActionMapping mapping,ActionForm form,
   HttpServletRequest request,HttpServletResponse response){
  DebtUtil rp = new DebtUtil();
  ActionErrors errors = new ActionErrors();
  String fileName = "";
  try{
   fileName = rp.getFileName();
  }catch(Exception e){
   errors.add("success", new ActionError("message.warn","未找到模板路径,请联系管理员!"));
   saveErrors(request, errors);
   return mapping.findForward("success");
  }
  //选择模板文件
  response.setContentType("application/x-msdownload");
  response.setHeader("Content-Disposition","attachment; filename=\"" +Utf8Util.toUtf8String(fileName) + "\"");
  // 定义输出类型
  response.setContentType("application/msexcel");
try {
   rp.export(response.getOutputStream(),form);
} catch (Exception e) {
   e.printStackTrace();
   logger.debug(e);
  }
  return null;
 }

?
======导出方法

public void export(OutputStream out, ActionForm aform) throws Exception {
  HSSFWorkbook workbook;
  HSSFSheet sheet;
  HSSFRow row;
  HSSFCell cell = null;
  FileInputStream fis=null;
  try {
   // 选择模板文件
 String filePath = getModelPath("/模板");
 fis=new FileInputStream(filePath);
   workbook = new HSSFWorkbook(fis);
   sheet = workbook.getSheetAt(0);
   log.debug(filePath);
。。。。。
row = sheet.getRow(7);
row.getCell(2).setCellValue(your value);

workbook.getCreationHelper().createFormulaEvaluator().evaluateAll();
   workbook.write(out);
    } finally {
    workbook.close();
   if (null != out) {
    out.flush();
    out.close();
   }
   if (null != fis) 
    fis.close();
  }

 

?

POI做报表

原文:http://zhima.iteye.com/blog/2302487

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!