影视广告制作公司,seo数据分析,1000倍爆率传奇,wap网站建设方案 pdf摘要#xff1a;由于开发需要批量导入Excel中的数据#xff0c;使用了Apache POI库#xff0c;记录下使用过程 1. 背景 Java 中操作 Excel 文件的库常用的有Apache POI 和阿里巴巴的 EasyExcel 。Apache POI 是一个功能比较全面的 Java 库#xff0c;适合处理复杂的 Offi…摘要由于开发需要批量导入Excel中的数据使用了Apache POI库记录下使用过程 1. 背景 Java 中操作 Excel 文件的库常用的有Apache POI 和阿里巴巴的 EasyExcel 。Apache POI 是一个功能比较全面的 Java 库适合处理复杂的 Office 文件操作需求而 EasyExcel 则是一个专注于 Excel 文件读写的简单、高效工具更适合处理 Excel 文件的批量读写需求并且易于上手。当Excel的数据量很大时推荐使用EasyExcel更合适。 Apache的POI 用于处理 Microsoft Office 格式文件如Excel、Word、PowerPoint。它支持读取、写入和操作 Excel 文件可以处理各种 Excel 格式如 .xls 和 .xlsx。Apache POI - the Java API for Microsoft Documentshttps://poi.apache.org/ 阿里的EasyExcel关于Easyexcel | Easy ExcelEasyExcel是一个基于Java的简单、省内存的读写Excel的开源项目在尽可能节约内存的情况下支持读写百M的Excel。https://easyexcel.opensource.alibaba.com/docs/current/
2. Apache POI的使用
2.1 引入依赖poi和poi-ooxml 首先在pom.xml中配置相关依赖并使用Maven引入
!--引入处理Excel的POI类poi这是Apache POI的核心模块用于处理Excel 97-2003格式的.xls 文件。poi-ooxml这个模块则用于处理Office Open XML格式的.xlsx 文件它是基于XML的Office文件格式对应于较新版本的Excel。
--
dependencygroupIdorg.apache.poi/groupIdartifactIdpoi/artifactIdversion3.16/version
/dependency
dependencygroupIdorg.apache.poi/groupIdartifactIdpoi-ooxml/artifactIdversion3.16/version
/dependency
2.2 使用POI读取Excel中数据填充对象 使用Apache POI解析Excel大体过程通过输入流FileInputStream读取Excel中的数据使用XSSFWorkbook类加载输入流创建了一个新版本的Excel工作簿下例子为workbook从创建的工作簿中获取第一个工作表 firstsheet再通过迭代器逐行逐个单元格遍历工作表将单元格中的数据填充到对象中并将对象添加到列表。此时对得到的列表遍历执行相应的数据库操作实现批量导入的功能。
public void addProductByExcel(File destFile) throws IOException {// 从Excel文件中读取商品信息,并转换为商品列表ListProduct products readProductsFromExcel(destFile);for (int i 0; i products.size(); i) {Product product products.get(i);Product productOld productMapper.selectByName(product.getName()); // 通过商品名称来查询数据库中是否已存在同名商品if (productOld ! null) { // 查询到同名商品则抛出自定义异常throw new ImoocMallException(ImoocMallExceptionEnum.NAME_EXISTED);}int count productMapper.insertSelective(product); // 不存在同名商品则向数据库中插入当前商品信息存储受影响的行数if (count 0) {throw new ImoocMallException(ImoocMallExceptionEnum.CREATE_FAILED);}}
}private ListProduct readProductsFromExcel(File excelFile) throws IOException {ArrayListProduct listProducts new ArrayList(); // 用于存储读取到的商品信息FileInputStream inputStream new FileInputStream(excelFile); // 文件输入流 inputStream用于读取Excel文件中的数据XSSFWorkbook workbook new XSSFWorkbook(inputStream); // 使用XSSFWorkbook类加载输入流创建了一个新版本的Excel工作簿workbookXSSFSheet firstsheet workbook.getSheetAt(0); // 获取第一个工作表,index从0还是1开始需要根据实际情况确认IteratorRow iterator firstsheet.iterator(); // 通过工作表的迭代器创建一个行迭代器iterator用于逐行遍历工作表中的数据while(iterator.hasNext()) {Row nextRow iterator.next(); // 逐行遍历IteratorCell cellIterator nextRow.cellIterator(); // 逐个单元格遍历Product aProduct new Product(); // 创建Product对象aProduct用于存储当前行数据对应的商品信息// 读取单元格填充aProductwhile (cellIterator.hasNext()) {Cell nextCell cellIterator.next();int columnIndex nextCell.getColumnIndex(); //获取单元格的索引即列号switch (columnIndex) {case 0:aProduct.setName((String) ExcelUtil.getCellValue(nextCell)); // 针对不同的列号执行相应的操作将单元格数据填充到aProduct对象的相应属性中break;case 1:aProduct.setImage((String) ExcelUtil.getCellValue(nextCell));break;case 2:aProduct.setDetail((String) ExcelUtil.getCellValue(nextCell));break;case 3:Double cellValue (Double) ExcelUtil.getCellValue(nextCell); // excel中数字类型默认为DoubleaProduct.setCategoryId(cellValue.intValue());break;case 4:cellValue (Double) ExcelUtil.getCellValue(nextCell); // excel中数字类型默认为DoubleaProduct.setPrice(cellValue.intValue());break;case 5:cellValue (Double) ExcelUtil.getCellValue(nextCell); // excel中数字类型默认为DoubleaProduct.setStock(cellValue.intValue());break;case 6:cellValue (Double) ExcelUtil.getCellValue(nextCell); // excel中数字类型默认为DoubleaProduct.setStatus(cellValue.intValue());break;default:break;}}listProducts.add(aProduct); // 将填充好数据的aProduct对象添加到商品列表listProducts中}workbook.close(); // 关闭Excel工作簿inputStream.close(); // 关闭文件输入流return listProducts;
} 综上实现了读取Excel中的数据填充进入对象listProducts最终添加到数据库的功能。