本文介绍如何使用easypoi将数据当如到excel中;
官方文档:http://easypoi.mydoc.io/
里面有介绍easypoi的注解使用等。
poi的依赖

 <!--easypoi的依赖--><dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-base</artifactId><version>3.2.0</version></dependency><dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-web</artifactId><version>3.2.0</version></dependency><dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-annotation</artifactId><version>3.2.0</version></dependency>

本文是将用户名和密码导入到excel中,所以封装一个用户对象

import cn.afterturn.easypoi.excel.annotation.Excel;
import lombok.*;
import lombok.experimental.Accessors;import java.io.Serializable;/*** @program: bootshirojwt* @description:* @author: liu.chuanjiang* @create: 2022-05-11 20:55*/
@Builder
@Data
@EqualsAndHashCode(callSuper = false)
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class UserInfo implements Serializable {private static final long serialVersionUID = 1L;@Excel(name = "用户名", width = 20, orderNum = "1")private String userName;@Excel(name = "密码", width = 20, orderNum = "2")private String password;}

如果需要对excel的样式进行设置,那么就封装一个样式类

import cn.afterturn.easypoi.excel.export.styler.AbstractExcelExportStyler;
import cn.afterturn.easypoi.excel.export.styler.IExcelExportStyler;
import org.apache.poi.ss.usermodel.*;/*** @ClassName: ExcelExportMyStylerImpl* @Description: 自定义报表导出样式,可以修改表头颜色,高度等* @Author: sunt* @Date: 2019/8/29 21:39* @Version 1.0**/
public class ExcelExportMyStyler extends AbstractExcelExportStyler implements IExcelExportStyler {public ExcelExportMyStyler(Workbook workbook) {super.createStyles(workbook);}@Overridepublic CellStyle getTitleStyle(short color) {CellStyle titleStyle = workbook.createCellStyle();Font font = workbook.createFont();font.setBold(true);// 加粗titleStyle.setFont(font);titleStyle.setAlignment(HorizontalAlignment.CENTER);// 居中titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);// 垂直居中titleStyle.setFillForegroundColor(IndexedColors.AQUA.index);// 设置颜色titleStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);titleStyle.setBorderRight(BorderStyle.THIN);titleStyle.setWrapText(true);return titleStyle;}@SuppressWarnings("deprecation")@Overridepublic CellStyle stringSeptailStyle(Workbook workbook, boolean isWarp) {CellStyle style = workbook.createCellStyle();style.setAlignment(CellStyle.ALIGN_CENTER);style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);style.setDataFormat(STRING_FORMAT);if (isWarp) {style.setWrapText(true);}return style;}@Overridepublic CellStyle getHeaderStyle(short color) {CellStyle titleStyle = workbook.createCellStyle();Font font = workbook.createFont();font.setBold(true);// 加粗font.setColor(IndexedColors.RED.index);font.setFontHeightInPoints((short) 11);titleStyle.setFont(font);titleStyle.setAlignment(HorizontalAlignment.CENTER);// 居中titleStyle.setFillForegroundColor(IndexedColors.WHITE.index);// 设置颜色titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);// 垂直居中titleStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);titleStyle.setBorderRight(BorderStyle.THIN);titleStyle.setWrapText(true);return titleStyle;}@SuppressWarnings("deprecation")@Overridepublic CellStyle stringNoneStyle(Workbook workbook, boolean isWarp) {CellStyle style = workbook.createCellStyle();style.setAlignment(CellStyle.ALIGN_CENTER);style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);style.setDataFormat(STRING_FORMAT);if (isWarp) {style.setWrapText(true);}return style;}
}

接下就是excel导出的核心之处了,excel工具类

package com.lcj.easypoi;import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;/*** @ClassName: ExcelExportUtil* @Description: Exceld导出工具类* @Author: sunt* @Date: 2019/8/30 14:49* @Version 1.0**/
@Slf4j
public class MyExcelExportUtil {/*** Excel文件导出,导出的文件名默认为:headTitle+当前系统时间** @param listData  要导出的list数据* @param pojoClass 定义excel属性信息* @param headTitle Excel文件头信息* @param sheetName Excel文件sheet名称* @param response*/public static void exportExcel(Collection<?> listData, Class<?> pojoClass, String headTitle, String sheetName, HttpServletResponse response) {log.info("进入到exportExcel");ExportParams params = new ExportParams(headTitle, sheetName);params.setHeight((short) 8);params.setStyle(ExcelExportMyStyler.class);try {Workbook workbook = ExcelExportUtil.exportExcel(params, pojoClass, listData);String fileName = headTitle + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());fileName = URLEncoder.encode(fileName, "UTF8");response.setCharacterEncoding("UTF-8");response.setContentType("application/vnd.ms-excel;chartset=utf-8");response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");ServletOutputStream out = response.getOutputStream();workbook.write(out);out.flush();out.close();} catch (Exception e) {e.printStackTrace();}}public static void exportExcel1(Collection<?> listData, Class<?> pojoClass, String headTitle, String sheetName) {log.info("进入到exportExcel");FileOutputStream outStream=null;try {String fileName = "C:\\Users\\Administrator.DESKTOP-JMAFGHR\\Desktop\\工作\\test.xlsx";outStream = new FileOutputStream(fileName);ExportParams params = new ExportParams(headTitle, sheetName);params.setHeight((short) 8);params.setStyle(ExcelExportMyStyler.class);Workbook workbook = ExcelExportUtil.exportExcel(params, pojoClass, listData);workbook.write(outStream);} catch (Exception e) {e.printStackTrace();}finally {try {if (outStream != null) {outStream.close();}} catch (IOException e) {e.printStackTrace();}}}
}

测试类

/*** @program: test_pay* @description:* @author: liu.chuanjiang* @create: 2022-05-11 22:19*/
public class test {public static void main(String[] args) {List<UserInfo> list=new ArrayList<>();UserInfo userInfo1 = new UserInfo().setUserName("张三").setPassword("123");list.add(userInfo1);UserInfo userInfo2 = new UserInfo().setUserName("李四").setPassword("456");list.add(userInfo2);MyExcelExportUtil.exportExcel1(list,UserInfo.class,"用户信息","用户号信息");}
}

使用easypoi将数据导入excel中相关推荐

  1. openrowset excel 科学计数_txt的数据导入excel中身份证或银行卡显示成科学计数如何解决...

    用excel自带的"数据"->"自文本"将txt的数据导入excel中,如果数字特别长比如身份证或者银行卡会显示成科学计数法,如下: 借助sqlcel我们 ...

  2. txt的数据导入excel中身份证或银行卡显示成科学计数如何解决

    用excel自带的"数据"->"自文本"将txt的数据导入excel中,如果数字特别长比如身份证或者银行卡会显示成科学计数法,如下: 借助sqlcel我们 ...

  3. easypoi导出数值型_SpringBoot使用EasyPoi进行数据导入导出Excel(一)

    在实际项目开发中,对于Excel的导入导出还是很常见的需求,比如说将数据根据模板批量导入到数据库中,以及将数据库中的数据批量导出陈Excel的形式 现有需求: 下载固定的导入Excel模板 导入Exc ...

  4. python导入excel数据-如何把python中的数据导入excel

    python将数据导入excel的方法:1.在python官网下载xlrd第三方库:2.利用xlrd中的open_workbook函数读入excel文件,即可在python中导入excel数据. 一. ...

  5. matlab打开矩阵表,如何将Excel数据导入MATLAB中?:EXCLE中通过矩阵表输出选项

    怎么把matlab中处理的数据存入到excel中 可以直接xlswrite命令. data=randn(100,1); % data为需要存储的数 xlswrite('D:\write2Excel.x ...

  6. python excel模板 生成excel表格_python制作简单excel统计报表3之将mysql数据库中的数据导入excel模板并生成统计图...

    python制作简单excel统计报表3之将mysql数据库中的数据导入excel模板并生成统计图 #coding=utf-8 from openpyxl importload_workbookfro ...

  7. matlab导入word数据,如何将Excel数据导入MATLAB中?/excel数据导入word模板

    如何将Excel数据导入MATLAB中? 从excel中导入,可以用xlsread()函数. 例如: A=xlsread('C:\Users\Administrator\Desktop\07-29预. ...

  8. 如何将excel表格导入matlab,将Excel数据导入MATLAB中的方法

    在使用MATLAB对矩阵进行数据处理时,为了方便编辑与修改,常常需要先将数据录入到Excel中,然后再将其导入到MATLAB中参与矩阵运算.那么下面小编教你怎么将Excel数据导入MATLAB中. 将 ...

  9. 将一个Excel中的数据导入DataGrid中

    第一步: 获取计算机中的一个Excel文件利用File Field控件和Javascrip脚本 File Field控件ID:DocUpload <INPUT id="DocUploa ...

最新文章

  1. 杀毒时能否使用计算机,电脑杀毒以后,程序无法使用,电脑杀毒后共享不能使用-...
  2. POJ3461 Oulipo ——KMP算法——Pku3461
  3. MATLAB线型和颜色对应说明
  4. [Erlang 0004] Centos 源代码编译 安装 Erlang
  5. matlab randn state 2,MATLAB?中的randn函数
  6. MATLAB 动图绘制、保存
  7. 拓端tecdat|Python实现谱聚类Spectral Clustering算法和改变簇数结果可视化比较
  8. delphi xe 10.4 开发 APP
  9. Windows 95/98虚拟机OS安装说明书[仅限VirtualBox]
  10. cadence virtuoso画版图提示LUP.6错误
  11. 面向对象使用python-docx模块制作格式化文本(奖状生成器)
  12. Conduit-面向Kubernetes的轻量化服务网格
  13. php 问号乱码,如何解决php问号乱码的问题
  14. [548]OpenCV之cv2函数
  15. BNN Pytorch代码阅读笔记
  16. 伪静态URLRewrite学习笔记
  17. 【千锋Python2205班9.29笔记-day09-字符串(一阶段)】
  18. 水仙花数是指一个n位数(n≥3),它的每个位上的数字的n次幂之和等于它本身。例如:1^3+5^3+3^3=153
  19. CTP2资金/持仓管理
  20. 用java做小学数学系统_基于jsp的小学数学试卷生成-JavaEE实现小学数学试卷生成 - java项目源码...

热门文章

  1. qt 判断文件是否存在
  2. SparkShell创建表
  3. 微信消息模板申请攻略
  4. SUN 的JDO标准
  5. 最简陋的MP3播放器
  6. Race Condition 引起的 HashMap CPU100%
  7. [网摘]---有关int,Int32的疑惑解答
  8. 基于内容的视频信息检索系统
  9. springboot 图片映射404问题
  10. 编程语言有哪些?这些知识可以了解一下