添加支持
<!--添加导入/出表格依赖-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency> 

新建Excel实体类
import java.io.Serializable;
import java.util.List;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;}
}

 
添加excel工具类
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.OutputStream;
import java.util.List;import com.soft.ssmproject.entity.ExcelData;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder.BorderSide;import java.awt.Color;
import java.net.URLEncoder;public class ExcelUtils {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);} catch(Exception e){e.printStackTrace();}finally{//此处需要关闭 wb 变量out.close();}}private static void writeExcel(XSSFWorkbook wb, Sheet sheet, ExcelData data) {int rowIndex = 0;rowIndex = writeTitlesToExcel(wb, sheet, data.getTitles());writeRowsToExcel(wb, sheet, data.getRows(), rowIndex);autoSizeColumns(sheet, data.getTitles().size() + 1);}private static int writeTitlesToExcel(XSSFWorkbook wb, Sheet sheet, List<String> titles) {int rowIndex = 0;int colIndex = 0;Font titleFont = wb.createFont();titleFont.setFontName("simsun");//titleFont.setBoldweight(Short.MAX_VALUE);// 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(182, 184, 192)));titleStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);titleStyle.setFont(titleFont);setBorder(titleStyle, BorderStyle.THIN, new XSSFColor(new Color(0, 0, 0)));Row titleRow = sheet.createRow(rowIndex);// titleRow.setHeightInPoints(25);colIndex = 0;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 = 0;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(0, 0, 0)));for (List<Object> rowData : rows) {Row dataRow = sheet.createRow(rowIndex);// dataRow.setHeightInPoints(25);colIndex = 0;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 = 0; i < columnNumber; i++) {int orgWidth = sheet.getColumnWidth(i);sheet.autoSizeColumn(i, true);int newWidth = (int) (sheet.getColumnWidth(i) + 100);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);}
}


# controller层
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;import javax.servlet.http.HttpServletResponse;import com.soft.ssmproject.entity.ExcelData;
import com.soft.ssmproject.entity.ExcelInfo;
import com.soft.ssmproject.tool.ExcelUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/excel")
public class ExcelController {@RequestMapping(value = "/export", method = RequestMethod.POST)public void excel(HttpServletResponse response,ExcelInfo excelInfo) throws Exception {ExcelData data = new ExcelData();data.setName("用户信息数据");//添加表头List<String> titles = new ArrayList();//for(String title: excelInfo.getNames())titles.add(excelInfo.getNames()[0]);titles.add(excelInfo.getAccount()[0]);titles.add(excelInfo.getDept()[0]);titles.add(excelInfo.getGender()[0]);titles.add(excelInfo.getEmail()[0]);data.setTitles(titles);//添加列List<List<Object>> rows = new ArrayList();List<Object> row = null;for(int i=1; i<excelInfo.getNames().length;i++){row=new ArrayList();row.add(excelInfo.getNames()[i]);row.add(excelInfo.getAccount()[i]);row.add(excelInfo.getDept()[i]);row.add(excelInfo.getGender()[i]);row.add(excelInfo.getEmail()[i]);rows.add(row);}data.setRows(rows);SimpleDateFormat fdate=new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");String fileName=fdate.format(new Date())+".xls";ExcelUtils.exportExcel(response, fileName, data);}
}

转载于:https://www.cnblogs.com/tengtianshan/p/9696014.html

Spring Boot 导出Excel表格相关推荐

  1. Spring Boot poi 导出Excel表格、Txt到浏览器下载

    Spring Boot & poi 导出Excel表格.Txt到浏览器下载 原文链接:小回博客 文章目录 Spring Boot & poi 导出Excel表格.Txt到浏览器下载 一 ...

  2. FineReport 报表模板生成+导出Excel表格

    跟spring boot 整合 :https://blog.csdn.net/zhanglixin_1984/article/details/79194145 看官方的文档纠结了一天,好东西,这个怎么 ...

  3. java实现这个无表头空表格,《java程序导出excel表格是空白的没有数据?》 java怎么导入数据库...

    java程序导出excel表格是空白的没有数据? 看一下是否可以链接上数据库,或者查看一下链接的数据库是否有数据 java从数据库中导出excel poi 建议你使用pageoffice. java ...

  4. Spring boot读取Excel并存入PG数据库(一)

    Spring boot读取Excel并存入PG数据库(一) 目录 一.前言 二.项目需求 三.数据库表设计 四.代码实现和测试 五.总结 一.前言 进入9月以来,一直忙于项目,特别是临近国庆节这半个月 ...

  5. 上传文件、下载文件、数据导出excel表格整理模板

    上传文件 思路: 1.设置上传的路径,上传名 2.在这个路径path上新建名为filename的文件 file 3.判断该文件的父目录是否存在,若不存在就新建一个 4.用.transferTo方法将文 ...

  6. html 文字从数据库导出excel,html数据库导出excel表格

    关于 html数据库导出excel表格的搜索结果 回答 将execl表格导出为csv格式&xff0c;然后数据就是一行一行以逗号分割的数据.后面就好办了吧#找python的excel库.htt ...

  7. elementui中有导出组件吗_Element-ui组件库Table表格导出Excel表格

    安装 npm install --save xlsx file-saver 两个插件的详细地址在下面 https://github.com/SheetJS/js-xlsx https://github ...

  8. 使用NPOI将数据库里信息导出Excel表格并提示用户下载

    使用NPOI进行导出Excel表格大家基本都会,我在网上却很少找到导出Excel表格并提示下载的 简单的代码如下 1 //mvc项目可以传多个id以逗号相隔的字符串2 public ActionRes ...

  9. java中使用jxl导出Excel表格详细通用步骤

    该方法一般接收两个参数,response和要导出的表格内容的list. 一般我们将数据库的数据查询出来在页面进行展示,根据用户需求,可能需要对页面数据进行导出. 此时只要将展示之前查询所得的数据放入s ...

最新文章

  1. 在路由器上设置虚拟ftp服务器,怎么在路由器上开启ftp服务器配置
  2. Shader Compilation for Multiple Platforms
  3. emeditor利用书签功能导出匹配结果到新文件
  4. 哪些医药企业使用SAP系统呢?
  5. 1.Android中解析json程序代码
  6. 斗地主案例的代码实现
  7. jquery验证表单很简单的方法
  8. 为自己编写的windows应用程序制作安装包
  9. 拓端tecdat|R语言近似贝叶斯计算MCMC(ABC-MCMC)轨迹图和边缘图可视化
  10. 阵列信号处理及matlab实现,《阵列信号处理的理论和应用》(pdf+程序)
  11. Mac 删除不必要的 Adobe PS AI 组件
  12. 模仿微信图片编辑器--动画实现向上弹出文字编辑框(遮罩)界面
  13. eclipse中的svn提交代码时文件上出现蓝色加号或者十字架时最笨但最有效的方法
  14. 种春草肥禾,织数字天下
  15. 基于Matlab的汽车安全应用轨道融合仿真(附源码)
  16. php中的sql函数的作用,PHP实用函数9
  17. vue给标签动态添加元素_vue页面动态添加元素
  18. 解析ActivityManagerService
  19. libvirt虚拟化技术介绍
  20. 使用Python进行图像处理

热门文章

  1. 分布式文件系统MFS(moosefs)实现存储共享(一)
  2. 深度 | 伯克利教授Stuart Russell:人工智能基础概念与34个误区
  3. Android实现按两次back键退出应用
  4. socket.io框架学习
  5. 谢宝友:会说话的Linux内核
  6. IIS FTP 安装程序无法复制文件的问题
  7. 陶哲轩实分析例17.2.3
  8. 计算机完成了加法操作执行的是,cpu是通过运算器中的什么来完成加法运算的
  9. adsense 注册_adsense在注册时的注意事项
  10. java emf 转jpg_java – emf到jpg的转换