用apache的poi在项目中的实际运用--倒出Excel报表

本文展示了如何使用Apache POI库在Java项目中生成Excel报表。代码详细定义了单元格样式、合并单元格,并提供了数据导出到Excel的流程。适用于企业级应用的数据导出场景。
摘要由CSDN通过智能技术生成

package com.report.invest.web.actions;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.hssf.util.Region;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.spsoft.framework.security.AuthenticatorHolder;
import com.spsoft.framework.security.ClientSession;
import com.spsoft.framework.struts.BasePerformAction;
import com.spsoft.global.service.Services;
import com.spsoft.report.invest.domain.MonthadviceplanReport;
import com.spsoft.report.invest.service.YearadviceplanService;
import com.spsoft.report.invest.web.forms.MonthadviceplanForm;
import com.spsoft.sysmgr.service.SysCorpService;

/**
 * @author gjy
 *
 */

public class QueryMonthAdvicePlanToExcelAction extends BasePerformAction {

  // 标题长度
 private final short TITLE_COL_LEN     = 20;
 // 标题
 private final short TITLE_COL     = 0;
 // 说明
 private final short DESC_COL     = 0;
 //项目信息
 private final short ettprojInfo_COL    =0;
 //项目名称
 private final short ettprojname_COL    =0;
 //项目编号
 private final short code_COL     =1;
 
 //年度资金计划
 private final short YEARFUNDPLAN_COL   =2;
 //当年计划
 private final short yearfundplannow_COL   =2;
 //剩余计划
 private final short sparePlan_COL    =3;
 //到上季末累计资金计划
 private final short totalfundplan_COL   =4;

 //建议季度资金计划
 private final short fundplanadvice_COL   =5;
// 合计
 private final short sum_COL      =5;
 //本季工程款预付
 private final short engineeringadvance_COL  =6;
 //第一月
 private final short engineeringadvance1_COL  =6;
 //第二月
 private final short engineeringadvance2_COL  =7;
 //第三月
 private final short engineeringadvance3_COL  =8;
 //本季物资款预付
 private final short monthadviceplanid_COL  =9;
 //第一月
 private final short monthadviceplanid1_COL  =9;
 //第二月
 private final short monthadviceplanid2_COL  =10;
 //第三月
 private final short monthadviceplanid3_COL  =11;
 
 //上季工程款结算
 private final short upengineeringadvance_COL =12;
 //第一月
 private final short upengineeringadvance1_COL =12;
 //第二月
 private final short upengineeringadvance2_COL =13;
 //第三月
 private final short upengineeringadvance3_COL =14;
 //上季物资款结算
 private final short upmonthadvice_COL   =15;
 //第一月
 private final short upmonthadvice1_COL   =15;
 //第二月
 private final short upmonthadvice2_COL   =16;
 //第三月
 private final short upmonthadvice3_COL   =17;
 
 
 public ActionForward perform(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {
  // TODO Auto-generated method stub
  
  YearadviceplanService service = (YearadviceplanService) Services
    .GetService(YearadviceplanService.SERVICE_NAME);
  
  MonthadviceplanForm objForm = (MonthadviceplanForm) form;
  ClientSession clientSession = AuthenticatorHolder.getClientSession();
  String userCode = clientSession.getUser().getUserCode();
  HashMap map = new HashMap();
  if (objForm.getYear() != null && !objForm.getYear().equals("")) {
   map.put("year", objForm.getYear());
   objForm.setYear(objForm.getYear());
  } else {
   Calendar date = Calendar.getInstance();
   map.put("year", date.get(Calendar.YEAR) + "");
   objForm.setYear(date.get(Calendar.YEAR) + "");
  }
  if (objForm.getQuarter() != null && !objForm.getQuarter().equals("")) {
   if (objForm.getQuarter().equals("1")) {
    map.put("month1", "1");
    map.put("month2", "2");
    map.put("month3", "3");
   } else if (objForm.getQuarter().equals("2")) {
    map.put("month1", "4");
    map.put("month2", "5");
    map.put("month3", "6");
   } else if (objForm.getQuarter().equals("3")) {
    map.put("month1", "7");
    map.put("month2", "8");
    map.put("month3", "9");
   } else if (objForm.getQuarter().equals("4")) {
    map.put("month1", "10");
    map.put("month2", "11");
    map.put("month3", "12");
   }
  } else {
   map.put("month1", "1");
   map.put("month2", "2");
   map.put("month3", "3");
  }
  
  String vol     = request.getParameter("voltageGrade");
  String corpId   = request.getParameter("corp");
  String isAll   = request.getParameter("recursive");
  
  request.setAttribute("voltageGrade", vol);
  request.setAttribute("corp", corpId);
  request.setAttribute("recursive", isAll);
  
  SysCorpService sysCorpService = (SysCorpService) Services.GetService(SysCorpService.SERVICE_NAME);
  String corpCode    = null;
  if (corpId != null) {
   corpCode    = sysCorpService.getCode(Long.decode(corpId));
  }
  
  if (vol != null && !vol.equals("10")) {
   map.put("vol", vol);
  }
  if (isAll != null && isAll.equals("true")) {
   if (corpCode != null) {
    map.put("corpCode", corpCode + "%");
   }
   map.put("isAll", "true");
  } else {
   if (corpCode != null) {
    map.put("corpCode", corpCode);
   }
   map.put("isAll", "false");
  }
  
  map.put("userName", userCode);
  List list = service.queryMonthPageReport(map);

  //把你查出要倒出的记录放入List调用export()方法就行了
  try {
   response.reset();
      response.setContentType("application/octet-stream");
      response.setHeader("Content-Disposition","attachment;filename="
        + new String("季度投资和资金建议计划填报.xls".getBytes("GBK"),"ISO-8859-1"));
  
   export(list, response.getOutputStream());
  } catch (IOException e) {
   e.printStackTrace();
  } catch (Exception e) {
   e.pr

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值