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

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.printStackTrace();
  }
  
  return null;
 }

 
 public void export(List modelList, OutputStream os) throws Exception {
  HSSFWorkbook wb = new HSSFWorkbook();
  HSSFSheet sheet = wb.createSheet();
  HSSFCell cell;
  
  HSSFRow row;
  
  // 字体大小
  short fontHeight = -1;
  // 对齐方式, 默认居中
  short alignment  = -1;
  
  int titleRowNum  = 0;
  int desRowNum  = 1;
  int titleRowNum1 = 2;
  int titleRowNum2 = 3;
  int titleRowNum3 = 4;
  
  // 内容的第一行所在excel表格的行
  int bodyFirstRow = 4;
  
  //生成第一行标题
  row = sheet.createRow(titleRowNum);
  cell = row.createCell(this.TITLE_COL);
  
  fontHeight = (short) 20;

  // 设置字体
  HSSFFont boldFont = wb.createFont();
  boldFont.setFontName("SimSun");
  boldFont.setFontHeightInPoints(fontHeight);
  boldFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
  //
  Long corpId=AuthenticatorHolder.getClientSession().getUser().getCorpID();
  SysCorpService sysCorpService=(SysCorpService)Services.GetService(SysCorpService.SERVICE_NAME);
     String corpName=(sysCorpService.getCorpObj(corpId)).getCorpName();
  setCell(wb, cell, corpName+"季度投资和资金建议计划填报", boldFont, alignment, HSSFColor.WHITE.index);
  
  for (int i = 1; i < TITLE_COL_LEN; i++) {
   cell = row.createCell((short)i);
   setCell(wb, cell, "", null, alignment, HSSFColor.WHITE.index);
  }
  
  // 合并标题
  sheet.addMergedRegion(new Region(
    titleRowNum, this.TITLE_COL, titleRowNum, (short) (TITLE_COL_LEN - 1)));
  
  // 说明
  row = sheet.createRow(desRowNum);
  cell = row.createCell(this.DESC_COL);
  
  fontHeight = (short) 12;
  alignment  = HSSFCellStyle.ALIGN_LEFT;
  
  // 设置字体
  HSSFFont font = wb.createFont();
  font.setFontName("SimSun");
  font.setFontHeightInPoints(fontHeight);
  
  setCell(wb, cell, "",
    font, alignment, HSSFColor.WHITE.index);
  
  for (int i = 1; i < TITLE_COL_LEN; i++) {
   cell = row.createCell((short)i);
   setCell(wb, cell, "", null, alignment, HSSFColor.WHITE.index);
  }
  
  // 合并说明
  sheet.addMergedRegion(new Region(
    desRowNum, this.DESC_COL, desRowNum, (short) (TITLE_COL_LEN - 1)));
  
  
  //生成第二行标题
  HSSFRow row2;
  HSSFRow row3;
  row = sheet.createRow(titleRowNum1);
  row2 = sheet.createRow(titleRowNum2);
  row3=sheet.createRow(titleRowNum3);
  
  alignment = -1;
  
  /*-
   * 合并标题的参数的数组
   * 前两个表示:希望合并单元的格左上角单元格的坐标
   * 后两个表示:希望合并单元的格右下角单元格的坐标
   */
  int[][] mergeTitle = {
//    项目信息
    {titleRowNum1, this.ettprojInfo_COL, titleRowNum1, this.ettprojInfo_COL+1},
//    项目名称
    {titleRowNum2, this.ettprojname_COL, titleRowNum2 , this.ettprojname_COL},    
//    项目编号
    {titleRowNum2, this.code_COL, titleRowNum2, this.code_COL},   
//    年度资金计划
    {titleRowNum1, this.YEARFUNDPLAN_COL, titleRowNum1, this.YEARFUNDPLAN_COL+1},    
//    当年计划
    {titleRowNum2, this.yearfundplannow_COL, titleRowNum2, this.yearfundplannow_COL},
//    剩余计划
    {titleRowNum2, this.sparePlan_COL, titleRowNum2, this.sparePlan_COL},
//    到上季末累计资金计划
    {titleRowNum1, this.totalfundplan_COL, titleRowNum1+ 1, this.totalfundplan_COL},   
//    建议季度资金计划
    {titleRowNum1, this.fundplanadvice_COL, titleRowNum1, this.fundplanadvice_COL+4},
    // 合计
    {titleRowNum2, this.sum_COL, titleRowNum2, this.sum_COL},    
//    本季工程款预付
    {titleRowNum2, this.engineeringadvance_COL, titleRowNum2, this.engineeringadvance_COL+2},
//    第一月
    {titleRowNum3, this.engineeringadvance1_COL, titleRowNum3, this.engineeringadvance1_COL},    
//    第二月
    {titleRowNum3, this.engineeringadvance2_COL, titleRowNum3, this.engineeringadvance2_COL},   
//    第三月
    {titleRowNum3, this.engineeringadvance3_COL, titleRowNum3, this.engineeringadvance3_COL},    
//    本季物资款预付
    {titleRowNum2, this.monthadviceplanid1_COL, titleRowNum2, this.monthadviceplanid1_COL+2},
//    第一月
    {titleRowNum3, this.monthadviceplanid1_COL, titleRowNum3, this.monthadviceplanid1_COL},
//    第二月
    {titleRowNum3, this.monthadviceplanid2_COL, titleRowNum3, this.monthadviceplanid2_COL},   
//    第三月
    {titleRowNum3, this.monthadviceplanid3_COL, titleRowNum3, this.monthadviceplanid3_COL},
    //上季工程款结算
    {titleRowNum2, this.upengineeringadvance_COL, titleRowNum2, this.upengineeringadvance_COL+2},
    //
    // 第一月
    {titleRowNum3, this.upengineeringadvance1_COL, titleRowNum3, this.upengineeringadvance1_COL},    
//    第二月
    {titleRowNum3, this.upengineeringadvance2_COL, titleRowNum3, this.upengineeringadvance2_COL},
//    第三月
    {titleRowNum3, this.upengineeringadvance3_COL, titleRowNum3, this.upengineeringadvance3_COL},    
//    上季物资款结算
    {titleRowNum2, this.upmonthadvice_COL, titleRowNum2, this.upmonthadvice_COL+2},   
//    第一月
    {titleRowNum3, this.upmonthadvice1_COL, titleRowNum3, this.upmonthadvice1_COL},    
//    第二月
    {titleRowNum3, this.upmonthadvice2_COL, titleRowNum3, this.upmonthadvice2_COL},
//    第三月
    {titleRowNum3, this.upmonthadvice3_COL, titleRowNum3, this.upmonthadvice3_COL},     
    
  };
  
  
  short[][] mergeBody = {
//    项目信息
    {this.ettprojInfo_COL,this.ettprojInfo_COL},
//    项目名称
    {this.ettprojname_COL,this.ettprojname_COL},    
//    项目编号
    {this.code_COL,this.code_COL},   
//    年度资金计划
    {this.YEARFUNDPLAN_COL,this.YEARFUNDPLAN_COL},    
//    当年计划
    {this.yearfundplannow_COL,this.yearfundplannow_COL},
//    剩余计划
    {this.sparePlan_COL,this.sparePlan_COL},
//    到上季末累计资金计划
    {this.totalfundplan_COL,this.totalfundplan_COL},   
//    建议季度资金计划
    {this.fundplanadvice_COL,this.fundplanadvice_COL},
    // 合计
    {this.sum_COL,this.sum_COL},    
//    本季工程款预付
    {this.engineeringadvance_COL,this.engineeringadvance_COL},
//    第一月
    {this.engineeringadvance1_COL,this.engineeringadvance1_COL},    
//    第二月
    {this.engineeringadvance2_COL,this.engineeringadvance2_COL},   
//    第三月
    {this.engineeringadvance3_COL,this.engineeringadvance3_COL},    
//    本季物资款预付
    {this.monthadviceplanid1_COL,this.monthadviceplanid1_COL},
//    第一月
    {this.monthadviceplanid1_COL,this.monthadviceplanid1_COL},
//    第二月
    {this.monthadviceplanid2_COL,this.monthadviceplanid2_COL},   
//    第三月
    {this.monthadviceplanid3_COL,this.monthadviceplanid3_COL},
    //上季工程款结算
    {this.upengineeringadvance_COL,this.upengineeringadvance_COL},
    //
    // 第一月
    {this.upengineeringadvance1_COL,this.upengineeringadvance1_COL},    
//    第二月
    {this.upengineeringadvance2_COL, this.upengineeringadvance2_COL},
//    第三月
    {this.upengineeringadvance3_COL,this.upengineeringadvance3_COL},    
//    上季物资款结算
    {this.upmonthadvice_COL,this.upmonthadvice_COL},   
//    第一月
    {this.upmonthadvice1_COL,this.upmonthadvice1_COL},    
//    第二月
    {this.upmonthadvice2_COL,this.upmonthadvice2_COL},
//    第三月
    {this.upmonthadvice3_COL,this.upmonthadvice3_COL},     
  };
 
  cell = row.createCell(this.ettprojInfo_COL);
  setCell(wb, cell, "项目信息", null, alignment, HSSFColor.LIGHT_GREEN.index);
  cell = row2.createCell(this.ettprojInfo_COL);
  setCell(wb, cell, "", null, alignment, HSSFColor.LIGHT_GREEN.index);
  cell = row3.createCell(this.ettprojInfo_COL);
  setCell(wb, cell, "", null, alignment, HSSFColor.LIGHT_GREEN.index);
  
  cell = row2.createCell(this.ettprojname_COL);
  setCell(wb, cell, "项目名称", null, alignment, HSSFColor.LIGHT_GREEN.index);
//  cell = row2.createCell(this.ettprojname_COL);
//  setCell(wb, cell, "", null, alignment, HSSFColor.LIGHT_GREEN.index);
  cell = row3.createCell(this.ettprojname_COL);
  setCell(wb, cell, "", null, alignment, HSSFColor.LIGHT_GREEN.index);
  
  cell = row2.createCell(this.code_COL);
  setCell(wb, cell, "项目编号", null, alignment, HSSFColor.LIGHT_GREEN.index);
//  cell = row2.createCell(this.code_COL);
//  setCell(wb, cell, "", null, alignment, HSSFColor.LIGHT_GREEN.index);
  cell = row3.createCell(this.code_COL);
  setCell(wb, cell, "", null, alignment, HSSFColor.LIGHT_GREEN.index);
  
  cell = row.createCell(this.YEARFUNDPLAN_COL);
  setCell(wb, cell, "年度资金计划", null, alignment, HSSFColor.LIGHT_GREEN.index);
  cell = row2.createCell(this.YEARFUNDPLAN_COL);
  setCell(wb, cell, "", null, alignment, HSSFColor.LIGHT_GREEN.index);
  cell = row3.createCell(this.YEARFUNDPLAN_COL);
  setCell(wb, cell, "", null, alignment, HSSFColor.LIGHT_GREEN.index);
  
  cell = row2.createCell(this.yearfundplannow_COL);
  setCell(wb, cell, "当年计划", null, alignment, HSSFColor.LIGHT_GREEN.index);
//  cell = row2.createCell(this.yearfundplannow_COL);
//  setCell(wb, cell, "", null, alignment, HSSFColor.LIGHT_GREEN.index);
  cell = row3.createCell(this.yearfundplannow_COL);
  setCell(wb, cell, "", null, alignment, HSSFColor.LIGHT_GREEN.index);
  
  cell = row2.createCell(this.sparePlan_COL);
  setCell(wb, cell, "剩余计划", null, alignment, HSSFColor.LIGHT_GREEN.index);
//  cell = row2.createCell(this.sparePlan_COL);
//  setCell(wb, cell, "", null, alignment, HSSFColor.LIGHT_GREEN.index);
  cell = row3.createCell(this.sparePlan_COL);
  setCell(wb, cell, "", null, alignment, HSSFColor.LIGHT_GREEN.index);
  
  cell = row.createCell(this.totalfundplan_COL);
  setCell(wb, cell, "到上季末累计资金计划", null, alignment, HSSFColor.LIGHT_GREEN.index);
  cell = row2.createCell(this.totalfundplan_COL);
  setCell(wb, cell, "", null, alignment, HSSFColor.LIGHT_GREEN.index);
  cell = row3.createCell(this.totalfundplan_COL);
  setCell(wb, cell, "", null, alignment, HSSFColor.LIGHT_GREEN.index);
  
  cell = row.createCell(this.fundplanadvice_COL);
  setCell(wb, cell, "建议季度资金计划", null, alignment, HSSFColor.LIGHT_GREEN.index);
  cell = row2.createCell(this.fundplanadvice_COL);
  setCell(wb, cell, "", null, alignment, HSSFColor.LIGHT_GREEN.index);
  cell = row3.createCell(this.fundplanadvice_COL);
  setCell(wb, cell, "", null, alignment, HSSFColor.LIGHT_GREEN.index);
  
  cell = row2.createCell(this.sum_COL);
  setCell(wb, cell, "合计", null, alignment, HSSFColor.LIGHT_GREEN.index);
//  cell = row2.createCell(this.sum_COL);
//  setCell(wb, cell, "", null, alignment, HSSFColor.LIGHT_GREEN.index);
  cell = row3.createCell(this.sum_COL);
  setCell(wb, cell, "", null, alignment, HSSFColor.LIGHT_GREEN.index);
  
  cell = row2.createCell(this.engineeringadvance_COL);
  setCell(wb, cell, "本季工程款预付", null, alignment, HSSFColor.LIGHT_GREEN.index);
//  cell = row2.createCell(this.engineeringadvance_COL);
//  setCell(wb, cell, "", null, alignment, HSSFColor.LIGHT_GREEN.index);
  cell = row3.createCell(this.engineeringadvance_COL);
  setCell(wb, cell, "", null, alignment, HSSFColor.LIGHT_GREEN.index);
  
  cell = row3.createCell(this.engineeringadvance1_COL);
  setCell(wb, cell, "第一月", null, alignment, HSSFColor.LIGHT_GREEN.index);
//  cell = row2.createCell(this.engineeringadvance1_COL);
//  setCell(wb, cell, "", null, alignment, HSSFColor.LIGHT_GREEN.index);
//  cell = row3.createCell(this.engineeringadvance1_COL);
//  setCell(wb, cell, "", null, alignment, HSSFColor.LIGHT_GREEN.index);
  
  cell = row3.createCell(this.engineeringadvance2_COL);
  setCell(wb, cell, "第二月", null, alignment, HSSFColor.LIGHT_GREEN.index);
//  cell = row2.createCell(this.engineeringadvance2_COL);
//  setCell(wb, cell, "", null, alignment, HSSFColor.LIGHT_GREEN.index);
//  cell = row3.createCell(this.engineeringadvance2_COL);
//  setCell(wb, cell, "", null, alignment, HSSFColor.LIGHT_GREEN.index);
  
  cell = row3.createCell(this.engineeringadvance3_COL);
  setCell(wb, cell, "第三月", null, alignment, HSSFColor.LIGHT_GREEN.index);
//  cell = row2.createCell(this.engineeringadvance3_COL);
//  setCell(wb, cell, "", null, alignment, HSSFColor.LIGHT_GREEN.index);
//  cell = row3.createCell(this.engineeringadvance3_COL);
//  setCell(wb, cell, "", null, alignment, HSSFColor.LIGHT_GREEN.index);
  
  cell = row2.createCell(this.monthadviceplanid_COL);
  setCell(wb, cell, "本季物资款预付", null, alignment, HSSFColor.LIGHT_GREEN.index);
//  cell = row2.createCell(this.monthadviceplanid_COL);
//  setCell(wb, cell, "", null, alignment, HSSFColor.LIGHT_GREEN.index);
  cell = row3.createCell(this.monthadviceplanid_COL);
  setCell(wb, cell, "", null, alignment, HSSFColor.LIGHT_GREEN.index);
  
  cell = row3.createCell(this.monthadviceplanid1_COL);
//  setCell(wb, cell, "第一月", null, alignment, HSSFColor.LIGHT_GREEN.index);
//  cell = row2.createCell(this.monthadviceplanid1_COL);
//  setCell(wb, cell, "", null, alignment, HSSFColor.LIGHT_GREEN.index);
//  cell = row3.createCell(this.monthadviceplanid1_COL);
//  setCell(wb, cell, "", null, alignment, HSSFColor.LIGHT_GREEN.index);
  
  cell = row3.createCell(this.monthadviceplanid2_COL);
  setCell(wb, cell, "第二月", null, alignment, HSSFColor.LIGHT_GREEN.index);
//  cell = row2.createCell(this.monthadviceplanid2_COL);
//  setCell(wb, cell, "", null, alignment, HSSFColor.LIGHT_GREEN.index);
//  cell = row3.createCell(this.monthadviceplanid2_COL);
//  setCell(wb, cell, "", null, alignment, HSSFColor.LIGHT_GREEN.index);
  
  cell = row3.createCell(this.monthadviceplanid3_COL);
  setCell(wb, cell, "第三月", null, alignment, HSSFColor.LIGHT_GREEN.index);
//  cell = row2.createCell(this.monthadviceplanid3_COL);
//  setCell(wb, cell, "", null, alignment, HSSFColor.LIGHT_GREEN.index);
//  cell = row3.createCell(this.monthadviceplanid3_COL);
//  setCell(wb, cell, "", null, alignment, HSSFColor.LIGHT_GREEN.index);
  
  cell = row2.createCell(this.upengineeringadvance_COL);
  setCell(wb, cell, "上季工程款结算", null, alignment, HSSFColor.LIGHT_GREEN.index);
//  cell = row2.createCell(this.upengineeringadvance_COL);
//  setCell(wb, cell, "", null, alignment, HSSFColor.LIGHT_GREEN.index);
  cell = row3.createCell(this.upengineeringadvance_COL);
  setCell(wb, cell, "", null, alignment, HSSFColor.LIGHT_GREEN.index);
  
  cell = row3.createCell(this.upengineeringadvance1_COL);
  setCell(wb, cell, "第一月", null, alignment, HSSFColor.LIGHT_GREEN.index);
//  cell = row2.createCell(this.upengineeringadvance1_COL);
//  setCell(wb, cell, "", null, alignment, HSSFColor.LIGHT_GREEN.index);
//  cell = row3.createCell(this.upengineeringadvance1_COL);
//  setCell(wb, cell, "", null, alignment, HSSFColor.LIGHT_GREEN.index);
  
  cell = row3.createCell(this.upengineeringadvance2_COL);
  setCell(wb, cell, "第二月", null, alignment, HSSFColor.LIGHT_GREEN.index);
//  cell = row2.createCell(this.upengineeringadvance2_COL);
//  setCell(wb, cell, "", null, alignment, HSSFColor.LIGHT_GREEN.index);
//  cell = row3.createCell(this.upengineeringadvance2_COL);
//  setCell(wb, cell, "", null, alignment, HSSFColor.LIGHT_GREEN.index);
  
  cell = row3.createCell(this.upengineeringadvance3_COL);
  setCell(wb, cell, "第三月", null, alignment, HSSFColor.LIGHT_GREEN.index);
//  cell = row2.createCell(this.upengineeringadvance3_COL);
//  setCell(wb, cell, "", null, alignment, HSSFColor.LIGHT_GREEN.index);
//  cell = row3.createCell(this.upengineeringadvance3_COL);
//  setCell(wb, cell, "", null, alignment, HSSFColor.LIGHT_GREEN.index);
 
  cell = row2.createCell(this.upmonthadvice_COL);
  setCell(wb, cell, "上季物资款结算", null, alignment, HSSFColor.LIGHT_GREEN.index);
//  cell = row2.createCell(this.upmonthadvice_COL);
//  setCell(wb, cell, "", null, alignment, HSSFColor.LIGHT_GREEN.index);
  cell = row3.createCell(this.upmonthadvice_COL);
  setCell(wb, cell, "", null, alignment, HSSFColor.LIGHT_GREEN.index);
  
  cell = row3.createCell(this.upmonthadvice1_COL);
  setCell(wb, cell, "第一月", null, alignment, HSSFColor.LIGHT_GREEN.index);
//  cell = row2.createCell(this.upmonthadvice1_COL);
//  setCell(wb, cell, "", null, alignment, HSSFColor.LIGHT_GREEN.index);
//  cell = row3.createCell(this.upmonthadvice1_COL);
//  setCell(wb, cell, "", null, alignment, HSSFColor.LIGHT_GREEN.index);
  
  cell = row3.createCell(this.upmonthadvice2_COL);
  setCell(wb, cell, "第二月", null, alignment, HSSFColor.LIGHT_GREEN.index);
//  cell = row2.createCell(this.upmonthadvice2_COL);
//  setCell(wb, cell, "", null, alignment, HSSFColor.LIGHT_GREEN.index);
//  cell = row3.createCell(this.upmonthadvice2_COL);
//  setCell(wb, cell, "", null, alignment, HSSFColor.LIGHT_GREEN.index);
  
  cell = row3.createCell(this.upmonthadvice3_COL);
  setCell(wb, cell, "第三月", null, alignment, HSSFColor.LIGHT_GREEN.index);
//  cell = row2.createCell(this.upmonthadvice3_COL);
//  setCell(wb, cell, "", null, alignment, HSSFColor.LIGHT_GREEN.index);
//  cell = row3.createCell(this.upmonthadvice3_COL);
//  setCell(wb, cell, "", null, alignment, HSSFColor.LIGHT_GREEN.index);
 
  
  // 左对齐
  alignment = HSSFCellStyle.ALIGN_LEFT;
  
 
  
  for (int j = 1; j <= modelList.size(); j++) {
   MonthadviceplanReport bean =
     (MonthadviceplanReport) modelList.get(j - 1);
   
   int rowNum = j * 2 - 2 + bodyFirstRow;
   
   row  = sheet.createRow(rowNum);
   row2 = sheet.createRow(rowNum + 1);
//   row3=sheet.createRow(rowNum + 2);
 
   //项目名称
   cell = row.createCell(this.ettprojname_COL);
   setCell(wb, cell, "", null, alignment, HSSFColor.WHITE.index);
   cell = row2.createCell(this.ettprojname_COL);
   setCell(wb, cell, "", null, alignment,HSSFColor.WHITE.index);
   cell = row3.createCell(this.ettprojInfo_COL);
   setCell(wb, cell, "", null, alignment,HSSFColor.WHITE.index);      
   
   //项目编号
   cell = row.createCell(this.code_COL);
   setCell(wb, cell,"", null, alignment, HSSFColor.WHITE.index);
   cell = row2.createCell(this.code_COL);
   setCell(wb, cell, "", null, alignment, HSSFColor.WHITE.index);
   cell = row3.createCell(this.ettprojInfo_COL);
   setCell(wb, cell, "", null, alignment,HSSFColor.WHITE.index); 
   
//   当年计划
   cell = row.createCell(this.yearfundplannow_COL);
   setCell(wb, cell,"", null, alignment, HSSFColor.WHITE.index);
   cell = row2.createCell(this.yearfundplannow_COL);
   setCell(wb, cell, "", null, alignment, HSSFColor.WHITE.index);
   cell = row3.createCell(this.ettprojInfo_COL);
   setCell(wb, cell, "", null, alignment,HSSFColor.WHITE.index); 
  
   // 剩余计划
   cell = row.createCell(this.sparePlan_COL);
   setCell(wb, cell, "", null, alignment, HSSFColor.WHITE.index);
   cell = row2.createCell(this.sparePlan_COL);
   setCell(wb, cell, "", null, alignment, HSSFColor.WHITE.index);
   cell = row3.createCell(this.ettprojInfo_COL);
   setCell(wb, cell, "", null, alignment,HSSFColor.WHITE.index); 
   
   // 到上季末累计资金计划
   cell = row.createCell(this.totalfundplan_COL);
   setCell(wb, cell, "", null, alignment, HSSFColor.WHITE.index);
   cell = row2.createCell(this.totalfundplan_COL);
   setCell(wb, cell, "", null, alignment, HSSFColor.WHITE.index);
   cell = row3.createCell(this.ettprojInfo_COL);
   setCell(wb, cell, "", null, alignment,HSSFColor.WHITE.index); 
   
   // 合计
   cell = row.createCell(this.sum_COL);
   setCell(wb, cell, "", null, alignment, HSSFColor.WHITE.index);
   cell = row2.createCell(this.sum_COL);
   setCell(wb, cell, "", null, alignment, HSSFColor.WHITE.index);
   cell = row3.createCell(this.ettprojInfo_COL);
   setCell(wb, cell, "", null, alignment,HSSFColor.WHITE.index); 
   
   //本季工程款预付
   // 第一月
   cell = row.createCell(this.engineeringadvance1_COL);
   setCell(wb, cell, "", null, alignment, HSSFColor.WHITE.index);
   cell = row2.createCell(this.engineeringadvance1_COL);
   setCell(wb, cell, "", null, alignment, HSSFColor.WHITE.index);
   cell = row3.createCell(this.ettprojInfo_COL);
   setCell(wb, cell, "", null, alignment,HSSFColor.WHITE.index); 
   
   // 第二月
   cell = row.createCell(this.engineeringadvance2_COL);
   setCell(wb, cell, "", null, alignment, HSSFColor.WHITE.index);
   cell = row2.createCell(this.engineeringadvance2_COL);
   setCell(wb, cell, "", null, alignment, HSSFColor.LIGHT_GREEN.index);
   cell = row3.createCell(this.ettprojInfo_COL);
   setCell(wb, cell, "", null, alignment,HSSFColor.WHITE.index); 
   
   // 第三月
   cell = row.createCell(this.engineeringadvance3_COL);
   setCell(wb, cell,"", null, alignment, HSSFColor.WHITE.index);
   cell = row2.createCell(this.engineeringadvance3_COL);
   setCell(wb, cell,"", null, alignment, HSSFColor.LIGHT_GREEN.index);
   cell = row3.createCell(this.ettprojInfo_COL);
   setCell(wb, cell, "", null, alignment,HSSFColor.WHITE.index); 
   
   //本季物资款预付
   // 第一月
   cell = row.createCell(this.monthadviceplanid1_COL);
   setCell(wb, cell, "", null, alignment, HSSFColor.WHITE.index);
   cell = row2.createCell(this.monthadviceplanid1_COL);
   setCell(wb, cell, "", null, alignment, HSSFColor.WHITE.index);
   cell = row3.createCell(this.ettprojInfo_COL);
   setCell(wb, cell, "", null, alignment,HSSFColor.WHITE.index); 
   
   // 第二月
   cell = row.createCell(this.monthadviceplanid2_COL);
   setCell(wb, cell, "", null, alignment, HSSFColor.WHITE.index);
   cell = row2.createCell(this.monthadviceplanid2_COL);
   setCell(wb, cell, "", null, alignment, HSSFColor.LIGHT_GREEN.index);
   cell = row3.createCell(this.ettprojInfo_COL);
   setCell(wb, cell, "", null, alignment,HSSFColor.WHITE.index); 
   
   // 第三月
   cell = row.createCell(this.monthadviceplanid3_COL);
   setCell(wb, cell,"", null, alignment, HSSFColor.WHITE.index);
   cell = row2.createCell(this.monthadviceplanid3_COL);
   setCell(wb, cell,"", null, alignment, HSSFColor.LIGHT_GREEN.index);
   cell = row3.createCell(this.ettprojInfo_COL);
   setCell(wb, cell, "", null, alignment,HSSFColor.WHITE.index); 
   
   //上季工程款结算
   // 第一月
   cell = row.createCell(this.upengineeringadvance1_COL);
   setCell(wb, cell, "", null, alignment, HSSFColor.WHITE.index);
   cell = row2.createCell(this.upengineeringadvance1_COL);
   setCell(wb, cell, "", null, alignment, HSSFColor.WHITE.index);
   cell = row3.createCell(this.ettprojInfo_COL);
   setCell(wb, cell, "", null, alignment,HSSFColor.WHITE.index); 
   
   // 第二月
   cell = row.createCell(this.upengineeringadvance2_COL);
   setCell(wb, cell, "", null, alignment, HSSFColor.WHITE.index);
   cell = row2.createCell(this.upengineeringadvance2_COL);
   setCell(wb, cell, "", null, alignment, HSSFColor.LIGHT_GREEN.index);
   cell = row3.createCell(this.ettprojInfo_COL);
   setCell(wb, cell, "", null, alignment,HSSFColor.WHITE.index); 
   
   // 第三月
   cell = row.createCell(this.upengineeringadvance3_COL);
   setCell(wb, cell,"", null, alignment, HSSFColor.WHITE.index);
   cell = row2.createCell(this.upengineeringadvance3_COL);
   setCell(wb, cell,"", null, alignment, HSSFColor.LIGHT_GREEN.index);
   cell = row3.createCell(this.ettprojInfo_COL);
   setCell(wb, cell, "", null, alignment,HSSFColor.WHITE.index); 
   
   //上季物资款结算
   // 第一月
   cell = row.createCell(this.upmonthadvice1_COL);
   setCell(wb, cell, "", null, alignment, HSSFColor.WHITE.index);
   cell = row2.createCell(this.upmonthadvice1_COL);
   setCell(wb, cell, "", null, alignment, HSSFColor.WHITE.index);
   cell = row3.createCell(this.ettprojInfo_COL);
   setCell(wb, cell, "", null, alignment,HSSFColor.WHITE.index); 
   
   // 第二月
   cell = row.createCell(this.upmonthadvice2_COL);
   setCell(wb, cell, "", null, alignment, HSSFColor.WHITE.index);
   cell = row2.createCell(this.upmonthadvice2_COL);
   setCell(wb, cell, "", null, alignment, HSSFColor.LIGHT_GREEN.index);
   cell = row3.createCell(this.ettprojInfo_COL);
   setCell(wb, cell, "", null, alignment,HSSFColor.WHITE.index); 
   // 第三月
   cell = row.createCell(this.upmonthadvice3_COL);
   setCell(wb, cell,"", null, alignment, HSSFColor.WHITE.index);
   cell = row2.createCell(this.upmonthadvice3_COL);
   setCell(wb, cell,"", null, alignment, HSSFColor.LIGHT_GREEN.index);
   cell = row3.createCell(this.ettprojInfo_COL);
   setCell(wb, cell, "", null, alignment,HSSFColor.WHITE.index); 
  }
  

  // 左对齐
  alignment = HSSFCellStyle.ALIGN_CENTER;
  
  // 合并标题
  for (int i = 0; i < mergeTitle.length; i++) {   
   sheet.addMergedRegion(
     new Region(mergeTitle[i][0], (short) mergeTitle[i][1],
       mergeTitle[i][2], (short) mergeTitle[i][3]));
  }

  // 左对齐
  alignment = HSSFCellStyle.ALIGN_LEFT;
  
  // 合并具体内容的单元格
  for (int i = 1; i <= modelList.size(); i++) {
   for (int j = 0; j < mergeBody.length; j++) {

    int rowNum = i * 2 - 2 + bodyFirstRow;
    
    sheet.addMergedRegion(
      new Region(rowNum, mergeBody[j][0], rowNum + 1, mergeBody[j][1]));
   }
  }

  short[] width = {
    5000, 6000, 4000,
    4000, 4000, 4000,
    4000, 4000, 4000,
    4000, 4000, 4000,
    4000, 4000, 4000,
    4000, 4000, 4000,
    4000, 4000, 5000};
  
  for (int i = 0; i < width.length; i++) {
   sheet.setColumnWidth((short)(i),width[i]);
  }
    
  wb.write(os);

 }

 public void setCell(HSSFWorkbook wb, HSSFCell cell, String value,
   HSSFFont boldFont, short alignment, short backgroundColor) {
  
  cell.setCellType(HSSFCell.CELL_TYPE_STRING);
  cell.setEncoding(HSSFCell.ENCODING_UTF_16);
  cell.setCellValue(value);
  
  HSSFCellStyle style = wb.createCellStyle();
  style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中
  style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中
  style.setWrapText(false);
  style.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
  style.setBorderBottom(HSSFCellStyle.BORDER_THIN);//下边框
  style.setBorderLeft(HSSFCellStyle.BORDER_THIN); // 左边框
  style.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框
  
  if (boldFont != null) {
   style.setFont(boldFont);
  }
  
  if (alignment != -1) {
   style.setAlignment(alignment);
  }

  style.setFillPattern(HSSFCellStyle.BIG_SPOTS);
  style.setFillForegroundColor(backgroundColor);
  style.setFillBackgroundColor(backgroundColor);
  
  cell.setCellStyle(style);
  
 }

你可能感兴趣的:(apache,String,Excel,null,import,alignment)