POI导出工具类,可导出模板,也可导出数据.

package com.example.demo.exportExcel;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;
import java.util.*;


/**
 * Copyright © 2018 Shanghai 
 *
 * @Description Excel 导入工具辅助类
 * @Author zhaoweijiang
 * @Date 2019/3/29 11:11
 *
 * Excel_2003 支持最大导出65536行
 * Excel_2007 支持最大导出1048576行
 * 详情查看SpreadsheetVersion.class
 *
 */
public class ExportExcelUtil {

    //注释换行符
    public static final String NEWLINE = "\r\n";
    //工作簿
    private Workbook wb;
    //工作表
    private Sheet sheet;
    //样式
    private CellStyle cellStyle;
    //工作簿类型 .xls/.xlsx
    private String extension;
    /**
     * 默认模板工作表名称
     */
    private String sheetName = "";

    /*
     * 默认模板名称字体大小为36
     */
    private int typeFontSize = 36;
    /*
     * 默认模板注释字体大小为11
     */
    private int annotationFontSize = 11;
    /*
     * 默认模板标题字体大小为11
     */
    private int titleFontSize = 11;
    /*
     * 默认模板示例字体大小为11
     */
    private int exampleFontSize = 11;

    /**
     * 对外提供set方法,可修改默认字体大小
     */
    public void setSize(int typeFontSize) {
        this.typeFontSize = typeFontSize;
    }

    public void setAnnotationFontSize(int annotationFontSize) {
        this.annotationFontSize = annotationFontSize;
    }

    public void setTitleFontSize(int titleFontSize) {
        this.titleFontSize = titleFontSize;
    }

    public void setSheetName(String sheetName) {
        this.sheetName = sheetName;
    }

    /**
     * 构造函数初始化
     * ExcelTypeTl 自定义的一个枚举类,该枚举类是工作簿格式 .xls/.xlsx
     */
    public ExportExcelUtil(ExcelTypeTl excelTypeTl){

        String type = excelTypeTl.getType();

        if (".xls".equals(type)){
            this.wb = new HSSFWorkbook();
        }else if (".xlsx".equals(type)){
            this.wb = new XSSFWorkbook();
        }else {
            throw new IllegalArgumentException("Illegal parameter type");
        }
        //传递给私有属性,下面的其他方法还需要
        this.extension = type;
    }

    /**
     * 创建工作表
     * 创建样式
     * 注:表名称不可用特殊符号
     */
    private void createSheet(){
        this.sheet = wb.createSheet(this.sheetName.length() > 0 ? sheetName : "sheet1");
        this.cellStyle = wb.createCellStyle();

    }

    /**
     * 说明: excel的字体大小 * 20 = 设置值  例; excel 字体是 13 * 20 = 260
     *  你需要设置字体大小为260.导出的excel才会是13的字体大小 ,,,同行高,具体源码里有
     *
     * row.setHeight( (short)( 20 * Math.round( (this.typeFontSize/0.69) ) )); //设置行高
     * 我设置的行高,根据字体大小自己调试的我认为最佳行高
     *
     * 创建Excel模板名称   / 第一行
     * @param titleLenght 标题长度
     * @param description 模板名称 同工作簿名称
     */
    private void createTlType(int titleLenght , String description){

        CellRangeAddress region &
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值