至于poi的用法就不多说了,网上多得很,但是发现spring boot结合poi的就不多了,而且大多也有各种各样的问题。

public class ExcelData implements Serializable {

    private static final long serialVersionUID = 4444017239100620999L;

    // 表头
private List<String> titles; // 数据
private List<List<Object>> rows; // 页签名称
private String name; public List<String> getTitles() {
return titles;
} public void setTitles(List<String> titles) {
this.titles = titles;
} public List<List<Object>> getRows() {
return rows;
} public void setRows(List<List<Object>> rows) {
this.rows = rows;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} }
public class ExportExcelUtils {
public static void exportExcel(HttpServletResponse response, String fileName, ExcelData data) throws Exception {
// 告诉浏览器用什么软件可以打开此文件
response.setHeader("content-Type", "application/vnd.ms-excel");
// 下载文件的默认名称
response.setHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode(fileName, "utf-8"));
exportExcel(data, response.getOutputStream());
} public static void exportExcel(ExcelData data, OutputStream out) throws Exception { XSSFWorkbook wb = new XSSFWorkbook();
try {
String sheetName = data.getName();
if (null == sheetName) {
sheetName = "Sheet1";
}
XSSFSheet sheet = wb.createSheet(sheetName);
writeExcel(wb, sheet, data); wb.write(out);
} finally {
wb.close();
}
} private static void writeExcel(XSSFWorkbook wb, Sheet sheet, ExcelData data) { int rowIndex = ; rowIndex = writeTitlesToExcel(wb, sheet, data.getTitles());
writeRowsToExcel(wb, sheet, data.getRows(), rowIndex);
autoSizeColumns(sheet, data.getTitles().size() + ); } private static int writeTitlesToExcel(XSSFWorkbook wb, Sheet sheet, List<String> titles) {
int rowIndex = ;
int colIndex = ; Font titleFont = wb.createFont();
titleFont.setFontName("simsun");
titleFont.setBold(true);
// titleFont.setFontHeightInPoints((short) 14);
titleFont.setColor(IndexedColors.BLACK.index); XSSFCellStyle titleStyle = wb.createCellStyle();
titleStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
titleStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
titleStyle.setFillForegroundColor(new XSSFColor(new Color(, , )));
titleStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
titleStyle.setFont(titleFont);
setBorder(titleStyle, BorderStyle.THIN, new XSSFColor(new Color(, , ))); Row titleRow = sheet.createRow(rowIndex);
// titleRow.setHeightInPoints(25);
colIndex = ; for (String field : titles) {
Cell cell = titleRow.createCell(colIndex);
cell.setCellValue(field);
cell.setCellStyle(titleStyle);
colIndex++;
} rowIndex++;
return rowIndex;
} private static int writeRowsToExcel(XSSFWorkbook wb, Sheet sheet, List<List<Object>> rows, int rowIndex) {
int colIndex = ; Font dataFont = wb.createFont();
dataFont.setFontName("simsun");
// dataFont.setFontHeightInPoints((short) 14);
dataFont.setColor(IndexedColors.BLACK.index); XSSFCellStyle dataStyle = wb.createCellStyle();
dataStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
dataStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
dataStyle.setFont(dataFont);
setBorder(dataStyle, BorderStyle.THIN, new XSSFColor(new Color(, , ))); for (List<Object> rowData : rows) {
Row dataRow = sheet.createRow(rowIndex);
// dataRow.setHeightInPoints(25);
colIndex = ; for (Object cellData : rowData) {
Cell cell = dataRow.createCell(colIndex);
if (cellData != null) {
cell.setCellValue(cellData.toString());
} else {
cell.setCellValue("");
} cell.setCellStyle(dataStyle);
colIndex++;
}
rowIndex++;
}
return rowIndex;
} private static void autoSizeColumns(Sheet sheet, int columnNumber) { for (int i = ; i < columnNumber; i++) {
int orgWidth = sheet.getColumnWidth(i);
sheet.autoSizeColumn(i, true);
int newWidth = (int) (sheet.getColumnWidth(i) + );
if (newWidth > orgWidth) {
sheet.setColumnWidth(i, newWidth);
} else {
sheet.setColumnWidth(i, orgWidth);
}
}
} private static void setBorder(XSSFCellStyle style, BorderStyle border, XSSFColor color) {
style.setBorderTop(border);
style.setBorderLeft(border);
style.setBorderRight(border);
style.setBorderBottom(border);
style.setBorderColor(BorderSide.TOP, color);
style.setBorderColor(BorderSide.LEFT, color);
style.setBorderColor(BorderSide.RIGHT, color);
style.setBorderColor(BorderSide.BOTTOM, color);
}
}
@RestController
public class ExcelController {
@RequestMapping(value = "/excel", method = RequestMethod.GET)
public void excel(HttpServletResponse response) throws Exception {
ExcelData data = new ExcelData();
data.setName("hello");
List<String> titles = new ArrayList();
titles.add("a1");
titles.add("a2");
titles.add("a3");
data.setTitles(titles); List<List<Object>> rows = new ArrayList();
List<Object> row = new ArrayList();
row.add("");
row.add("");
row.add("");
rows.add(row); data.setRows(rows); //生成本地
/*File f = new File("c:/test.xlsx");
FileOutputStream out = new FileOutputStream(f);
ExportExcelUtils.exportExcel(data, out);
out.close();*/
ExportExcelUtils.exportExcel(response,"hello.xlsx",data);
}
}

功能比较简单,没有做太多扩展,想自己扩展的可以查看poi的官方文档自行扩展,简单的很。

如果用POST提交,前端不能用Ajax,只能用form表单提交,参数接受Controller那一块用@ModelAttribute,@RequestBody是专门用于接受Json数据的

项目地址:https://github.com/xiaopotian1990/SpringBootExcel

Spring Boot利用poi导出Excel的更多相关文章

  1. spring boot:使用poi导出excel电子表格文件(spring boot 2.3.1)

    一,什么是poi? 1,poi poi是用来兼容微软文档格式的java api, 它是apache的顶级项目之一, 也是我们在生产环境中导出excel时使用最多的库 2,poi官方网站: http:/ ...

  2. spring boot 整合 poi 导出excel

    一. 第一种方式 1.首先从中央仓库中导入架包Poi3.14以及Poi-ooxml3.14. <dependency> <groupId>org.apache.poi</ ...

  3. spring boot 使用POI导出数据到Excel表格

    在spring boot 的项目经常碰到将数据导出到Excel表格的需求,而POI技术则对于java操作Excel表格提供了API,POI中对于多种类型的文档都提供了操作的接口,但是其对于Excel表 ...

  4. spring boot使用AbstractXlsView导出excel

    一.maven依赖jar包 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi ...

  5. struts2中利用POI导出Excel文档并下载

    1.项目组负责人让我实现这个接口,因为以前做过类似的,中间并没有遇到什么太困难的事情.其他不说,先上代码: package com.tydic.eshop.action.feedback; impor ...

  6. 利用poi导出Excel

    import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.lang.r ...

  7. JAVAWeb SSH框架 利用POI 导出EXCEL,弹出保存框

    导入包这一些不多说,直接贴出关键代码,JSP只要点一个Action链接就行. poi包我是用:poi-3.11-20141221.jar 亲测有效: 效果: Action 类代码: private I ...

  8. spring boot 使用 POI 读取Excel文件

    内容简介 本文主要介绍使用POI进行Excel文件的相关操作,涉及读取文件,获取sheet表格,对单元格内容进行读写操作,以及合并单元格的操作. Excel文件目录 Excel模板文件存了resour ...

  9. Java Web利用POI导出Excel简单例子

    采用Spring mvc架构: Controller层代码如下 @Controller public class StudentExportController{ @Autowired private ...

随机推荐

  1. 第三方登录之微信登录,基于ThinkSDK

    本文基于ThinkSDK,为其补充微信登录demo 增加ThinkSDK的微信第三方登录 阅读本文之前请先了解ThinkSDK的文档 http://www.echomod.com/nexstep/fo ...

  2. node.js 获取客户端信息

    结果:

  3. oracke创建db link

    -- Create database link create public database link test_link connect to 用户名 identified by 密码 using ...

  4. spring 配置双数据源并读写分离

    摘自 开源项目Ibase4j    关键思想在于AbstractRoutingSource 类 还有方法名称和切入点去控制使用哪个数据源    1.首先在配置文件配置多个数据源 并且交给继承自spri ...

  5. file_get_content服务器对服务器二进制文件上传

    1.file_get_contents函数可安全用于二进制对象,适用服务器对服务器文件是上传场景 base64_encode(file_get_contents('1268879774AaCl4wIE ...

  6. linux grep日志查询

    ll access.2018-09-*.gz zcat access.2018-09-*.gz |grep --color '1073011900' | head -n  100  匹配字符由于管道h ...

  7. CentOS7.6安装mailx

    由于ECS服务器安全问题,发送邮件统一使用SSL模式 安装开始: 第一步:Yum安装mailx:yum install -y mailx 第二步: 创建证书存放目录(如以存在无需创建):mkdir - ...

  8. IOS 后台之长时间任务 beginBackgroundTaskWithExpirationHandler 申请后台十分钟 600秒

    10分钟 beginBackgroundTaskWithExpirationHandler,beginBackgroundTaskWithName endBackgroundTask 定义变量 UIB ...

  9. STL : 反向迭代器(Reverse Iterator)

    1. 定义反向迭代器(Reverse Iterator)是一种反向遍历容器的迭代器.也就是,从最后一个元素到第一个元素遍历容器.反向迭代器将自增(和自减)的含义反过来了:对于反向迭代器,++运算将访问 ...

  10. Memcache 内存对象缓存系统

    简介: Memcached 是一个高性能的分布式内存存储对象缓存系统,用于动态 WEB 应用以减轻数据库负载. 它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提供动态.数据库驱动网站的速度. ...