POI做报表

反复查阅了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();
  }

 
原文地址:https://www.cnblogs.com/zhima/p/7474834.html