JAVA操作Excel 可配置,动态 生成复杂表头 复杂的中国式报表表头

转载:开源社区http://www.oschina.net/code/snippet_1424099_49530?p=2代码] [Java]代码 该代码实现了Excel复杂表头的生成 基于sql server 的一张树表 你们可以 看代码自行建库

package com.jingjiu.util;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
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.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.Region;
import org.aspectj.weaver.patterns.ThisOrTargetAnnotationPointcut;

import com.google.common.collect.Table.Cell;

public class GenerExcle {

    HSSFWorkbook wb =null;
    HSSFSheet sheet =null;
    HSSFCellStyle style = null;

    public void reData(){
        //所有表头集合
        List allTableHead = new ArrayList();

    }

    public GenerExcle(){

          wb = new HSSFWorkbook();
          sheet = wb.createSheet("new   sheet");
          style = wb.createCellStyle(); // 样式对象
          style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直
          style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平     

          //style.setBottomBorderColor(HSSFCellStyle.BORDER_DOTTED);
          style.setWrapText(true);

    }
    /**
     * 保存生成的文件
     * @param name
     */
    public void saveAaSpecName(String name){
        OutputStream out;
        try {
            out = new FileOutputStream(name);
            wb.write(out);

            System.out.println(out);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    public  void addRow(int count){
        for(int i = 0; i<count; i++)
         sheet.createRow((short) i);
    }
    public void testAll(){
        this.addRow(3);
        sheet.addMergedRegion(new Region(0,(short)0,2,(short)0));
        HSSFRow row = sheet.getRow(0);
        HSSFCell cell = row.createCell(0);
        cell.setCellValue("公司名");
        this.saveAaSpecName("e:/like.xls");

    }
    /**
     * 合并cell并设置值
     * @param startRow
     * @param startCell
     * @param endRow
     * @param endCell
     * @param leve
     * @param cellMove
     * @param content
     */
    public  void mergeCell(int startRow,int startCell,int endRow,int endCell,int leve,int cellMove,String content){

        sheet.addMergedRegion(new Region(startRow,(short)startCell,endRow,(short)endCell));
        HSSFRow row = sheet.getRow(startRow);
        HSSFCell cell= row.createCell(cellMove);

        cell.setCellValue(content);

        cell.setCellStyle(style);

    }
    public StringBuffer excleToHtml(){
        ExcelShower es = new ExcelShower();
        StringBuffer excleToHtml = es.excleToHtml(this.wb);
        return excleToHtml;

    }

      public  static void main(String [] args){
          GenerExcle ge = new GenerExcle();
        // ge.testAll();
          if(true){
            //  return;
          }
          int reportId = 1;
          getReportHeaderById(ge, reportId);
      }
     /**
      * 根据报表ID遍历该报表的所有表头
      * 并以主表头为原子单位进行处理
      * @param ge
      * @param reportId
      */
    private static void getReportHeaderById(GenerExcle ge, int reportId) {
        //拿到煤报表的一级所有一级表头
          String sql =
                  "with tem_header(id,pid,headervalue,leve,custom_col) as(\n" +
                          "\n" +
                          "select id ,pid, headvalue,leve, custom_col from dbo.headerinfo where pid = 0 and report_id = ?\n" +
                          "union all\n" +
                          "select  a.id,a.pid,a.headvalue,a.leve ,a.custom_col from dbo.headerinfo a ,tem_header b\n" +
                          "where a.pid = b.id\n" +
                            "\n"+
                            "and  a.report_id = ?"+
                          "\n" +
                          "\n" +
                          ")\n" +
                          "select * from tem_header where  pid = 0";

          PreparedStatement ps = null;
          ResultSet rs  = null;
          Connection connection = null;
          try {
            connection= DBUtil.getConnection();
            ps = connection.prepareStatement(sql);
            ps.setInt(1, reportId);
            ps.setInt(2, reportId);
            ps.executeQuery();
            rs = ps.getResultSet();
            int max = 0;

            while (rs.next()) {
                int id = rs.getInt("id");
                int pid = rs.getInt("pid");
                String headvalue = rs.getString("headervalue");
                int leve = rs.getInt("leve");
                System.out.println("ID"+id+"Pid"+pid+"头名"+headvalue);
                //先判断最深层有多少层
                int maxLeve = getMaxLeve(reportId,id,connection,ps,rs);
                if(maxLeve>max){
                    max = maxLeve;
                }

            }
            //根据最深层添加行
            ge.addRow(max);
            ps.executeQuery();
            rs = ps.getResultSet();
            int currentIndex = 0;
            int index = 0;
            Map<String, Integer> moveInfoMap = new HashMap<String, Integer>();
            Map<String, Integer> leveMoveInfoMap = new HashMap<String, Integer>();

                moveInfoMap.put("currentIndex",0);
                moveInfoMap.put("cellMove", 0);

            while (rs.next()) {
                moveInfoMap.put("index",index);
                int id = rs.getInt("id");
                int pid = rs.getInt("pid");
                String headvalue = rs.getString("headervalue");
                int leve = rs.getInt("leve");
                int custom_col = rs.getInt("custom_col");
                if( leveMoveInfoMap.get(leve+"")==null){
                    leveMoveInfoMap.put(leve+"",0);
                }
                System.out.println("ID"+id+"Pid"+pid+"头名"+headvalue);

                generHeader(id,
                                     pid,
                                     headvalue,
                                     custom_col,
                                     max,
                                     rs,
                                     ps,
                                     connection,
                                     ge,
                                     reportId,
                                     leve,
                                     currentIndex,
                                     moveInfoMap,
                                     leveMoveInfoMap);
                //当前索引自加1
                currentIndex++;
                index++;
            }

        //  ge.saveAaSpecName("e:/today.xls");
            ge.excleToHtml();
            DBUtil.closeResources(connection, ps, rs);

            System.out.println(connection);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            DBUtil.closeResources(connection, ps, rs);
        }
    }

/**
 * 判断最深有多少层
 * @param i
 * @param id
 * @param rs
 * @param ps
 * @return
 */

    private static int getMaxLeve(int reportId, int id,Connection con, PreparedStatement ps, ResultSet rs) {
        String sql=
                "with tem_header(id,pid,headervalue,leve) as(\n" +
                        "\n" +
                        "select id ,pid, headvalue,leve from dbo.headerinfo where pid = 0 and report_id = ? and id = ?\n" +
                        "union all\n" +
                        "select  a.id,a.pid,a.headvalue,a.leve from dbo.headerinfo a ,tem_header b\n" +
                        "where a.pid = b.id\n" +
                        "\n"+
                        "and  a.report_id = ?"+
                        "\n" +
                        ")\n" +
                        "select count(1) from (select t.leve from tem_header t group by t.leve) mm";
     try {
    ps =    con.prepareStatement(sql);
    ps.setObject(1, reportId);
    ps.setObject(2, id);
    ps.setObject(3,reportId);
    rs  = ps.executeQuery();
    if(rs.next())
    return rs.getInt(1);
    return -1;
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
        return 0;
    }

    private static void generAfterHeader(int id,
            int pid,
            String headvalue,
            int max,
            ResultSet rs,
            PreparedStatement ps,
            Connection connection,
            GenerExcle ge,
            int reportId,
            int leve,
            int currentIndex,
            Map<String, Integer> moveInfoMap,
            Map<String, Integer> leveMoveInfoMap,
            Map<String, Map<String,Integer>> repairMovePoint,
            int index){

                            /**
                             * 处理多级表头的合并
                             */
                    //但到表头的直接后继表头
                    rs = getDirAfter(id,pid,reportId,leve,connection,ps,rs);
                    //存放直接后继的子头集合
                    List<DataModel> listResultSets = new ArrayList<DataModel>();
                    boolean flag = false;
                    try {
                    //  rs.last();
                    //  int size = rs.getRow();
                        //rs.first();

                        while(rs.next()){
                        int _id= rs.getInt("id");
                        int _pid = rs.getInt("pid");
                        String _headvalue = rs.getString("headervalue");
                        int _leve = rs.getInt("leve");
                        if( leveMoveInfoMap.get(_leve+"")==null){
                            leveMoveInfoMap.put(_leve+"",0);
                        }
                        Map<String,Integer> infoMovePlus = null;
                        if(repairMovePoint.get(_leve+1+"")==null){
                             infoMovePlus = new HashMap<String, Integer>();
                        }else{
                        infoMovePlus =repairMovePoint.get(_leve+1+"");
                        }
                        infoMovePlus.put(0+"", 0);
                        infoMovePlus.put(rs.getRow()+"", 0);
                        repairMovePoint.put(_leve+1+"", infoMovePlus);
                        /**
                         * 判断该表头有没有直接后继节点
                         * 如果有不做向下合并处理
                         * 如果没有则合并下面的空格
                         */

                        //如果不是最后一行则会发生空出一格的情况这时候需要进行行合并
                    int _temDownMovePonit = 0;
                    int _currentMaxLeve = leveMoveInfoMap.get("currentMaxLeve");
                        if(_leve<_currentMaxLeve&&_leve>1){
                            ResultSet _rs = getDirAfter(_id, _pid, reportId, _leve, connection, ps, rs);
                            //如果没有直接后继节点
                            if(!_rs.next()){
                                //判断需要向下合并几个位移量
                                _temDownMovePonit = 3-_leve;
                                //让以下每一级的指针右移1位补空位
                        /*      if(row==1){
                                for(int i = 0;i<3-_leve;i++){

                                //  leveMoveInfoMap.put(_leve+i+1+"",leveMoveInfoMap.get(_leve+i+1+"")+1);

                                }
                                }*/
                                //如果是连续有值
                                if(infoMovePlus.get(rs.getRow()-1+"")!=null&&infoMovePlus.get(rs.getRow()-1+"")>=1){
                            //      infoMovePlus.put(rs.getRow()-1+"",infoMovePlus.get(rs.getRow()-1+"")+1);
                                }

                                infoMovePlus.put(rs.getRow()+"",infoMovePlus.get(rs.getRow()-1+"")+1);

                            }

                        }

                        int plusPoint = 0;
                        if(!flag){
                        if(repairMovePoint.get(_leve+"")!=null){
                            Map<String, Integer> map = repairMovePoint.get(_leve+"");
                            int _rowCount = rs.getRow();
                            for(int i = 1;i<=index;i++)
                            if(map.get(i+"")!=null){
                            plusPoint = map.get(i+"");
                            }
                            flag = true;

                        }
                        }
                        int _sizeMoveCount = leveMoveInfoMap.get(_leve-1+"");
                        int sizeMoveCount = leveMoveInfoMap.get(_leve+"")+plusPoint;
                    //  int _tem=moveInfoMap.get("cellMove");
                        int start =_leve-1+(repairMovePoint.get("mainRowCount").get("mainRowCount")-1);
                        int maxCurrentLeveMegreCell = getMaxCurrentLeveMegreCell(_id, reportId, _pid, ps, rs, connection);
                    //  ge.mergeCell(_leve-1, sizeMoveCount, _leve-1+_temDownMovePonit, sizeMoveCount+maxCurrentLeveMegreCell-1, _leve, sizeMoveCount, _headvalue);
                            ge.mergeCell(start, sizeMoveCount, start+_temDownMovePonit, sizeMoveCount+maxCurrentLeveMegreCell-1, _leve, sizeMoveCount, _headvalue);
                        leveMoveInfoMap.put(_leve+"",sizeMoveCount+maxCurrentLeveMegreCell);
                        //_temDownMovePonit= 0;

                        DataModel dm = new DataModel();
                        dm.setId(_id);
                        dm.setPid(_pid);
                        dm.setHeadvalue(_headvalue);
                        dm.setMax(max);
                        dm.setRs(rs);;
                        dm.setPs(ps);
                        dm.setConnection(connection);
                        dm.setGe(ge);
                        dm.setReportId(reportId);
                        dm.setLeve(_leve);
                        dm.setCurrentIndex(currentIndex);
                        dm.setMoveInfoMap(leveMoveInfoMap);
                        dm.setLeveMoveInfoMap(leveMoveInfoMap);
                        dm.setRepairMoveInfo(repairMovePoint);

                        listResultSets.add(dm);

                    //  getDirAfter(_id, _pid, reportId, maxCurrentLeveMegreCell, connection, ps, rs);
                        //break;
                    //  System.out.println("leve 2ID"+_id+"Pid"+_pid+"头名"+_headvalue+""+maxCurrentLeveMegreCell);
                        }
                        /**
                         * 判断该表的最终级别有几列 说明该表的合并列为多少
                         * 所有单元格的合并最终是看当前表头开始它的最终有多少个子表头
                         * 也就是最后一行
                         */
                        if(listResultSets!=null&&listResultSets.size()>0){

                            for(int i = 0 ;i<listResultSets.size() ; i ++){
                                DataModel dModel = listResultSets.get(i);
                                dModel.setIndex(i);
                                generAfterHeader(dModel.getId() , dModel.getPid(), dModel.getHeadvalue(),
                                        dModel.getMax(),
                                        dModel.getRs(),
                                        dModel.getPs(),
                                        dModel.getConnection(),
                                        dModel.getGe(),
                                        dModel.getReportId(),
                                        dModel.getLeve(),
                                        dModel.getCurrentIndex(),
                                        dModel.getMoveInfoMap(),
                                        dModel.getLeveMoveInfoMap(),
                                        dModel.getRepairMoveInfo(),
                                        dModel.getIndex());

                            }

                        }

                    } catch (SQLException e2) {
                        // TODO Auto-generated catch block
                        e2.printStackTrace();
                    }

    }

   /**
    * 生成表头
    * @param id
    * @param pid
    * @param headvalue
    * @param max
    * @param max
    * @param rs
    * @param ps
    * @param connection
    * @param ge
    * @param leve
    * @param currentIndex
    * @param moveInfoMap
    * @param leveMoveInfoMap
    */
    private static void generHeader(int id,
                                                       int pid,
                                                       String headvalue,
                                                       int customCol,
                                                       int max, ResultSet rs,
                                                       PreparedStatement ps,
                                                       Connection connection,
                                                       GenerExcle ge,
                                                       int reportId,
                                                       int leve,
                                                       int currentIndex,
                                                       Map<String, Integer> moveInfoMap,
                                                       Map<String, Integer> leveMoveInfoMap) {

        //获取当前主表头应该跨几列
        int maxMoveCell = getMaxCurrentLeveMegreCell(id,reportId,pid,ps,rs,connection);
        Map<String, Integer> _moveInfoMap = new HashMap<String, Integer>();
        //获得该主表头的层级
        int currentMaxLeve  = getMaxLeve(reportId, id, connection, ps, rs);
        //获取当前主表头应该占几行
        int mainRowCount = max - (currentMaxLeve -1);
        if(mainRowCount!=max){
            getMax(id, pid, rs, ps, connection, reportId, leve, _moveInfoMap,currentMaxLeve);
            if(_moveInfoMap.get("addOtherCell")!=null){
            String _max = _moveInfoMap.get("addOtherCell").toString();
            maxMoveCell +=Integer.parseInt(_max);
            }
        }
        //以主表头层级关系每开一个主表头的位移量想当于下一个主表头的开始量(这是个累加的过程)
        maxMoveCell+=customCol;
        if(leveMoveInfoMap.get("allColMove")==null){
            leveMoveInfoMap.put("allColMove",maxMoveCell);
        }else{
            leveMoveInfoMap.put("allColMove",leveMoveInfoMap.get("allColMove")+maxMoveCell);
        }
        /*
         * 把当前主表头的层级关系带进去 以便控制子表如果下沉合并时的位移量
         * */
        leveMoveInfoMap.put("currentMaxLeve", currentMaxLeve);
        System.out.println(maxMoveCell);
        //初始化每级行的位移量
        for(int i = 1 ;i<=max;i++){
            if(leveMoveInfoMap.get(i+"")==null)
                leveMoveInfoMap.put(i+"",0);
        }
        //进行单元格合并
        ge.mergeCell(leve-1,
                             moveInfoMap.get("cellMove"),
                             mainRowCount-1,
                             moveInfoMap.get("cellMove")+maxMoveCell-1,
                             leveMoveInfoMap.get(leve+""),
                             moveInfoMap.get("cellMove"),
                             headvalue);

        moveInfoMap.put("currentIndex",moveInfoMap.get("currentIndex")+maxMoveCell);
        moveInfoMap.put("cellMove",moveInfoMap.get("cellMove")+maxMoveCell);
        //如果当前表应该占几行与最大行相同则说明该表头没有子表头
        if (mainRowCount == max) {
            // 如果该主表头没有子表则所有单元格只位移1个
            for (int i = 1; i <= max; i++) {
                leveMoveInfoMap.put(i + "", leveMoveInfoMap.get(i + "") + 1+customCol);
            }
            return;
        }
        /**
         * 开始处理后继表头的合并工作
         */
        Map<String,Map<String,Integer>> repairMovePoint  = new HashMap<String,Map<String,Integer>>();
        Map<String,Integer> _tem = new HashMap<String, Integer>();
        _tem.put("mainRowCount", mainRowCount);
        repairMovePoint.put("mainRowCount",_tem);
       generAfterHeader(id,
                                   pid,
                                   headvalue,
                                   maxMoveCell,
                                   rs,
                                   ps,
                                   connection,
                                   ge,
                                   reportId,
                                   leve,
                                   currentIndex,
                                   moveInfoMap,
                                   leveMoveInfoMap,
                                   repairMovePoint,0);
        //当一个主表头以级它下面的子表头的填冲合并工作完成后记录最大移动了多少列
       //因为下一个主表头的所有数据填冲都会心这个为开始
    for(int i=1;i<=max;i++){
            leveMoveInfoMap.put(i + "", leveMoveInfoMap.get("allColMove"));
    }
    }

    /**
     * 获取当前层级元素应该合并多少个单元格
     * @param id
     * @param reportId
     * @param pid
     * @param ps
     * @param rs
     * @param connection
     * @return
     */
    private static int getMaxCurrentLeveMegreCell(int id, int reportId, int pid,
            PreparedStatement ps, ResultSet rs, Connection connection) {
    /*  String maxCell =
                "with tem_header(id,pid,headervalue,leve) as(\n" +
                        "\n" +
                        "select id ,pid, headvalue,leve from dbo.headerinfo where pid = ? and report_id = ?  and id =?\n" +
                        "union all\n" +
                        "select  a.id,a.pid,a.headvalue,a.leve from dbo.headerinfo a ,tem_header b\n" +
                        "where a.pid = b.id\n" +
                        "\n and a.report_id = ?" +
                        "\n" +
                        "\n" +
                        ")\n" +
                        "select max(aa.a) from (select count(1) a from tem_header   group by leve) aa";*/
        String maxCell = 

                                "with tem_header(id,pid,headervalue,leve) as(\n" +
                                "\n" +
                                "            select id ,pid, headvalue,leve from dbo.headerinfo where pid = ? and report_id = ?  and id =?\n" +
                                "            union all\n" +
                                "            select  a.id,a.pid,a.headvalue,a.leve from dbo.headerinfo a ,tem_header b\n" +
                                "            where a.pid = b.id\n" +
                                "             and a.report_id = ?\n" +
                                "\n" +
                                "\n" +
                                "            )\n" +
                                "\n" +
                                "\n" +
                                "            select top 1 aa.a  from (select count(1) a ,leve le from tem_header   group by leve) aa order by aa.le desc";

        try {
            ps = connection.prepareStatement(maxCell);
            ps.setInt(1, pid);
            ps.setInt(2,reportId);
            ps.setInt(3, id);
            ps.setInt(4,reportId);
            ps.executeQuery();
            rs = ps.getResultSet();
            if(rs.next()){
                return  rs.getInt(1);
            }
        } catch (SQLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        return 0;
    }

/**
 *
 * @param id
 * @param pid
 * @param leve
 * @param leve2
 * @param rs
 * @param ps
 * @param connection
 * @return
 */
private static ResultSet getDirAfter(int id,
                                                       int pid,
                                                       int reportid,
                                                       int leve,
                                                       Connection connection,
                                                       PreparedStatement ps,
                                                       ResultSet rs) {
    String sql =
            "\n" +
                    "with tem_header(id,pid,headervalue,leve) as(\n" +
                    "\n" +
                    "select id ,pid, headvalue,leve from dbo.headerinfo where pid = ? and report_id = ?\n" +
                    "union all\n" +
                    "select  a.id,a.pid,a.headvalue,a.leve from dbo.headerinfo a ,tem_header b\n" +
                    "where a.pid = b.id\n" +
                    "\n"+
                    "and  a.report_id = ?"+
                    "\n" +
                    "\n" +
                    ")\n" +
                    "select * from tem_header t  where t.leve = ?";
    try {
        ps = connection.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
        ps.setInt(1, id);
        ps.setInt(2,reportid);
        ps.setInt(4, leve+1);
        ps.setInt(3,reportid);
        ps.executeQuery();
        return ps.getResultSet();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return null;
} 

private static void getMax(int id,
        int pid,
        ResultSet rs,
        PreparedStatement ps,
        Connection connection,
        int reportId,
        int leve,
        Map<String, Integer> moveInfoMap,
        int currentMaxLeve){

                        /**
                         * 处理多级表头的合并
                         */
                //但到表头的直接后继表头
                rs = getDirAfter(id,pid,reportId,leve,connection,ps,rs);

                /*try {
                    if(!rs.next()&&leve!=3){
                    if(moveInfoMap.get("addOtherCell")==null){
                        moveInfoMap.put("addOtherCell", 0);
                        moveInfoMap.put("addOtherCell", moveInfoMap.get("addOtherCell")+1);
                    }

                    }
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }*/
            //  rs = getDirAfter(id,pid,reportId,leve,connection,ps,rs);
                //存放直接后继的子头集合
                List<DataModel> listResultSets = new ArrayList<DataModel>();
                try {
                    while(rs.next()){
                    int _id= rs.getInt("id");
                    int _pid = rs.getInt("pid");
                    String _headvalue = rs.getString("headervalue");
                    int _leve = rs.getInt("leve");
                    //如果当前层级不是最深层
                    //判断该表头有没有直接后继节点
                    if(_leve<currentMaxLeve){

                        ResultSet _rs = getDirAfter(_id,_pid,reportId,_leve,connection,ps,rs);
                        //如果没有直接后继节点
                        if(!_rs.next()){

                            if(moveInfoMap.get("addOtherCell")==null){
                                moveInfoMap.put("addOtherCell", 0);
                                moveInfoMap.put("addOtherCell", moveInfoMap.get("addOtherCell")+1);
                            }else{
                                moveInfoMap.put("addOtherCell", moveInfoMap.get("addOtherCell")+1);
                            }

                        }

                    }

                    DataModel dm = new DataModel();
                    dm.setId(_id);
                    dm.setPid(_pid);
                    dm.setHeadvalue(_headvalue);

                    dm.setRs(rs);;
                    dm.setPs(ps);
                    dm.setConnection(connection);
                    dm.setReportId(reportId);
                    dm.setLeve(_leve);
                    dm.setMoveInfoMap(moveInfoMap);
                    listResultSets.add(dm);

                //  getDirAfter(_id, _pid, reportId, maxCurrentLeveMegreCell, connection, ps, rs);
                    //break;
                //  System.out.println("leve 2ID"+_id+"Pid"+_pid+"头名"+_headvalue+""+maxCurrentLeveMegreCell);
                    }
                    /**
                     * 判断该表的最终级别有几列 说明该表的合并列为多少
                     * 所有单元格的合并最终是看当前表头开始它的最终有多少个子表头
                     * 也就是最后一行
                     */
                    if(listResultSets!=null&&listResultSets.size()>0){

                        for(int i = 0 ;i<listResultSets.size() ; i ++){
                            DataModel dModel = listResultSets.get(i);
                            getMax(dModel.getId() , dModel.getPid(), 

                                    dModel.getRs(),
                                    dModel.getPs(),
                                    dModel.getConnection(),
                                    dModel.getReportId(), dModel.getLeve(),dModel.getMoveInfoMap(),
                                    currentMaxLeve

                                    );

                        }

                    }

                } catch (SQLException e2) {
                    // TODO Auto-generated catch block
                    e2.printStackTrace();
                }

}

}
时间: 2024-11-03 21:47:18

JAVA操作Excel 可配置,动态 生成复杂表头 复杂的中国式报表表头的相关文章

java操作Excel

一.POI简介 Jakarta POI 是apache的子项目,目标是处理ole2对象.它提供了一组操纵Windows文档的Java API 目前比较成熟的是HSSF接口,处理MS Excel(97-2002)对象.它不象我们仅仅是用csv生成的没有格式的可以由Excel转换的东西,而是真正的Excel对象,你可以控制一些属性如sheet,cell等等. 二.HSSF概况 HSSF 是Horrible SpreadSheet Format的缩写,也即“讨厌的电子表格格式”.也许HSSF的名字有点

Java 操作 Excel (读取Excel2007,Poi实现)

关于Java读取Excel2007的文章在Google.百度上搜索一下,没有太好的例子,实现的也不算太好.查看了一下Poi,最新的 POI 3.5 beta 4 支持读写 Excel2007和PPT2007(XLSX and PPTX),自己来实现Java读取Excel2007了. 1,下载 POI 3.5 beta 4 解压,把其中的jar包导入项目文件.以我的读取为例,导入了以下jar包.  没有配置 log4j,测试时报告警报信息,应该为加载顺序导致的初始化问题造成(暂时没有找原因). 2

java 操作 Excel,java导出excel

WritableWorkbook out = null; try { response.getServletResponse().reset(); ((HttpServletResponse) response.getServletResponse()).setHeader("Content-Disposition", "attachment;filename=export.xls"); response.getServletResponse().setConten

Java 操作 Excel

这次的任务是实现数据的导入/导出功能,就是用Java操作Excel,上网找了一下,就Java来说用Apache的POI库的比较多,功能也相对比较强大.其他Excel开发库: http://www.oschina.net/project/tag/258/excel-tools Apache POI 下载地址: http://poi.apache.org/download.html 废话不多说,直接给几个例子就了解怎么用了. Excel 2003 (xls后缀) 1 import java.io.F

java操作Excel处理数字类型的精度损失问题验证

java操作Excel处理数字类型的精度损失问题验证: 场景: CELL_TYPE_NUMERIC-->CELL_TYPE_STRING--->CELL_TYPE_NUMERIC POI版本:poi-3.10.1poi-3.9 Code: package poi; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.i

java 操作excel 文件

JAVA EXCEL API:是一开放源码项目,通过它Java开发人员可以读取Excel文件的内容.创建新的Excel文件.更新已经存在的Excel文件.使用该API非Windows操作系统也可以通过纯Java应用来处理Excel数据表.因为它是使用Java编写的,所以我们在Web应用中可以通过JSP.Servlet来调用API实现对Excel数据表的访问. 下载: 官方网站 http://www.andykhan.com/jexcelapi/ 下载最新版本(本人下的是jexcelapi_2_6

Java操作excel表格

最近老师布置了个任务,用Java对excel后缀名为xlsx的文件进行简单的增,删,改,查操作:虽说是个简单的程序,可作为刚接触的我来说还是有些磕磕碰碰.不过好在还是完成了,进行一个简单的总结. 首先导入了一个poi.jar 网上有很多这个资源可以下载 XSSFSheet sheet=null;XSSFWorkbook book=null; 一:查  (查找本地指定位置的excel表格,在控制台输出) public void print_excel(){ //获取excel表格的行数 int l

Java操作Excel之POI简单例子

21 /** 22 * 利用POI操作Excel表单 23 * 24 * 需要jar包: 25 * HSSF针对03及以前版本,即.xls后缀 26 * |---poi-3.16.jar 27 * XSSF针对07及以后版本,即xlsx后缀 28 * |---poi-3.16.jar 29 * |---poi-ooxml.3.16.jar 30 * |---poi-ooxml-schemas-3.16.jar 31 * |---xmlbeans-2.6.0.jar 32 * |---common

c#操作Excel模板,替换命名单元格或关键字形成报表

http://blog.sina.com.cn/s/blog_45eaa01a0102vqma.html一 建立Excel 模板文件 template.xls 1.1 插入命名单元格的方法: 左上角名称框,显示当前单元格的行列号C2,加入命名后会显示其命名name 方法一: (1) 点击 单元格“姓名”之后的单元格(2) 菜单 插入--名称--定义(3) 在框中输入 name 确保底部的引用位置为 =Users!$C$2   按“添加”.“确定”按钮 即可 方法二:(1) 点击 单元格“姓名”之