用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);
  
 }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章