上一篇关于XSSFPivotTable的相关源码

本文探讨了XSSFPivotTable的内部工作原理,通过 utils 类的详细分析,特别是MyBase类如何根据列数量动态获取列号的功能。同时,文章也介绍了与之配合的数据实体类entity的相关内容。
摘要由CSDN通过智能技术生成

utils类:


import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.AreaReference;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFPivotTable;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.*;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.*;

import static org.apache.poi.ss.SpreadsheetVersion.EXCEL2007;

public class ExcelUtils {


    /**
     * 将数据保存到EXCEL文件并生成数据透视表
     * <remark>
     * 需要使用实体类来装载数据,
     * 因为是使用反射获取所有方法后使用get方法来取到数据值的,
     * 且使用属性上的@TitltName标签来获取该字段对应的表格标题的
     * </remark>
     *
     * @param dataList 数据。
     * @param type     类型 1 xls 2 xlsx
     * @return
     * @throws Exception
     */
    public static Workbook toExcelMergeCell(List<?> dataList, int type) throws Exception {
        Workbook workbook;
        CellStyle textStyle;
        DataFormat format;
        if (type == 1) {
            workbook = new XSSFWorkbook();
        } else if (type == 2) {
            workbook = new SXSSFWorkbook();
        } else {
            workbook = new HSSFWorkbook();
        }
        textStyle = workbook.createCellStyle();
        format = workbook.createDataFormat();
        textStyle.setDataFormat(format.getFormat("@"));
        List<Method> methodList = null;//用来遍历object的get方法
        Sheet sheet = workbook.createSheet("数据列表");
        //sheet.setColumnWidth(2, 50);
        int index = sheet.getPhysicalNumberOfRows();

        //迭代保存数据,暂未合并单元格
        for (int i = 0; i < dataList.size(); i++) {
            Object object = dataList.get(i);
            if (methodList == null) {
                Method[] methods = object.getClass().getMethods();
                methodList = new ArrayList<>();
                Row rowHead = sheet.createRow(index);
                int c = 0;
                Field[] fields = object.getClass().getDeclaredFields();
                for (Field field : fields) {

                    for (int m = 0; m < methods.length; m++) {
                        if (methods[m].getName().toLowerCase().equals("get" + field.getName().toLowerCase())) {
                            methodList.add(methods[m]);
                            Cell cell = rowHead.createCell(c);
                            setCellValue(cell, field.getAnnotation(TitleName.class).value());
                            c++;
                        }
                    }
                }
                rowHead.createCell(methodList.size()).setCellValue("行号");
            }

            Row row = sheet.createRow(index + 1);
            row.createCell(methodList.size()).setCellValue(i + 1);//设置序号

            for (int m = 0; m < methodList.size(); m++) {//保存列数据
                Object value = methodList.get(m).invoke(object);
                Cell cell = row.createCell(m);
                cell.setCellStyle(textStyle);
                Object textValue = getValue(value);
                setCellValue(cell, textValue);
            }
            index++;
        }

        System.out.println("save data finish!");
        System.out.println("保存为数据透视表 start");
        workbook = toPivotTabl
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值