位置: IT常识 - 正文

使用EasyPoi导出Excel(easyposer怎么导出)

编辑:rootadmin
使用EasyPoi导出Excel 1、引入Java包依赖<dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-base</artifactId> <version>3.2.0</version></dependency>2、创建导出的Excel样式类:import org.apache.poi.ss.usermodel.BorderStyle;import org.apache.poi.ss.usermodel.CellStyle;import org.apache.poi.ss.usermodel.FillPatternType;import org.apache.poi.ss.usermodel.Font;import org.apache.poi.ss.usermodel.HorizontalAlignment;import org.apache.poi.ss.usermodel.IndexedColors;import org.apache.poi.ss.usermodel.VerticalAlignment;import org.apache.poi.ss.usermodel.Workbook;import cn.afterturn.easypoi.excel.export.styler.AbstractExcelExportStyler;import cn.afterturn.easypoi.excel.export.styler.IExcelExportStyler;/** * @ClassName: ExcelExportMyStylerImpl * @Description: 自定义报表导出样式,可以修改表头颜色,高度等 * @Author: sunt * @Date: 2019/8/29 21:39 * @Version 1.0 **/public class ExcelExportMyStylerImpl extends AbstractExcelExportStyler implements IExcelExportStyler { public ExcelExportMyStylerImpl(Workbook workbook) { super.createStyles(workbook); } @Override public 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") @Override public 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; } @Override public 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") @Override public 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; }}3、创建核心导出工具类import cn.afterturn.easypoi.excel.ExcelExportUtil;import cn.afterturn.easypoi.excel.entity.ExportParams;import com.sunny.spring.boot.poi.common.ExcelExportMyStylerImpl;import com.sunny.spring.boot.poi.pojo.StudentInfoBean;import org.apache.poi.ss.formula.functions.T;import org.apache.poi.ss.usermodel.Workbook;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServletResponse;import java.net.URLEncoder;import java.text.SimpleDateFormat;import java.util.Collection;import java.util.Date;import java.util.List;/** * @ClassName: ExcelExportUtil * @Description: Exceld导出工具类 * @Author: sunt * @Date: 2019/8/30 14:49 * @Version 1.0 **/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) { ExportParams params = new ExportParams(headTitle, sheetName); params.setHeight((short) 8); params.setStyle(ExcelExportMyStylerImpl.class); try { Workbook workbook = ExcelExportUtil.exportExcel(params, pojoClass, listData); String fileName = headTitle + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()); fileName = URLEncoder.encode(fileName, "UTF8"); 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(); } }}4、创建导出对象实体Bean注意日期类型 注解内要加上: exportFormat = "yyyy-MM-dd hh:mm:ss"import cn.afterturn.easypoi.excel.annotation.Excel;import com.baomidou.mybatisplus.annotation.TableField;import com.baomidou.mybatisplus.annotation.TableId;import com.baomidou.mybatisplus.annotation.TableName;import lombok.Data;import lombok.EqualsAndHashCode;import lombok.experimental.Accessors;import java.io.Serializable;import java.math.BigDecimal;/** * <p> * 学生基本信息表 * </p> * * @author sunt * @since 2019-08-29 */@Data@EqualsAndHashCode(callSuper = false)@Accessors(chain = true)@TableName("T_STUDENT")public class StudentInfoBean implements Serializable { private static final long serialVersionUID = 1L; /** * 学号 */ @TableId("ID") @Excel(name = "学号", width = 20, orderNum = "1") private String id; /** * 姓名 */ @TableField("NAME") @Excel(name = "姓名", width = 20, orderNum = "2") private String name; /** * 性别(1:男 2:女) * replace:导出是{a_id,b_id} 导入反过来,注意大括号里面单独引号引起来的 */ @TableField("SEX") @Excel(name = "性别", width = 20, replace = { "男_1", "女_2" },orderNum = "3") private String sex; /** * 年龄 */ @TableField("AGE") @Excel(name = "年龄", width = 20, orderNum = "4") private Integer age; /** * 出生日期 */ @TableField("BIRTHDAY")@Excel(name = "商品创建时间", width = 20, orderNum = "12",exportFormat = "yyyy-MM-dd hh:mm:ss") private String birthday; /** * 入学时间 */ @TableField("REGIST_DATE") @Excel(name = "入学时间",width = 20,orderNum = "6") private String registDate; /** * 学费 */ @TableField("FEE") @Excel(name = "学费", width = 20, orderNum = "7") private BigDecimal fee;}属性字段属性值@TableField这个字段代表数据库表的字段@Excelname代表导出Excel列名称@ExcelorderNum代表Excel列排在第几列@Excelreplace一般数据库存的性别例如0和1,导出的值0展示为男性,女展示为女性5、后台方法:

推荐整理分享使用EasyPoi导出Excel(easyposer怎么导出),希望有所帮助,仅作参考,欢迎阅读内容。

使用EasyPoi导出Excel(easyposer怎么导出)

文章相关热门搜索词:easypoi导入,easypoi导出excel模板,easypoi导出xlsx,easypoi导出excel模板,easypoi导出word,easypoi导出excel模板,easypoi导出xlsx,easypoi导出xlsx,内容如对您有帮助,希望把文章链接给更多的朋友!

直接调用查询方法,返回给前台就OK

@RequestMapping("/exportStudent") public void exportStudent(HttpServletResponse response) { try { List<StudentInfoBean> sutdentList = studentService.queryAllStudent(); MyExcelExportUtil.exportExcel(sutdentList,StudentInfoBean.class,"学生基本信息","新生入学信息",response); } catch (Exception e) { e.printStackTrace(); } }6、前台的方法

不能使用ajax方法,返回的是字符串,后台返回的是流,如果用ajax返回的是乱码,并且浏览器不下载

//导出excel excel(){ window.open("http://localhost:88/api/shop/shop/exportShop") },
本文链接地址:https://www.jiuchutong.com/zhishi/300323.html 转载请保留说明!

上一篇:区块链开发完整指南。如何开发一款区块链项目?(区块链技术开发入门)

下一篇:前端基础之CSS扫盲(前端schema)

  • 印花税的计税方法
  • 一般纳税人材料销售税率
  • 小规模纳税人如何转一般纳税人
  • 两年前发票怎么红冲
  • 会计凭证辅助项
  • 个税是每个月都扣吗
  • 外购的产品用于投资
  • 事业单位存货发生盘亏或盘盈处理
  • 销售补差怎么做分析
  • 进口增值税和进项税
  • 出口业务退税流程资料
  • 各种收入与应纳税额的比例
  • 无法支付的应付款怎么处理
  • 应付账款不用付怎么处理
  • 分包工程有哪些风险
  • 旅游业相关行业
  • 企业采购设备有哪几种情形
  • 预缴和实际应付的区别
  • 福利费专票进项转出怎么做账
  • 委托加工材料收回后的入账价值
  • 担保贷款造成的损失可以税前扣除吗?
  • 小规模季报资产总额填错了有影响吗
  • 收到汽车报废补贴怎么做账?
  • 机器用油怎么做成的
  • 无偿取得股权账务处理
  • 建筑工地临时工工伤
  • mac和wondows
  • 税控盘减免税款结转会计分录
  • 房地产企业预缴增值税如何申报
  • 腾讯电脑管家怎么修复dll
  • php类和对象写法
  • 免征的农资增值税怎么算
  • 合同资产减值怎么填
  • 销售部发生广告宣传费计入财务费用
  • PHP:highlight_file()的用法_misc函数
  • 股息红利要交税吗
  • npm命令不存在
  • 债券投资账务处理例题
  • php读取excel内容
  • 电子发票和纸质发票的法律效力
  • vscode怎么写前端代码
  • 研发入库的产品销售出库怎样做账
  • 公司申报是每月一次吗
  • python django做网页
  • 银行回单应如何打印
  • 长期待摊费用的摊销方法
  • 商誉在资产负债表中如何体现
  • 背书转让的操作
  • 房屋出租简易计税进项税额需要转出么
  • 红字发票抵扣联和发票联要给对方吗
  • 递延所得税资产和递延所得税负债
  • 暂估入库一直未取得发票需要调账吗
  • 替其他公司支付工资怎么做账
  • 应收账款的账面余额是什么意思
  • 技术服务费怎么做分录
  • 开办费计入期间费用明细表
  • 主营业务成本怎么核算
  • 销售成本包括哪些内容
  • 残保金什么时候截止
  • 生产成本如何设置明细账
  • mysql如何解压
  • explain分析sql效率的方法
  • win10预览版0x80072ee2
  • Windows Server 2008使用软件授权管理工具
  • openssl安装教程
  • svchos1.exe - svchos1是什么教程 有什么作用
  • windows10预览版怎么样
  • linux中who命令
  • win7如何安装kb3033929补丁
  • opengl vs2017
  • unity进度条控制动画进度
  • 从零开始的基础篇
  • JavaScript中的this指向
  • javascript基础编程
  • python smtplib模块详解
  • 上海增值税发票红字怎么开
  • 税务系统个人业务自传
  • 出口货物免抵税额怎么申报附加税
  • 浅谈企业所得税论文
  • 建筑业甲方代扣代缴增值税吗
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设