一对多关系实体类对象excel导出

package org.ibas.util;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

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.CellRangeAddress;

//两个事件的测试
public class OtherSolutiaonFor2Event {

    public static void main(String[] agrs) throws Exception {

        String filenameZip = "D://testCascade1111235888999.xls";
        File pathFile = new File(filenameZip);
        if (pathFile.exists()) {
            pathFile.delete();// 删除文件
        }
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet();

        Entity0 e0 = new Entity0("00", "01", "02", "03");
        Entity0 e00 = new Entity0("000", "010", "020", "030");

        Entity1 e10 = new Entity1("10");
        Entity1 e11 = new Entity1("11");
        Entity1 e12 = new Entity1("12");

        Entity2 e20 = new Entity2("20");
        Entity2 e21 = new Entity2("21");

        Entity3 e30 = new Entity3("30");

        Entity4 e40 = new Entity4("40", "41", "42", "43", "44", "45");
        Entity4 e41 = new Entity4("401", "411", "421", "431", "441", "451");
        Entity4 e42 = new Entity4("40", "41", "42", "43", "44", "45");
        Entity4 e43 = new Entity4("40", "41", "42", "43", "44", "45");

        List<Entity0> list0 = new ArrayList<Entity0>();
        list0.add(e0);
        list0.add(e00);

        int startRowForMA = 0;
        // 将非0,非1的各个的大小值添加到一个集合中,求最小公倍数
        for (int i = 0; i < list0.size(); i++) { // 事件编号 从0开始,一直向后
            // 每个事件的相关类对应的单元格数
            // 查询得到所有的list,计算每一个事件对应的multiple
            List<Entity1> list1 = new ArrayList<Entity1>();
            list1.add(e10);
            list1.add(e11);
            list1.add(e12);

            List<Entity2> list2 = new ArrayList<Entity2>();
            list2.add(e20);
            list2.add(e21);

            List<Entity3> list3 = new ArrayList<Entity3>();
            list3.add(e30);

            List<Entity4> list4 = new ArrayList<Entity4>();
            list4.add(e40);
            list4.add(e41);

            int[] sizeArray = new int[13]; // 对应列的合并后的单元格的个数,13个属性,下标为属性下标
            sizeArray[0] = 1;
            sizeArray[1] = 1;
            sizeArray[2] = 1;
            sizeArray[3] = 1;

            sizeArray[4] = list1.size();
            sizeArray[5] = list2.size();
            sizeArray[6] = list3.size();

            sizeArray[7] = list4.size();
            sizeArray[8] = list4.size();
            sizeArray[9] = list4.size();
            sizeArray[10] = list4.size();
            sizeArray[11] = list4.size();
            sizeArray[12] = list4.size();

            List<Integer> sizeList = new ArrayList<Integer>();
            sizeList.add(1);
            sizeList.add(3);
            sizeList.add(3);
            sizeList.add(1);
            sizeList.add(4);

            int[] entityLatNum = new int[5]; // 第i(数组下标)个类的对象的个数
            entityLatNum[0] = 1;
            entityLatNum[1] = list1.size();
            entityLatNum[2] = list2.size();
            entityLatNum[3] = list3.size();
            entityLatNum[4] = list4.size();

            // 求k数的最小公倍数
            int multiple = 1;
            if (sizeList != null) {
                multiple = ComputeLeastCommonMultiple.getLeastCommonMultiple((List<Integer>) sizeList);// 最小公倍数
            }
            int[] latnum = new int[13]; // 每个列所占的单元格的个数
            for (int u = 0; u < 13; u++) { // 每个类的单元格所占的行数 13位列数
                if (sizeArray[u] != 0) {
                    latnum[u] = multiple / sizeArray[u];
                } else {
                    latnum[u] = sizeArray[u];
                }
            }
            // 将0到3扩展到0到6,共7个,每列单元格的格子数
            int[] collatNum = new int[13];
            for (int u = 0; u < 13; u++) {
                collatNum[u] = latnum[u];
            }

            for (int j = 0; j < 13; j++) { // 列数,最小公倍数(0,12)
                if (sizeArray[j] != 0) { // 每列单元格的个数
                    for (int k = 0; k < sizeArray[j]; k++) { // k为合并的格子的索引,第j列的第k个数,collatNum[j]第j列每个单元格的个数
                        Integer startRow = startRowForMA + collatNum[j] * (k);
                        Integer endRow = startRowForMA + collatNum[j] * (k + 1) - 1;
                        Integer startCol = j;
                        Integer endCol = j;
                        if (!(startRow.equals(endRow) && startCol.equals(endCol))) { // 不为空则合并
                            CellRangeAddress region1 = new CellRangeAddress(startRow, endRow, startCol, endCol);
                            sheet.addMergedRegion(region1);
                        }

                    }
                }

                // sizeArray[j]为零说明,第j类所属的对象的大小为0,前面未做任何处理
                if (sizeArray[0] != 0 && sizeArray[j] == 0) { // 第一个list不为空,其它类的list为空情况
                    Integer startRow = startRowForMA;
                    Integer endRow = startRowForMA + multiple - 1;
                    Integer startCol = j;
                    Integer endCol = j;
                    if (!(startRow.equals(endRow) && startCol.equals(endCol))) { // 不为空则合并
                        CellRangeAddress region1 = new CellRangeAddress(startRow, endRow, startCol, endCol);
                        sheet.addMergedRegion(region1);
                    }
                }
            }

            // 赋值
            // latnum[u] 每个合并后的单元格所占的行数
            // 对每个事件的单元格赋值
            Entity0 en0 = list0.get(i);
            HSSFRow r0 = sheet.getRow(startRowForMA);
            if (r0 == null) {
                r0 = sheet.createRow(startRowForMA);
            }
            r0.createCell(0).setCellValue(en0.getValue00());
            r0.createCell(1).setCellValue(en0.getValue01());
            r0.createCell(2).setCellValue(en0.getValue02());
            r0.createCell(3).setCellValue(en0.getValue03());
            int startRow1 = startRowForMA;
            int startRow2 = startRowForMA;
            int startRow3 = startRowForMA;
            int startRow4 = startRowForMA;
            if (list1.size() > 0) {
                for (Entity1 en1 : list1) {
                    HSSFRow r1 = sheet.getRow(startRow1);
                    if (r1 == null) {
                        r1 = sheet.createRow(startRow1);

                    }
                    if (en1.getValue10() != null) {
                        r1.createCell(4).setCellValue(en1.getValue10());
                    }
                    startRow1 = startRow1 + multiple / entityLatNum[1];
                }
            }
            if (list2.size() > 0) {
                for (Entity2 en2 : list2) {
                    HSSFRow r2 = sheet.getRow(startRow2);
                    if (r2 == null) {
                        r2 = sheet.createRow(startRow2);

                    }
                    r2.createCell(5).setCellValue(en2.getValue20());
                    startRow2 = startRow2 + multiple / entityLatNum[2];
                }
            }

            if (list3.size() > 0) {
                for (Entity3 en3 : list3) {
                    HSSFRow r3 = sheet.getRow(startRow3);
                    if (r3 == null) {
                        r3 = sheet.createRow(startRow3);

                    }
                    r3.createCell(6).setCellValue(en3.getValue30());
                    startRow3 = startRow3 + multiple / entityLatNum[3];
                }
            }

            if (list4.size() > 0) {
                for (Entity4 en4 : list4) {
                    HSSFRow r4 = sheet.getRow(startRow4);
                    if (r4 == null) {
                        r4 = sheet.createRow(startRow4);

                    }
                    r4.createCell(7).setCellValue(en4.getValue40());
                    r4.createCell(8).setCellValue(en4.getValue41());
                    r4.createCell(9).setCellValue(en4.getValue42());
                    r4.createCell(10).setCellValue(en4.getValue43());
                    r4.createCell(11).setCellValue(en4.getValue44());
                    r4.createCell(12).setCellValue(en4.getValue45());
                    startRow4 = startRow4 + multiple / entityLatNum[4];
                }
            }

            startRowForMA = startRowForMA + multiple;

        }

        FileOutputStream fileOut;
        try {
            fileOut = new FileOutputStream("D:/testCascade1111235888999.xls");
            wb.write(fileOut);
            System.out.println("qqqq");
            fileOut.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

 

效果如下:

 

注意:

合并单元格需要知道起始行,起始列,终止行,终止列,对单个单元格(起始行,起始列)赋值即可渲染整个合并的单元格。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值