博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springboot导出excel
阅读量:6927 次
发布时间:2019-06-27

本文共 2567 字,大约阅读时间需要 8 分钟。

hot3.png

使用easypoi快速导出excel

新建springboot工程

pom

org.springframework.boot
spring-boot-starter-data-jpa
org.springframework.boot
spring-boot-starter-web
org.jeecg
easypoi-base
2.4.0

新建model

@Entity@Table@ExcelTarget(value = "user")public class User {    @Id    @GeneratedValue(strategy = GenerationType.IDENTITY)    private long uid;    @Excel(name = "学生账号", orderNum = "1", width = 25,height = 15)    private String username;    @Excel(name = "年龄", orderNum = "2", width = 25,height = 15)    private int age;    public long getUid() {        return uid;    }    public void setUid(long uid) {        this.uid = uid;    }    public String getUsername() {        return username;    }    public void setUsername(String username) {        this.username = username;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }}

repository

@Repositorypublic interface Userrepository extends JpaRepository
{}

controller

@Controllerpublic class UserController {    @Autowired    Userrepository userrepository;    @GetMapping(value = "/export")    public void export(HttpServletResponse response) throws IOException {        StringBuffer sb = new StringBuffer();        String excelname = new String(sb.append("用户统计").toString().getBytes("gbk"), "iso8859-1");        response.setHeader("content-Type", "application/vnd.ms-excel");        response.setHeader("Content-Disposition", "attachment;filename=" + excelname + ".xls");        OutputStream os = null;        List
userList = userrepository.findAll(); try { Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), User.class, userList); //设置样式 Sheet sheet = workbook.getSheet("Sheet0");//默認sheet的名字 Row row = sheet.getRow(0); //取XSL文件Sheet1页上第1行 CellStyle cellStyle = workbook.createCellStyle(); Font font = workbook.createFont(); font.setFontHeightInPoints((short)18); //字体大小 font.setFontName("楷体"); font.setBold(true); font.setColor(HSSFColor.RED.index); //紅色字 cellStyle.setFont(font); cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); cellStyle.setAlignment(HorizontalAlignment.CENTER); int cells = row.getPhysicalNumberOfCells(); for (int i=0;i

转载于:https://my.oschina.net/u/3125112/blog/1539757

你可能感兴趣的文章
响应式API的设计、实现和应用
查看>>
Visual Studio 2017 15.7预览版发布
查看>>
RxJava系列七(最佳实践)
查看>>
微软Windows Community Toolkit一览
查看>>
前端资源系列(5)-JavaScript奇味探索
查看>>
基于AngularJS的个推前端云组件探秘
查看>>
工行数据中心高级经理 李雁南:接口冒烟测试方法
查看>>
oh-my-zsh 精彩纷呈
查看>>
更靠谱的横竖屏检测方法
查看>>
git初始化操作以及一些问题的解决
查看>>
pcl常用小知识
查看>>
进军Docker 1.12,将代理与Swarm完美整合
查看>>
js中的立即执行函数
查看>>
多屏互动——H5 中级进阶
查看>>
1625行,解开 underscore.js 的面纱 - 第三章
查看>>
IOS-Swift开发基础——使用相机拍照
查看>>
magento 1 版本block、controller、model的重写
查看>>
关于CSS的position属性
查看>>
javascript 基本包装类型总结
查看>>
【安装PHP】如何在openSUSE42.1下编译安装PHP7
查看>>