SpringBoot导入Excel数据到MySQL数据库
package com.example.example1.Controller;
import com.example.example1.Entity.User;
import com.example.example1.Service.UserRepository;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
/**
* Created by BLIT on 2018/11/30.
*/
@Controller
@RequestMapping(path = "/excel")
public class ImportController {
@Autowired
private UserRepository userRepository; @GetMapping(path = "/import")
@ResponseBody
public void Import() throws IOException{
File file = new File("C:\\Users\\zxg\\Desktop\\测试2003.xls"); //实际这个路径由前端传后台
FileInputStream fis = new FileInputStream(file);
Workbook wb = null;
try {
if(isExcel2003(file.getPath())){
System.out.println("2003版本Excel: .xls结尾");
wb = new HSSFWorkbook(fis); //创建2003版本Excel工作簿对象
}else if (isExcel2007(file.getPath())){
System.out.println("2007版本Excel: .xlsx结尾");
wb = new XSSFWorkbook(fis); //创建2007版本Excel工作簿对象
}else {
System.out.println("未知版本的Excel !!!");
}
Sheet sheet = wb.getSheetAt(0); //获取第1个工作表
for(int i=1;i<=sheet.getLastRowNum();i++){//循环Excel文件的i=1行开始
User user = new User();
Row row = sheet.getRow(i); //获取第i行
Cell cell1 = row.getCell(0); //获取第1个单元格的数据
cell1.setCellType(Cell.CELL_TYPE_STRING); //设置Cell的类型为String类型
Cell cell2 = row.getCell(1); //获取第2个单元格的数据
cell2.setCellType(Cell.CELL_TYPE_STRING);
user.setName(cell1.getStringCellValue());
user.setEmail(cell2.getStringCellValue());
System.out.println("第一单元格: " + cell1.getStringCellValue());
System.out.println("第二单元格: " + cell2.getStringCellValue());
userRepository.save(user); //保存
}
} catch (Exception e) {
e.printStackTrace();
}finally {
fis.close();
}
} public static boolean isExcel2003(String filePath)
{
return filePath.matches("^.+\\.(?i)(xls)$");
} public static boolean isExcel2007(String filePath)
{
return filePath.matches("^.+\\.(?i)(xlsx)$");
} }
SpringBoot导入Excel数据到MySQL数据库的更多相关文章
- Asp.Net Core 导入Excel数据到Sqlite数据库并重新导出到Excel
Asp.Net Core 导入Excel数据到Sqlite数据库并重新导出到Excel 在博文"在Asp.Net Core 使用 Sqlite 数据库"中创建了ASP.NET Co ...
- PHP导入excel数据到MYSQL
这里介绍一个直接将excel文件导入mysql的例子.我花了一晚上的时间测试,无论导入简繁体都不会出现乱码,非常好用.PHP-ExcelReader,下载地址: http://sourceforge. ...
- 通过读取excel数据和mysql数据库数据做对比(二)-代码编写测试
通过上一步,环境已搭建好了. 下面开始实战, 首先,编写链接mysql的函数conn_sql.py import pymysql def sql_conn(u,pwd,h,db): conn=pymy ...
- 导入Excel数据到Oracle数据库的脚本
在cmd运行窗口中输入:sqlldr customermanager/123@orcl control="E:\CustomerData\excelInputOracle\insert.ct ...
- 使用PLSQL导入excel数据至oracle数据库
https://blog.csdn.net/qq_42909551/article/details/82108754 https://jingyan.baidu.com/album/14bd256e2 ...
- 导入excel 数据到mysql出现的时间格式
昨天把一张表的数据导出做修改,然后用Navcat 导入,结果总是失败,也看不出问题,说时间格式不对,我看了excel里时间格式对的,之前是excel导出的,搞 了一两个小时,今天发现导入有个选项,我的 ...
- 通过读取excel数据和mysql数据库数据做对比(一)-win环境准备
要想操作excel和mysql首先需要安装python,然后是安装excel和mysql插件: 第一步安装python: 直接百度搜索,下载安装就可以了. 第二步安装excel插件: 首先到这个htt ...
- 快速导入Excel数据到mysql
首先利用mysql文件,导出csv文件, 然后,直接修改csv文件,然后导入csv文件
- 从phpMyAdmin批量导入Excel内容到MySQL(亲测非常简洁有效)
今天做项目遇到需要用phpMyAdmin批量导入Excel内容到MySQL数据库.分析了我的踏坑经历并且总结一最便捷的一套导入数据的方法,非常实用简洁: 1.修改Excel表的数据,使得Excel中的 ...
随机推荐
- java之spring之整合ssh-2
这篇也是主要讲解 ssh 的整合,不同于上一篇的是它的注入方式. 这篇会采用扫描注入的方式,即去除 applicationContext-asd.xml 文件. 目录结构如下: 注意,这里只列举不同的 ...
- 使用Filezilla Server配置FTP服务器
一.下载Filezilla Server 官网网址:https://filezilla-project.org 二.安装Filezilla Server Filezilla Server的安 ...
- 复杂dic的文件化存储和读取问题
今天遇到一个难题.整出一个复杂的dic,里面不仅维度多,还含有numpy.array.超级复杂.过程中希望能够存储一下,万一服务器停了呢?万一断电了呢? 结果存好存,取出来可就不是那样了.网上搜索了很 ...
- 交换ESCHAUNGE英语ESCHAUNGE交易所
exchange From Middle English eschaunge, borrowed from Anglo-Norman eschaunge exchange 1.An act of ex ...
- java Document生成和解析xml
转自:https://blog.csdn.net/p812438109/article/details/81807440 Document场景:需要知道文档所有结构 需要把文档一些元素排序 文档中的信 ...
- day 01 预科
目录 作业 二,Markdown基本语法 一级标题 二级标题 三级标题 作业 二,Markdown基本语法 标题 一级标题 二级标题 三级标题 四级标题 加粗 哦,更粗了 斜体 咦,我歪了 高亮 == ...
- Apache Thrift安装介绍 (ubuntu)
apache thrift是一种常用的远程服务调用框架. 下面对apache thrift的安装进行介绍: 下面是thrift的源码安装: Debian/Ubuntu (14+) 编译运行依赖安装 $ ...
- 安恒西湖论剑线下上午CTF部分题目WP
简单的做了两个题,一道逆向,一道misc,其他题目,因为博主上课,时间不太够,复现时间也只有一天,后面的会慢慢补上 先说RE1,一道很简单的win32逆向,跟踪主函数,R或者TAB按几下, 根据esp ...
- RedHat7.4 yum配置
RedHat7.4 yum配置 linux 1. yum配置 1.1 本地yum源配置 1.2 配置网络yum源为CentOS源 1. yum配置 1.1 本地yum源配置 设置使用ISO镜像软件:虚 ...
- Maven之setting.xml 配置详解
文件存放位置 全局配置: ${M2_HOME}/conf/settings.xml 用户配置: ${user.home}/.m2/settings.xml note:用户配置优先于全局配置.${use ...