首页 > 其他分享 >HExcel,一个简单通用的导入导出Excel工具类

HExcel,一个简单通用的导入导出Excel工具类

时间:2023-10-30 11:33:20浏览次数:44  
标签:sheet title HExcel Excel 表头 导入 key put

  前言

  日常开发中,Excel的导出、导入可以说是最常见的功能模块之一,一个通用的、健壮的的工具类可以节省大量开发时间,让我们把更多精力放在业务处理上中

  之前我们也写了一个Excel的简单导出,甚至可以不依赖poi,还扩展了纯前端导出Excel!详情请戳:《POI导出Excel 》,遗憾的是这些导出并不支持复杂表头

  HExcel,一个简单通用的导入导出Excel工具类
    1、支持导出复杂表头(支持表头单元格水平合并、垂直合并,支持表头单元格个性化样式)
    2、支持导入读取sheet数据(只需要提供title与key的关系,不需要管列的顺序)

  代码思路都在代码注释里,感兴趣的自己看注释

 

  PS:依赖 poi 以及 hutool

<!-- POI -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>5.2.3</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>5.2.3</version>
</dependency>
<!-- hutool -->
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.7.4</version>
</dependency>

 

  先睹为快

   表头目前支持以下属性,可自行扩展:

title  标题
key  key
width  宽度
align 对齐方式
background-color  背景颜色(POI的IndexedColors)
color  字体颜色(POI的IndexedColors)
children  子级表头

 

  导出

  代码

//获取HExcel实例
HExcel hExcel1 = HExcel.newInstance();

//数据,一般是查数据库,经过数据处理生成
List<Map<String, Object>> dataList = new ArrayList<>();
HashMap<String, Object> date1 = new HashMap<>();
date1.put("user_name","张三");
date1.put("sex","男");
date1.put("age",20);
date1.put("yu_wen",90);
date1.put("ying_yu",0);
date1.put("shu_xue",85);
date1.put("wu_li",80);
date1.put("total",255);
dataList.add(date1);

HashMap<String, Object> date2 = new HashMap<>();
date2.put("user_name","李四");
date2.put("sex","女");
date2.put("age",18);
date2.put("yu_wen",81);
date2.put("ying_yu",0);
date2.put("shu_xue",90);
date2.put("wu_li",70);
date2.put("total",241);
dataList.add(date2);


//如果是固定表头数据,可以在项目资源文件夹下面新建个json文件夹,用来保存表头json数据,方便读、写
//JSONArray header = JSONUtil.parseArray(ResourceUtil.readUtf8Str("json/header.json"));

//如果是动态表头数据,直接把json字符串写在代码里,方便动态生成表头数据

//表头
String sheetName = "学生成绩单";
JSONArray headers = JSONUtil.parseArray("" +
        "[\n" +
        "    {\n" +
        "        \"title\":\""+sheetName+"\",\n" +
        "        \"children\":[\n" +
        "            {\n" +
        "                \"title\":\"日期:"+DateUtil.today()+"\",\n" +
        "                \"align\":\"right\",\n" +
        "                \"children\":[\n" +
        "                    {\n" +
        "                        \"title\":\"姓名\",\n" +
        "                        \"key\":\"user_name\",\n" +
        "                    },\n" +
        "                    {\n" +
        "                        \"title\":\"语文\",\n" +
        "                        \"key\":\"yu_wen\",\n" +
        "                    },\n" +
        "                    {\n" +
        "                        \"title\":\"数学\",\n" +
        "                        \"key\":\"shu_xue\",\n" +
        "                    },\n" +
        "                    {\n" +
        "                        \"title\":\"总分\",\n" +
        "                        \"key\":\"total\",\n" +
        "                        \"background-color\":17,\n" +
        "                        \"color\":10,\n" +
        "                        \"width\":30,\n" +
        "                    },\n" +
        "                ]\n" +
        "            },\n" +
        "        ]\n" +
        "    },\n" +
        "]" +
        "");
//生成sheet
hExcel1.buildSheet(sheetName, headers, dataList);


//保存成File文件
hExcel1.toFile("C:\\Users\\XFT User\\Desktop\\学生成绩单复杂表头导出测试.xls");

//关闭对象
hExcel1.close();

  效果

 

  导入

  需要导入的Excel文件

   代码

//需要设置title与key的关系
JSONObject headerTitleKey = new JSONObject("" +
        "{\n" +
        "    \"姓名\":\"user_name\",\n" +
        "    \"语文\":\"yu_wen\",\n" +
        "    \"数学\":\"shu_xue\",\n" +
        "    \"总分\":\"total\",\n" +
        "}" +
        "");

//根据Excel文件,获取HExcel实例
HExcel hExcel2 = HExcel.newInstance(new File("C:\\Users\\XFT User\\Desktop\\学生成绩单复杂表头导出测试.xls"));

//根据title-key关系,读取指定位置的sheet数据
List<Map<String, Object>> sheetList = hExcel2.readSheet(2, 3, headerTitleKey);

//打印sheetList数据
for (Map<String, Object> map : sheetList) {
    System.out.println(map.toString());
}

//关闭对象
hExcel2.close();

  效果

  {user_name=张三, yu_wen=90, shu_xue=85, total=255}
  {user_name=李四, yu_wen=81, shu_xue=90, total=241}

 

  完整代码

package cn.huanzi.qch.util;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.*;

/**
 * HExcel,一个简单通用的导入导出Excel工具类
 * 1、支持复杂表头导出(支持表头单元格水平合并、垂直合并,支持表头单元格个性化样式)
 * 2、支持导入读取sheet数据(只需要提供title与key的关系,不需要管列的顺序)
 *
 * PS:依赖 poi 以及 hutool
 *
 * 详情请戳:https://www.cnblogs.com/huanzi-qch/p/17797355.html
 */
public class HExcel {

    /**
     * 获取一个HExcel实例,并初始化空Workbook对象
     */
    public static HExcel newInstance(){
        HExcel hExcelUtil = new HExcel();
        hExcelUtil.hSSFWorkbook = new HSSFWorkbook();
        return hExcelUtil;
    }

    /**
     * 获取一个HExcel实例,并根据excelFile初始化Workbook对象
     */
    public static HExcel newInstance(File excelFile){
        HExcel hExcelUtil = new HExcel();
        try {
            hExcelUtil.hSSFWorkbook = new HSSFWorkbook(new FileInputStream(excelFile));
        } catch (IOException e) {
            throw new RuntimeException("【HExcel】 根据excelFile初始化Workbook对象异常",e);
        }
        return hExcelUtil;
    }

    /**
     * 导入并读取Excel
     *
     * @param sheetIndex 需要读取的sheet下标
     * @param firstDataRow 数据起始行
     * @param headerTitleKey title与key的关系json对象
     * @return 返回数据集合
     */
    public List<Map<String, Object>> readSheet(int sheetIndex,int firstDataRow,JSONObject headerTitleKey){
        //最终返回的数据集合
        ArrayList<Map<String, Object>> list = new ArrayList<>();

        //获取sheet
        HSSFSheet sheet = this.hSSFWorkbook.getSheetAt(sheetIndex);

        //获取title与col的对应关系
        HashMap<Integer, String> headerMap = new HashMap<>();
        int lastCellNum = sheet.getRow(0).getLastCellNum();
        for (int i = 0; i < lastCellNum; i++) {
            for (int j = firstDataRow-1; j >=0 ; j--) {
                HSSFCell cell = sheet.getRow(j).getCell(i);
                if(cell != null && !"".equals(cell.getStringCellValue())){
                    String title = cell.getStringCellValue();
                    headerMap.put(i,title);
                    break;
                }
            }
        }

        //获取数据
        for (int i = firstDataRow; i <= sheet.getLastRowNum(); i++) {
            HSSFRow row = sheet.getRow(i);
            LinkedHashMap<String, Object> dateMap = new LinkedHashMap<>();
            for (int j = 0; j < lastCellNum; j++) {
                String title = headerMap.get(j);
                String key = headerTitleKey.getStr(title);

                if(key != null && !"".equals(key)){
                    String value = row.getCell(j).getStringCellValue();
                    dateMap.put(key,value);
                }
            }
            list.add(dateMap);
        }



        return list;
    }

    /**
     * 构造一个sheet,以及生成复杂表头、表数据
     *
     * @param sheetName sheet名称
     * @param headers 复杂表头json数组对象
     * @param dataLists 表数据集合
     * @return HExcel
     */
    public HExcel buildSheet(String sheetName, JSONArray headers, List<Map<String, Object>> dataLists) {
        //建立新的sheet对象
        HSSFSheet sheet = this.hSSFWorkbook.createSheet(sheetName);//设置表单名

        //生成复杂表头
        int row = 0;//当前行
        int col = 0;//当前列
        HashMap<String, Object> hashMap = createHeader(sheet,row,col,headers);
        ArrayList<String> headerList = (ArrayList<String>) hashMap.get("keyList");
        row = (int) hashMap.get("maxRow");

        //取出水平合并区域数据
        List<CellRangeAddress> cellRangeAddressList = sheet.getMergedRegions();
        //垂直合并,单元格为空,且不属于水平合并区域
        //这里row-1是因为,生成所有表头结束后,maxRow比最大行+1,
        for (int i = 0; i < headerList.size(); i++) {
            for (int j = 0; j <= row-1; j++) {
                boolean flag = true;

                //单元格不为空
                HSSFCell cell = sheet.getRow(j).getCell(i);
                if(cell != null){
                    continue;
                }
                //检查合并区域
                for (CellRangeAddress cellAddresses : cellRangeAddressList) {
                    int OldFirstRow = cellAddresses.getFirstRow();
                    int OldLastRow = cellAddresses.getLastRow();
                    int OldFirstCol = cellAddresses.getFirstColumn();
                    int OldLastCol = cellAddresses.getLastColumn();

                    //与合并区域重叠
                    if ((OldFirstRow >= j && OldLastRow <= j) && (OldFirstCol >= i && OldLastCol <= i)) {
                        flag = false;
                        break;
                    }
                }

                //满足条件,将上一个单元格与最后一个单元格合并
                if(flag){
                    mergedCell(sheet,j-1,row-1,i,i);
                    break;
                }
            }
        }

        //开始填充数据
        HSSFCellStyle dataStyle = createDataStyle(sheet);
        for (Map<String, Object> map : dataLists) {
            //创建内容行
            HSSFRow dataHSSFRow = sheet.createRow(row);
            for (int i = 0; i < headerList.size(); i++) {
                String key = headerList.get(i);
                Object val = map.get(key);
                createCell(dataHSSFRow, i, dataStyle, val == null ? "" : String.valueOf(val));
            }
            row++;
        }

        return this;
    }

    /**
     * 保存成File文件
     *
     * @param path 完整文件路径+文件名
     */
    public void toFile(String path) {
        //try-catch语法糖
        try (FileOutputStream out = new FileOutputStream(path);){
            this.hSSFWorkbook.write(out);
        }catch (IOException e){
            throw new RuntimeException("【HExcel】 Workbook对象文件流写入File异常",e);
        }
    }

    /**
     * 保存到HttpServletResponse
     *
     * @param fileName 文件名
     * @param response HttpServletResponse对象
     */
    public void toHttpServletResponse(String fileName, HttpServletResponse response) {
        //try-catch语法糖
        try (ServletOutputStream outputStream = response.getOutputStream();){
            response.setHeader("Accept-Ranges", "bytes");
            response.setHeader("Content-disposition", "attachment; filename=\"" + URLEncoder.encode(fileName, "UTF-8") + "\"");
            response.setContentType("application/octet-stream");
            this.hSSFWorkbook.write(outputStream);
        }catch (Exception e){
            throw new RuntimeException("【HExcel】 Workbook对象文件流写入Response异常",e);
        }
    }

    /**
     * 关闭Workbook
     */
    public void close(){
        try{
            //关闭Workbook
            this.hSSFWorkbook.close();
        } catch (Exception e) {
            throw new RuntimeException("【HExcel】 关闭Workbook异常",e);
        }
    }


    /*          已下设置私有,对外隐藏实现细节           */

    /**
     * Workbook对象
     */
    private HSSFWorkbook hSSFWorkbook;

    /**
     * 构造表头
     *
     * @param sheet sheet
     * @param row 当前操作行
     * @param col 当前操作列
     * @param headers 表头数据
     * @return 返回一个map对象,供上级表头获取最新当前操作行、列、key集合
     */
    private HashMap<String,Object> createHeader(HSSFSheet sheet, int row, int col, JSONArray headers){
        //最终返回对象
        HashMap<String, Object> hashMap = new HashMap<>();

        //key集合
        ArrayList<String> keyList = new ArrayList<>();

        HSSFWorkbook wb = sheet.getWorkbook();
        HSSFRow headerHSSFRow = sheet.getRow(row);
        if(headerHSSFRow == null){
            headerHSSFRow = sheet.createRow(row);
        }
        for (Object object : headers) {
            JSONObject header = (JSONObject) object;
            String title = (String) header.get("title");
            String key = (String) header.get("key");
            Object width = header.get("width");
            Object align = header.get("align");
            Object backgroundColor = header.get("background-color");
            Object color = header.get("color");
            Object children = header.get("children");

            //单元格样式
            HSSFCellStyle headerStyle = createHeaderStyle(sheet);

            //自定义单元格背景色
            if(backgroundColor != null){
                headerStyle.setFillForegroundColor(Short.parseShort(backgroundColor+""));
            }

            //自定义单元格字体颜色
            if(color != null){
                headerStyle.getFont(wb).setColor(Short.parseShort(color+""));
            }

            //默认单元格宽度,20
            sheet.setColumnWidth(col, 20 * 256);
            if(width != null){
                //自定义单元格宽度
                sheet.setColumnWidth(col, (int) width * 256);
            }

            //默认水平对齐方式(水平居中)
            if(align != null){
                //自定义水平对齐方式
                HorizontalAlignment alignment;
                switch (String.valueOf(align).toUpperCase()){
                    case "LEFT":
                        alignment = HorizontalAlignment.LEFT;
                        break;
                    case "RIGHT":
                        alignment = HorizontalAlignment.RIGHT;
                        break;
                    default:
                        alignment = HorizontalAlignment.CENTER;
                        break;
                }
                headerStyle.setAlignment(alignment);
            }

            //System.out.println(title + " " + key + " " + row + " " + col);

            //生成单元格同时设置内容
            createCell(headerHSSFRow, col, headerStyle, title);

            //无子级表头
            if(children == null){
                //保留顺序,方便后面设置数据
                keyList.add(key);

                //当前列+1
                col++;
            }
            //有子级表头
            else{
                //递归生成子级表头前,保存父级表头col,用于水平合并
                int firstCol = col;

                //递归调用
                HashMap<String, Object> hashMap1 = createHeader(sheet, row + 1, col, (JSONArray) children);

                //获取最新col、key集合
                col = (int) hashMap1.get("col");
                hashMap.put("maxRow",hashMap1.get("maxRow"));
                keyList.addAll((ArrayList<String>) hashMap1.get("keyList"));

                //水平合并,这里col-1是因为,生成子级表头结束后,col比最后一个下级表头+1,
                if(!(firstCol == col-1)){
                    mergedCell(sheet,row,row,firstCol,col-1);
                }
            }
        }

        //将数据设置到对象中,返回上一层
        hashMap.put("maxRow",(hashMap.get("maxRow") != null ? Integer.parseInt(hashMap.get("maxRow")+"") : 0) + 1);//最大行
        hashMap.put("row",row);//当前操作行
        hashMap.put("col",col);//当前操作列
        hashMap.put("keyList",keyList);//key集合

        return hashMap;
    }

    /**
     * 创建一个单元格
     *
     * @param hSSFRow 当前行对象
     * @param col 当前列
     * @param cellStyle 单元格样式对象
     * @param text 单元格内容,目前只支持字符串,如需支持更多格式可自行扩展
     */
    private void createCell(HSSFRow hSSFRow, int col, HSSFCellStyle cellStyle, String text) {
        HSSFCell cell = hSSFRow.createCell(col);  // 创建单元格
        cell.setCellStyle(cellStyle); // 设置单元格样式
        cell.setCellValue(text);  // 设置值
    }

    /**
     * 构造表头、数据样式
     *
     * @param sheet sheet
     * @return 返回一个单元格样式对象
     */
    private HSSFCellStyle createHeaderStyle(HSSFSheet sheet){
        HSSFWorkbook wb = sheet.getWorkbook();

        //表头的样式
        HSSFCellStyle headerStyle = wb.createCellStyle();
        headerStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中
        headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
        //列名的字体
        HSSFFont dataFont = wb.createFont();
        dataFont.setFontHeightInPoints((short) 12);
        dataFont.setFontName("新宋体");
        headerStyle.setFont(dataFont);// 把字体 应用到当前样式
        headerStyle.setWrapText(true);//自动换行
        //填充样式,前景色、天空蓝
        headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        headerStyle.setFillForegroundColor(IndexedColors.PALE_BLUE.getIndex());

        // 设置边框
        headerStyle.setBorderBottom(BorderStyle.THIN);
        headerStyle.setBorderLeft(BorderStyle.THIN);
        headerStyle.setBorderRight(BorderStyle.THIN);
        headerStyle.setBorderTop(BorderStyle.THIN);

        return headerStyle;
    }
    private HSSFCellStyle createDataStyle(HSSFSheet sheet){
        HSSFWorkbook wb = sheet.getWorkbook();

        //内容的样式
        HSSFCellStyle dataStyle = wb.createCellStyle();
        dataStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中
        dataStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
        //内容的字体
        HSSFFont font3 = wb.createFont();
        font3.setFontHeightInPoints((short) 12);
        font3.setFontName("新宋体");
        dataStyle.setFont(font3);// 把字体 应用到当前样式
        dataStyle.setWrapText(true);//自动换行
        //默认无填充
        dataStyle.setFillPattern(FillPatternType.NO_FILL);
        // 设置边框
        dataStyle.setBorderBottom(BorderStyle.THIN);
        dataStyle.setBorderLeft(BorderStyle.THIN);
        dataStyle.setBorderRight(BorderStyle.THIN);
        dataStyle.setBorderTop(BorderStyle.THIN);

        return dataStyle;
    }

    /**
     * 合并单元格
     *
     * @param sheet sheet
     * @param firstRow 起始行
     * @param lastRow 结束行
     * @param firstCol 起始列
     * @param lastCol 结束列
     */
    private void mergedCell(HSSFSheet sheet,int firstRow, int lastRow, int firstCol, int lastCol){
        //一个单元格无需合并,例如:[0,0,0,0]
        if(firstRow == lastRow && firstCol == lastCol){
            return;
        }

        //先取出合并前的单元格样式
        HSSFCellStyle cellStyle = sheet.getRow(firstRow).getCell(firstCol).getCellStyle();

        //合并
        sheet.addMergedRegion(new CellRangeAddress(firstRow, lastRow, firstCol, lastCol));

        //解决合并后的边框等样式问题
        int first;
        int end;
        //垂直合并
        if(firstCol == lastCol){
            first = firstRow;
            end = lastRow+1;

            for (int i = first; i < end; i++) {
                HSSFRow row = sheet.getRow(i);
                if(row == null){
                    row = sheet.createRow(i);
                }
                HSSFCell cell = row.getCell(firstCol);
                if(cell == null){
                    cell = row.createCell(firstCol);
                }
                cell.setCellStyle(cellStyle);
            }
        }
        //水平合并
        else{
            first = firstCol;
            end = lastCol+1;

            for (int i = first; i < end; i++) {
                HSSFRow row = sheet.getRow(firstRow);
                if(row == null){
                    row = sheet.createRow(firstRow);
                }
                HSSFCell cell = row.getCell(i);
                if(cell == null){
                    cell = row.createCell(i);
                }
                cell.setCellStyle(cellStyle);
            }
        }
    }

}
View Code

 

   完整main测试

 

public static void main(String[] args) {
    //获取HExcel实例
    HExcel hExcel1 = HExcel.newInstance();

    //数据,一般是查数据库,经过数据处理生成
    List<Map<String, Object>> dataList = new ArrayList<>();
    HashMap<String, Object> date1 = new HashMap<>();
    date1.put("user_name","张三");
    date1.put("sex","男");
    date1.put("age",20);
    date1.put("yu_wen",90);
    date1.put("ying_yu",0);
    date1.put("shu_xue",85);
    date1.put("wu_li",80);
    date1.put("total",255);
    dataList.add(date1);

    HashMap<String, Object> date2 = new HashMap<>();
    date2.put("user_name","李四");
    date2.put("sex","女");
    date2.put("age",18);
    date2.put("yu_wen",81);
    date2.put("ying_yu",0);
    date2.put("shu_xue",90);
    date2.put("wu_li",70);
    date2.put("total",241);
    dataList.add(date2);



    //如果是固定表头数据,可以在项目资源文件夹下面新建个json文件夹,用来保存表头json数据,方便读、写
    //JSONArray header = JSONUtil.parseArray(ResourceUtil.readUtf8Str("json/header.json"));

    //如果是动态表头数据,直接把json字符串写在代码里,方便动态生成表头数据

    //表头
    String sheetName = "学生成绩单";
    JSONArray headers = JSONUtil.parseArray("" +
            "[\n" +
            "    {\n" +
            "        \"title\":\""+sheetName+"\",\n" +
            "        \"children\":[\n" +
            "            {\n" +
            "                \"title\":\"日期:"+DateUtil.today()+"\",\n" +
            "                \"align\":\"right\",\n" +
            "                \"children\":[\n" +
            "                    {\n" +
            "                        \"title\":\"姓名\",\n" +
            "                        \"key\":\"user_name\",\n" +
            "                    },\n" +
            "                    {\n" +
            "                        \"title\":\"语文\",\n" +
            "                        \"key\":\"yu_wen\",\n" +
            "                    },\n" +
            "                    {\n" +
            "                        \"title\":\"数学\",\n" +
            "                        \"key\":\"shu_xue\",\n" +
            "                    },\n" +
            "                    {\n" +
            "                        \"title\":\"总分\",\n" +
            "                        \"key\":\"total\",\n" +
            "                        \"background-color\":17,\n" +
            "                        \"color\":10,\n" +
            "                        \"width\":30,\n" +
            "                    },\n" +
            "                ]\n" +
            "            },\n" +
            "        ]\n" +
            "    },\n" +
            "]" +
            "");
    //生成sheet
    hExcel1.buildSheet(sheetName, headers, dataList);

    //表头
    JSONArray headers2 = JSONUtil.parseArray("" +
            "[\n" +
            "    {\n" +
            "        \"title\":\"姓名\",\n" +
            "        \"key\":\"user_name\",\n" +
            "    },\n" +
            "    {\n" +
            "        \"title\":\"学科成绩\",\n" +
            "        \"children\":[\n" +
            "            {\n" +
            "                \"title\":\"语文\",\n" +
            "                \"key\":\"yu_wen\",\n" +
            "            },\n" +
            "            {\n" +
            "                \"title\":\"数学\",\n" +
            "                \"key\":\"shu_xue\",\n" +
            "            },\n" +
            "        ]\n" +
            "    },\n" +
            "    {\n" +
            "        \"title\":\"总分\",\n" +
            "        \"key\":\"total\",\n" +
            "        \"align\":\"right\",\n" +
            "        \"background-color\":17,\n" +
            "        \"color\":10,\n" +
            "        \"width\":30\n," +
            "    },\n" +
            "]" +
            "");
    //生成sheet
    hExcel1.buildSheet("学生成绩单2", headers2, dataList);

    //表头
    JSONArray headers3 = JSONUtil.parseArray("" +
            "[\n" +
            "    {\n" +
            "        \"title\":\"姓名\",\n" +
            "        \"key\":\"user_name\"\n" +
            "    },\n" +
            "    {\n" +
            "        \"title\":\"性别\",\n" +
            "        \"key\":\"sex\"\n" +
            "    },\n" +
            "    {\n" +
            "        \"title\":\"年龄\",\n" +
            "        \"key\":\"age\"\n" +
            "    },\n" +
            "    {\n" +
            "        \"title\":\"学科成绩\",\n" +
            "        \"children\":[\n" +
            "            {\n" +
            "                \"title\":\"语言类\",\n" +
            "                \"children\":[\n" +
            "                    {\n" +
            "                        \"title\":\"语文\",\n" +
            "                        \"key\":\"yu_wen\",\n" +
            "                        \"background-color\":7,\n" +
            "                        \"color\":5,\n" +
            "                    },\n" +
            "                  ]\n" +
            "            },\n" +
            "            {\n" +
            "                \"title\":\"科学类\",\n" +
            "                \"background-color\":10,\n" +
            "                \"children\":[\n" +
            "                    {\n" +
            "                        \"title\":\"数学\",\n" +
            "                        \"key\":\"shu_xue\"\n" +
            "                    },\n" +
            "                    {\n" +
            "                        \"title\":\"物理\",\n" +
            "                        \"key\":\"wu_li\"\n" +
            "                    }\n" +
            "                 ]\n" +
            "            },\n" +
            "        ]\n" +
            "    },\n" +
            "    {\n" +
            "        \"title\":\"总分\",\n" +
            "        \"key\":\"total\",\n" +
            "        \"align\":\"right\",\n" +
            "        \"background-color\":17,\n" +
            "        \"color\":10,\n" +
            "        \"width\":30\n," +
            "    },\n" +
            "]"+
            "");
    //生成sheet
    hExcel1.buildSheet("学生成绩单3", headers3, dataList);

    //表头
    JSONArray headers4 = JSONUtil.parseArray("" +
            "[\n" +
            "    {\n" +
            "        \"title\":\"姓名\",\n" +
            "        \"key\":\"user_name\"\n" +
            "    },\n" +
            "    {\n" +
            "        \"title\":\"性别\",\n" +
            "        \"key\":\"sex\"\n" +
            "    },\n" +
            "    {\n" +
            "        \"title\":\"年龄\",\n" +
            "        \"key\":\"age\"\n" +
            "    },\n" +
            "    {\n" +
            "        \"title\":\"学科成绩\",\n" +
            "        \"children\":[\n" +
            "            {\n" +
            "                \"title\":\"语文\",\n" +
            "                \"key\":\"yu_wen\",\n" +
            "            },\n" +
            "            {\n" +
            "                \"title\":\"科学类\",\n" +
            "                \"background-color\":10,\n" +
            "                \"children\":[\n" +
            "                    {\n" +
            "                        \"title\":\"数学\",\n" +
            "                        \"key\":\"shu_xue\"\n" +
            "                    },\n" +
            "                    {\n" +
            "                        \"title\":\"物理\",\n" +
            "                        \"key\":\"wu_li\"\n" +
            "                    }\n" +
            "                 ]\n" +
            "            },\n" +
            "            {\n" +
            "                \"title\":\"英语\",\n" +
            "                \"key\":\"ying_yu\",\n" +
            "            },\n" +
            "        ]\n" +
            "    },\n" +
            "    {\n" +
            "        \"title\":\"总分\",\n" +
            "        \"key\":\"total\",\n" +
            "        \"align\":\"right\",\n" +
            "        \"background-color\":17,\n" +
            "        \"color\":10,\n" +
            "        \"width\":30\n" +
            "      \n" +
            "    }\n" +
            "]"+
            "");
    //生成sheet
    hExcel1.buildSheet("学生成绩单4", headers4, dataList);

    //保存成File文件
    hExcel1.toFile("C:\\Users\\XFT User\\Desktop\\学生成绩单复杂表头导出测试.xls");
    System.out.println("导出完成!\n");

    //关闭对象
    hExcel1.close();

    //导入

    //需要设置title与key的关系
    JSONObject headerTitleKey = new JSONObject("" +
            "{\n" +
            "    \"姓名\":\"user_name\",\n" +
            "    \"语文\":\"yu_wen\",\n" +
            "    \"数学\":\"shu_xue\",\n" +
            "    \"总分\":\"total\",\n" +
            "}" +
            "");

    //根据Excel文件,获取HExcel实例
    HExcel hExcel2 = HExcel.newInstance(new File("C:\\Users\\XFT User\\Desktop\\学生成绩单复杂表头导出测试.xls"));

    //根据title-key关系,读取指定位置的sheet数据
    List<Map<String, Object>> sheetList = hExcel2.readSheet(2, 3, headerTitleKey);

    //打印sheetList数据
    System.out.println("导入完成!");
    for (Map<String, Object> map : sheetList) {
        System.out.println(map.toString());
    }

    //关闭对象
    hExcel2.close();

}

 

  后记

  一个简单通用的导入导出Excel工具类暂时先记录到这,后续再进行补充

 

标签:sheet,title,HExcel,Excel,表头,导入,key,put
From: https://www.cnblogs.com/huanzi-qch/p/17797355.html

相关文章

  • 工作常用的EXCEL公式 | 某列相同的数据进行汇总
    需求:解决方法:1、对部门排序,再进行分类汇总(数据-分类汇总) 2、选中A列,CTRL+G,快速定位空值,然后点击合并单元格3、选中A列,然后点击格式刷,刷在B列 4、取消分类汇总 5、删除A列,然后筛选B列为0的数据,然后删除,即可。 (调整一下格式即可) ......
  • C#学习笔记之使用Access读取Excel表格
    一、读取Excel表的内容(使用DataSet)1.DataSet定义:表示数据在内存中的缓存。可以理解为,将从Excel表中读取出来的数据存入DataSet类中,之后对DataSet进行数据处理,能提高处理的速度。2.DataSet属性和方法:①属性CaseSensitive:获取或设置一个值,该值指示DataTable中的字符串是否区分......
  • Python 利用pymysql和openpyxl操作MySQL数据库并插入Excel数据
    1.需求分析本文将介绍如何使用Python连接MySQL数据库,并从Excel文件中读取数据,将其插入到MySQL数据库中。2.环境准备在开始本文之前,请确保您已经安装好了以下环境:Python3.xPyMySQL库openpyxl库MySQL数据库3.连接MySQL数据库我们可以使用pymysql库来连接MySQL数据库......
  • Python使用pymysql和xlrd2将Excel数据导入MySQL数据库
    在数据处理和管理中,有时候需要将Excel文件中的数据导入到MySQL数据库中进行进一步的分析和操作。本文将介绍如何使用Python编程语言实现这个过程。导入所需库importxlrd2#导入xlrd2库,用于读取Excel文件importpymysql#导入pymysql库,用于连接和操作MySQL数据库fromdat......
  • Pandas数据导入和导出:CSV、Excel、MySQL、JSON
    导入MySQL查询结果:read_sqlimportpandascon="mysql+pymysql://user:[email protected]/test"sql="SELECT*FROM`student`WHEREid=2"#sql查询df1=pandas.read_sql(sql=sql,con=con)print(df1)导入MySQL整张表:read_sql_table#整张表df2=pandas.rea......
  • 使用 excel 快速拼接省市区镇街村居五级区划完整名称
    你知道的越多,你不知道的越多点赞再看,养成习惯文章目录前言数据准备excel函数附件前言之前做了国家区划的映射关系,在其过程中,使用代码区拼接完整的五级区划,感觉还是比较麻烦的,后面偶然在excel上发现可以使用函数去完成这个拼接操作,记录一下方法。数据准备首先需要准备3个广......
  • Python 模块:创建、导入和使用
    什么是模块?将模块视为代码库。模块是一个包含一组函数的文件,您想要在应用程序中包含这些函数。创建一个模块要创建一个模块,只需将要包含在其中的代码保存在扩展名为.py的文件中:示例:将以下代码保存在名为mymodule.py的文件中:defgreeting(name):print("Hello,"+name)......
  • Python 模块:创建、导入和使用
    什么是模块?将模块视为代码库。模块是一个包含一组函数的文件,您想要在应用程序中包含这些函数。创建一个模块要创建一个模块,只需将要包含在其中的代码保存在扩展名为.py的文件中:示例:将以下代码保存在名为mymodule.py的文件中:defgreeting(name):print("Hello,"+name......
  • navicat导入excel文件
    打开excel文件,将鼠标放在sheet上面,右键,取消隐藏,显示所有表navicat新建库,右键库下面的表按钮,导入向导,选择excel选择excel文件路径,选择要导入的表指定标题行和数据行由于数据库中没有对应的表,选择新建表指定主键和类型选择导入模式点击开始......
  • # yyds干货盘点 # 导入的xls文件,数字和日期都是文本格式,到df3都正常,但df4报错,什么原因
    大家好,我是皮皮。一、前言前几天在Python最强王者交流群【斌】问了一个Pandas数据处理的问题,一起来看看吧。我之前用过xls,现在练习pandas:目前导入的xls文件,数字和日期都是文本格式,到df3都正常,但df4报错,df4是算加权平均。下图是报错截图:二、实现过程这里我和【黑科技·鼓包】、【瑜......