Java pio Excel(二)

Java Pio Excel 单个sheet的实例

package com.gohigh.report;

import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
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.CellRangeAddress;

public class Sheet1Report {
 public static boolean CreateSheet(HSSFSheet sheet,HSSFWorkbook wb,List conList){
  try{
   setTitle(sheet,wb);
   setContent(sheet,wb,conList);
   return true;
  }catch(Exception e){
   return false;
  }
 }
 private static void setContent(HSSFSheet sheet,HSSFWorkbook wb,List conList) throws Exception{
  HSSFCellStyle cellStyle=CreateMonthReport.getContentStyle(wb);
  HSSFCellStyle cellStyle2=CreateMonthReport.getContentStyle2(wb);
  HSSFCellStyle shdrStyle=CreateMonthReport.getShdrStyle(wb);
  HSSFCellStyle contentSumStyle=CreateMonthReport.getContentSumStyle(wb);
  HSSFCellStyle contentSumStyle2=CreateMonthReport.getContentSumStyle2(wb);
  String [][] department = MRService.department1;
  for(int i = 0 ;i<department.length;i++){
    String departmentID = department[i][1];
    HSSFRow conRow = sheet.createRow((int)i+3);
     //编号
    HSSFCell curCell=conRow.createCell(1);
       curCell.setCellStyle(shdrStyle);
       curCell.setCellValue(i+1);
     //公司
    curCell=conRow.createCell(2);
       curCell.setCellStyle(shdrStyle);
       curCell.setCellValue(new HSSFRichTextString(department[i][0]));
       boolean isBeing=false;
    for(int j=0;j<conList.size();j++){
     Map map = (Map)conList.get(j);
     String filialeID=(String)map.get("FILIALEID");
     if(filialeID==departmentID || filialeID.equals(departmentID)){
         //自建电路总数(折合2M)
      curCell=conRow.createCell(3);
         curCell.setCellStyle(cellStyle);
         curCell.setCellValue(Integer.parseInt((String)map.get("CIRSUM1G")));
         curCell=conRow.createCell(4);
         curCell.setCellStyle(cellStyle);
         curCell.setCellValue(Integer.parseInt((String)map.get("CIRSUM2G")));
         curCell=conRow.createCell(5);
         curCell.setCellStyle(cellStyle);
         curCell.setCellValue(Integer.parseInt((String)map.get("CIRSUMCITYCORE")));
         curCell=conRow.createCell(6);
         curCell.setCellStyle(cellStyle);
         curCell.setCellValue(Integer.parseInt((String)map.get("CIRSUMCITYCONN")));
      //自建电路总数(折合2M)
         curCell=conRow.createCell(7);
         curCell.setCellStyle(cellStyle);
         curCell.setCellValue(Integer.parseInt((String)map.get("SBCIRSUM1G")));
         curCell=conRow.createCell(8);
         curCell.setCellStyle(cellStyle);
         curCell.setCellValue(Integer.parseInt((String)map.get("SBCIRSUM2G")));
         curCell=conRow.createCell(9);
         curCell.setCellStyle(cellStyle);
         curCell.setCellValue(Integer.parseInt((String)map.get("SBCIRSUMCITYCORE")));
         curCell=conRow.createCell(10);
         curCell.setCellStyle(cellStyle);
         curCell.setCellValue(Integer.parseInt((String)map.get("SBCIRSUMCITYCONN")));
      //光缆线路总量(皮长公里)
         curCell=conRow.createCell(11);
         curCell.setCellStyle(cellStyle2);
         curCell.setCellValue(Integer.parseInt((String)map.get("LCABLESUM1G")));
         curCell=conRow.createCell(12);
         curCell.setCellStyle(cellStyle2);
         curCell.setCellValue(Integer.parseInt((String)map.get("LCABLESUM2G")));
         curCell=conRow.createCell(13);
         curCell.setCellStyle(cellStyle2);
         curCell.setCellValue(Integer.parseInt((String)map.get("LCABLESUMCITYCORE")));
         curCell=conRow.createCell(14);
         curCell.setCellStyle(cellStyle2);
         curCell.setCellValue(Integer.parseInt((String)map.get("LCABLESUMCITYCONN")));
      //自建光缆线路(皮长公里)
         curCell=conRow.createCell(15);
         curCell.setCellStyle(cellStyle2);
         curCell.setCellValue(Integer.parseInt((String)map.get("SBLCABLESUM1G")));
         curCell=conRow.createCell(16);
         curCell.setCellStyle(cellStyle2);
         curCell.setCellValue(Integer.parseInt((String)map.get("SBLCABLESUM2G")));
         curCell=conRow.createCell(17);
         curCell.setCellStyle(cellStyle2);
         curCell.setCellValue(Integer.parseInt((String)map.get("SBLCABLESUMCITYCORE")));
         curCell=conRow.createCell(18);
         curCell.setCellStyle(cellStyle2);
         curCell.setCellValue(Integer.parseInt((String)map.get("SBLCABLESUMCITYCONN")));
      //网管系统(套)
         curCell=conRow.createCell(19);
         curCell.setCellStyle(cellStyle);
         curCell.setCellValue(Integer.parseInt((String)map.get("NMSNET")));
         curCell=conRow.createCell(20);
         curCell.setCellStyle(cellStyle);
         curCell.setCellValue(Integer.parseInt((String)map.get("NMSNE")));
         curCell=conRow.createCell(21);
         curCell.setCellStyle(cellStyle);
         curCell.setCellValue(Integer.parseInt((String)map.get("NMSLCT")));
         //传输维护人员数量
         curCell=conRow.createCell(22);
         curCell.setCellStyle(cellStyle);
         curCell.setCellValue(Integer.parseInt((String)map.get("MAINTENANCER")));
      isBeing=true;
         break;
     }
    }
    //用于填充样式
    if(!isBeing){
     for(int k=3;k<=22;k++){
      curCell=conRow.createCell(k);
         curCell.setCellStyle(cellStyle);
     }
    }
//    合计(公式)
       int rowNo=i+4;    //行号
       HSSFCell cellFormula =conRow.createCell(23);
       cellFormula.setCellStyle(contentSumStyle2);
    cellFormula.setCellFormula("SUM(D"+rowNo+":G"+rowNo+")");
    cellFormula =conRow.createCell(24);
    cellFormula.setCellStyle(contentSumStyle2);
    cellFormula.setCellFormula("SUM(H"+rowNo+":K"+rowNo+")");
    cellFormula =conRow.createCell(25);
    cellFormula.setCellStyle(contentSumStyle);
    cellFormula.setCellFormula("SUM(L"+rowNo+":O"+rowNo+")");
    cellFormula =conRow.createCell(26);
    cellFormula.setCellStyle(contentSumStyle);
    cellFormula.setCellFormula("SUM(P"+rowNo+":S"+rowNo+")");
    cellFormula =conRow.createCell(27);
    cellFormula.setCellStyle(contentSumStyle2);
    cellFormula.setCellFormula("SUM(T"+rowNo+":V"+rowNo+")");
   }
  //各项合计
   HSSFRow  conSumRow = sheet.createRow(21);
   HSSFCell cellFormula =conSumRow.createCell(1);
   cellFormula.setCellStyle(CreateMonthReport.getHdrStyle(wb));
   cellFormula.setCellValue(new HSSFRichTextString("各项合计"));
   String [][]formula = new String[][]{{"3","SUM(D4:D21)"},{"4","SUM(E4:E21)"},{"5","SUM(F4:F21)"},{"6","SUM(G4:G21)"},
             {"7","SUM(H4:H21)"},{"8","SUM(I4:I21)"},{"9","SUM(J4:J21)"},{"10","SUM(K4:K21)"},
             {"11","SUM(L4:L21)"},{"12","SUM(M4:M21)"},{"13","SUM(N4:N21)"},{"14","SUM(O4:O21)"},
             {"15","SUM(P4:P21)"},{"16","SUM(Q4:Q21)"},{"17","SUM(R4:R21)"},{"18","SUM(S4:S21)"},
             {"19","SUM(T4:T21)"},{"20","SUM(U4:U21)"},{"21","SUM(V4:V21)"},{"22","SUM(W4:W21)"},
             {"23","SUM(X4:X21)"},{"24","SUM(Y4:Y21)"},{"25","SUM(Z4:Z21)"},{"26","SUM(AA4:AA21)"},
             {"27","SUM(AB4:AB21)"}};
   for(int n=0;n<formula.length;n++){
    cellFormula =conSumRow.createCell(Integer.parseInt(formula[n][0]));
    if((n == formula.length-2) || (n == formula.length-3) ||(n == formula.length-10)||(n == formula.length-11)||(n == formula.length-12)||(n == formula.length-13)||(n == formula.length-14)||(n == formula.length-15)||(n == formula.length-16)||(n == formula.length-17)){
     cellFormula.setCellStyle(contentSumStyle);
    }else {
     cellFormula.setCellStyle(contentSumStyle2);
    }
   
    cellFormula.setCellFormula(formula[n][1]);
   }
 }
 private static void setTitle(HSSFSheet sheet,HSSFWorkbook wb) throws Exception{
  HSSFCellStyle titStyle=CreateMonthReport.getTitStyle(wb);
  HSSFCellStyle hdrStyle=CreateMonthReport.getHdrStyle(wb);
  HSSFCellStyle shdrStyle=CreateMonthReport.getShdrStyle(wb);
  sheet.setColumnWidth((int)0, (int)(35.7*12));      //设置列宽,"35.7"为基准值,12这列宽的象素值
  sheet.setColumnWidth((int)1, (int)(35.7*31));  
  sheet.setColumnWidth((int)2, (int)(35.7*112));
  HSSFRow titleRow = sheet.createRow((int)0);
  titleRow.setHeight((short)(15.625*40));    //设置行高,"15.625"为基准值,40这列宽的象素值
  HSSFCell titCell =titleRow.createCell(1);
  titCell.setCellStyle(titStyle);
  titCell.setCellValue(new HSSFRichTextString("表(1)"));
  titCell =titleRow.createCell(3);
  titCell.setCellStyle(titStyle);
  titCell.setCellValue(new HSSFRichTextString("网络维护操作数据"));
  //一级标题
  titleRow = sheet.createRow((int)1);
  HSSFCell curCell=titleRow.createCell(1);
     curCell.setCellStyle(hdrStyle);
     curCell.setCellValue(new HSSFRichTextString("序列"));
     curCell=titleRow.createCell(2);
     curCell.setCellStyle(hdrStyle);
     curCell.setCellValue(new HSSFRichTextString("公司"));
     curCell=titleRow.createCell(3);
     curCell.setCellStyle(hdrStyle);
     curCell.setCellValue(new HSSFRichTextString("传输电路总数(折合2M)"));
     curCell=titleRow.createCell(7);
     curCell.setCellStyle(hdrStyle);
     curCell.setCellValue(new HSSFRichTextString("自建电路总数(折合2M)"));
     curCell=titleRow.createCell(11);
     curCell.setCellStyle(hdrStyle);
     curCell.setCellValue(new HSSFRichTextString("光缆线路总量(皮长公里)"));
     curCell=titleRow.createCell(15);
     curCell.setCellStyle(hdrStyle);
     curCell.setCellValue(new HSSFRichTextString("自建光缆线路(皮长公里)"));
     curCell=titleRow.createCell(19);
     curCell.setCellStyle(hdrStyle);
     curCell.setCellValue(new HSSFRichTextString("网管系统(套)"));
     curCell=titleRow.createCell(22);
     curCell.setCellStyle(hdrStyle);
     curCell.setCellValue(new HSSFRichTextString("传输维护人员数量(名)"));
     curCell=titleRow.createCell(23);
     curCell.setCellStyle(hdrStyle);
     curCell.setCellValue(new HSSFRichTextString("各项合计"));
  //二级标题
  titleRow = sheet.createRow((int)2);
  String title[] =new String[]{"一干","二干","城域核心","城域接入","一干","二干","城域核心","城域接入",
          "一干","二干","城域核心","城域接入","一干","二干","城域核心","城域接入",
          "网络级网管","网元级网管","本地维护终端","","传输电路总数","自建电路总数",
          "光缆线路总量","自建光缆线路","网管系统"};
  for(int i=0;i<title.length;i++){
   curCell=titleRow.createCell(i+3);
   curCell.setCellStyle(shdrStyle);
   curCell.setCellValue(new HSSFRichTextString(title[i]));
  }
//  合并单元格(起始行,结束行,起始列,结束列)
  String  mergerCell[][] =
   new String[][]{{"0","0","1","2","titStyle"},
         {"0","0","3","6","titStyle"},
         {"1","2","1","1","hdrStyle"},
         {"1","2","2","2","hdrStyle"},
         {"1","1","3","6","hdrStyle"},
         {"1","1","7","10","hdrStyle"},
         {"1","1","11","14","hdrStyle"},
         {"1","1","15","18","hdrStyle"},
         {"1","1","19","21","hdrStyle"},
         {"1","2","22","22","hdrStyle"},
         {"1","1","23","27","hdrStyle"},
         {"21","21","1","2","hdrStyle"},
   };
  for(int i =0;i<mergerCell.length;i++){
   int firstRow = Integer.parseInt(mergerCell[i][0]);
   short lastRow = (short)Integer.parseInt(mergerCell[i][1]);
   int firstColumn = Integer.parseInt(mergerCell[i][2]);
   short lastColumn = (short)Integer.parseInt(mergerCell[i][3]);
   CellRangeAddress regin=new CellRangeAddress(firstRow,lastRow,firstColumn,lastColumn);
   if(mergerCell[i][4]=="titStyle"){
    CreateMonthReport.setRegionStyle(sheet,regin,titStyle);
   }else if(mergerCell[i][4]=="hdrStyle"){
    CreateMonthReport.setRegionStyle(sheet,regin,hdrStyle);
   }
   sheet.addMergedRegion(regin);
  }
 }
 
}

你可能感兴趣的:(java,apache,Excel,J#)