Springboot整合poi实现导出导入Excle表格+Vue引入echarts数据展示

您所在的位置:网站首页 红米k20pro如何调屏幕刷新率设置 Springboot整合poi实现导出导入Excle表格+Vue引入echarts数据展示

Springboot整合poi实现导出导入Excle表格+Vue引入echarts数据展示

2022-11-29 05:52| 来源: 网络整理| 查看: 265

需求: 一. 数据库数据表导出Excle表格           二. Excle数据导入数据库           三. Vue引入Echarts数据展示 工具: idea 数据库: mysql 框架:Springboot

准备工作:

1.maven主要依赖:poi

org.apache.poi poi 3.17 org.apache.poi poi-ooxml 4.1.2

2.yaml配置

spring: thymeleaf: cache: false suffix=: .html prefix: classpath:/templates/ datasource: url: jdbc:mysql://localhost:3306/mylove?serverTimezone=GMT-8 driver-class-name: com.mysql.cj.jdbc.Driver username: root password: root mybatis-plus: # mapperLocations: classpath:mapper/*.xml configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 全局配置id自增 => global-config: db-config: id-type: auto 一. 数据库数据表导出Excle表格 1.准备实体类(我这个是两表联查的实体类,有需要的可以单表)

 2.service查询数据

 两表查询出数据List集合

3.准备Excle工具类 public class ExcelUtil { /** * Excel表格导出 * @param response HttpServletResponse对象 * @param excelData Excel表格的数据,封装为List * @param sheetName sheet的名字 * @param fileName 导出Excel的文件名 * @param columnWidth Excel表格的宽度,建议为15 * @throws IOException 抛IO异常 */ public static void exportExcel(HttpServletResponse response, List excelData, String sheetName, String fileName, int columnWidth) throws IOException { //声明一个工作簿 HSSFWorkbook workbook = new HSSFWorkbook(); //生成一个表格,设置表格名称 HSSFSheet sheet = workbook.createSheet(sheetName); //设置表格列宽度 sheet.setDefaultColumnWidth(columnWidth); //写入List中的数据 int rowIndex = 0; for(List data : excelData){ //创建一个row行,然后自增1 HSSFRow row = sheet.createRow(rowIndex++); //遍历添加本行数据 for (int i = 0; i < data.size(); i++) { //创建一个单元格 HSSFCell cell = row.createCell(i); //创建一个内容对象 HSSFRichTextString text = new HSSFRichTextString(data.get(i)); //将内容对象的文字内容写入到单元格中 cell.setCellValue(text); } } //准备将Excel的输出流通过response输出到页面下载 //八进制输出流 response.setContentType("application/octet-stream"); //设置导出Excel的名称 response.setHeader("Content-disposition", "attachment;filename=" + fileName); //刷新缓冲 response.flushBuffer(); //workbook将Excel写入到response的输出流中,供页面下载该Excel文件 workbook.write(response.getOutputStream()); //关闭workbook workbook.close(); } } 4.编写Controller接口 @Controller public class ExcelController { @Autowired goodServiceImpl goodService; /** * Excel表格导出接口 * http://localhost:8080/ExcelDownload * * @param response response对象 * @throws IOException 抛IO异常 */ @RequestMapping("/ExcelDownload") public void excelDownload(HttpServletResponse response) throws IOException { //表头数据 String[] header = {"id", "数量", "姓名", "单价", "物品类型"}; //声明一个工作簿 HSSFWorkbook workbook = new HSSFWorkbook(); //生成一个表格,设置表格名称为"学生表" HSSFSheet sheet = workbook.createSheet("学生表"); //设置表格列宽度为10个字节 sheet.setDefaultColumnWidth(10); //创建第一行表头 HSSFRow headrow = sheet.createRow(0); //遍历添加表头(下面模拟遍历学生,也是同样的操作过程) for (int i = 0; i < header.length; i++) { //创建一个单元格 HSSFCell cell = headrow.createCell(i); //创建一个内容对象 HSSFRichTextString text = new HSSFRichTextString(header[i]); //将内容对象的文字内容写入到单元格中 cell.setCellValue(text); } //*********************************** //拿到数据 List goods = goodService.findAllGoods(); for (int i = 0; i < goods.size(); i++) { HSSFRow row = sheet.createRow(i + 1); Goods good = goods.get(i); HSSFCell cell = null; String[] names = {Math.toIntExact(good.getId()) + "", good.getCount() + "", good.getName() + "", good.getPrice() + "", good.getType() + ""}; for (int k = 0; k < names.length; k++) { cell = row.createCell(k); //拿到属性值 HSSFRichTextString text = new HSSFRichTextString(names[k]); cell.setCellValue(text); } } //***************************************** //准备将Excel的输出流通过response输出到页面下载 //八进制输出流 response.setContentType("application/octet-stream"); //这后面可以设置导出Excel的名称,此例中名为student.xls response.setHeader("Content-disposition", "attachment;filename=student.xls"); //刷新缓冲 response.flushBuffer(); //workbook将Excel写入到response的输出流中,供页面下载 workbook.write(response.getOutputStream()); } }

关键提醒:  **********************包裹起来的代码是从数据库拿到的数据List集合 

这样就可以生成Excle表格了

二. Excle数据导入数据库

1.编写新增实体类 @Data @AllArgsConstructor @NoArgsConstructor public class Goods { @Value(value = "id") private long id; @Value(value = "数量") private long count; @Value(value = "姓名") private String name; @Value(value = "单价") private double price; @Value(value = "物品类型") private long type; }

@Value属性是匹配Excle表格的标头名称

2.service层批量新增数据 /** * 批量新增goods方法 * @param list goods * @return 0 */ public int addAllGoods(List list){ for (int i = 0; i


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3