Apache POI操作分批导入MySQL数据库
发布时间:2023-02-20 10:45:32 所属栏目:Apache 来源:互联网
导读:poi介绍: Apache POI是用Java编写的免费开源的跨平台的Java API,Apache POI提供API给Java程序对Microsoft Office格式档案读和写的功能,其中使用最多的就是使用POI操作Excel文件。 POI使用到的相关maven依赖坐标如下: dependency groupIdorg.apache.poi/g
批量导入预约设置信息操作过程: 1、点击模板下载按钮下载Excel模板文件 2、将预约设置信息录入到模板文件中 3、点击上传文件按钮将录入完信息的模板文件上传到服务器 4、通过POI读取上传文件的数据并保存到数据库 创建对应的实体类 public class OrderSetting implements Serializable{ private Integer id ; private Date orderDate;//预约设置日期 private int number;//可预约人数 private int reservations ;//已预约人数 public OrderSetting() { } public OrderSetting(Date orderDate, int number) { this.orderDate = orderDate; this.number = number; } } 2.1、创建导入数据的Excel模板文件 目的是为了统一管理导入的数据格式 [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dbCEXvYt-1655542646539)(images/image-20220618142527340.png)] 修改页面提供一个下载批量导入数据的模板按钮 [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7OlfHCQJ-1655542646540)(images/image-20220618143241355.png)] //下载模板文件 downloadTemplate(){ window.location.href="../../template/ordersetting_template.xlsx" rel="external nofollow" ; } 文件上传前端代码实现 <el-upload action="/ordersetting/upload.do" name="excelFile" :show-file-list="false" :on-success="handleSuccess" :before-upload="beforeUpload"> <el-button type="primary">上传文件</el-button> </el-upload> 提交函数 ![]() // 上传之前进行文件格式校验 beforeUpload(file){ const isXLS = file.type === 'application/vnd.ms-excel'; if(isXLS){ return true; } const isXLSX = file.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; if (isXLSX) { return true; } this.$message.error('上传文件只能是xls或者xlsx格式!'); return false; }, // 上传成功提示 handleSuccess(response, file) { if(response.flag){ this.$message({ message: response.message, type: 'success' }); }else{ this.$message.error(response.message); } console.log(response, file, fileList); }, 2.2、批量上传文件的后端代码编写 关于上传文件一般网上都会有很多的工具类可以下载使用,也就是别人将已经封装好的代码拿出来分享给大家使用的 import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; import org.apache.poi.hssf.usermodel.HSSFWorkbook; 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.web.multipart.MultipartFile; public class POIUtils { private final static String xls = "xls"; private final static String xlsx = "xlsx"; private final static String DATE_FORMAT = "yyyy/MM/dd"; /** * 读入excel文件,解析后返回 * @param file * @throws IOException */ public static List<String[]> readExcel(MultipartFile file) throws IOException { //检查文件 checkFile(file); //获得Workbook工作薄对象 Workbook workbook = getWorkBook(file); //创建返回对象,把每行中的值作为一个数组,所有行作为一个集合返回 List<String[]> list = new ArrayList<String[]>(); if(workbook != null){ for(int sheetNum = 0;sheetNum < workbook.getNumberOfSheets();sheetNum++){ //获得当前sheet工作表 Sheet sheet = workbook.getSheetAt(sheetNum); if(sheet == null){ continue; } //获得当前sheet的开始行 int firstRowNum = sheet.getFirstRowNum(); //获得当前sheet的结束行 int lastRowNum = sheet.getLastRowNum(); //循环除了第一行的所有行 for(int rowNum = firstRowNum+1;rowNum <= lastRowNum;rowNum++){ //获得当前行 Row row = sheet.getRow(rowNum); if(row == null){ (编辑:甘南站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |