经常遇到给张Excel表,要求数据导入数据库,Navicat for MySQL可以做这个事,手闲写个POI调用通用Mapper插入,之前文章有反向操作,将数据写入Excel,Excel格式报表生成(POI技术)同步下载问题解决
POI依赖,框架用SpringBoot
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.11</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.11</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.11</version>
</dependency>
导入
可以使用单元测试导包,
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.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {
@Autowired
private PtsService ptsService;
@Test
public void contextLoads() {
ptsService.hah();
}
@Test
public void heihei() {
File file = new File("C:/Users/19686/Desktop/产品信息表.xlsx");
try {
InputStream fis = new FileInputStream(file);
//读取导入的Excel文件内容
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(fis);
//获取第一个sheet工作薄
Sheet sheet = xssfWorkbook.getSheetAt(0);
//读取出来的数据放到ListMap中
//List<Map<String, Object>> mapList =new ArrayList<Map<String,Object>>();
List<PtsPlateformProduct> ptsPlateformProducts = new ArrayList<PtsPlateformProduct>();
//获取sheet的最大row下标,实际获取的行数-1
int lastRowNum = sheet.getLastRowNum();
if (lastRowNum > 0) {
for (Row row : sheet) {
//跳过表头
if (row.getRowNum() < 3) {
continue;
}
//判断导入的excel文件中的是否存在空
boolean flag = isExistNull(row);
if (flag) {
PtsPlateformProduct ptsPlateformProduct = new PtsPlateformProduct();
ptsPlateformProduct.setId(UUID.randomUUID().toString().substring(0, 32));
// System.out.println("第"+ (row.getRowNum()+1) + "行,第1个单元格值为:" +row.getCell(0).getStringCellValue());
ptsPlateformProduct.setPtsProductCode(row.getCell(1).getStringCellValue());
// System.out.println("第"+ (row.getRowNum()+1) + "行,第2个单元格值为:" +row.getCell(1).getStringCellValue());
ptsPlateformProduct.setPtsProductName(row.getCell(2).getStringCellValue());
// System.out.println("第"+ (row.getRowNum()+1) + "行,第3个单元格值为:" +row.getCell(2).getStringCellValue());
ptsPlateformProduct.setPtsTradeName(row.getCell(3).getStringCellValue());
// System.out.println("第"+ (row.getRowNum()+1) + "行,第4个单元格值为:" +row.getCell(3).getStringCellValue());
ptsPlateformProduct.setPtsProductSpec(row.getCell(4).getStringCellValue());
// System.out.println("第"+ (row.getRowNum()+1) + "行,第5个单元格值为:" +row.getCell(4).getStringCellValue());
ptsPlateformProduct.setPtsUnitSpec(row.getCell(5).getStringCellValue());
// System.out.println("第"+ (row.getRowNum()+1) + "行,第6个单元格值为:" +row.getCell(5).getStringCellValue());
ptsPlateformProduct.setPtsDoseageForm(row.getCell(6).getStringCellValue());
// System.out.println("第"+ (row.getRowNum()+1) + "行,第7个单元格值为:" +row.getCell(6).getStringCellValue());
ptsPlateformProduct.setPtsDrugPacking(row.getCell(7).getStringCellValue());
// System.out.println("第"+ (row.getRowNum()+1) + "行,第8个单元格值为:" +row.getCell(7).getStringCellValue());
ptsPlateformProduct.setPtsMetricName("");
// System.out.println("第"+ (row.getRowNum()+1) + "行,第9个单元格值为:" +row.getCell(8).getStringCellValue());
ptsPlateformProduct.setPtsManufactureName(row.getCell(9).getStringCellValue());
// System.out.println("第"+ (row.getRowNum()+1) + "行,第10个单元格值为:" +row.getCell(9).getStringCellValue());
ptsPlateformProduct.setMspPlateformCode(row.getCell(10).getStringCellValue());
// System.out.println("第"+ (row.getRowNum()+1) + "行,第11个单元格值为:" +row.getCell(10).getStringCellValue());
ptsPlateformProduct.setVersion(null);
// System.out.println("第"+ (row.getRowNum()+1) + "行,第12个单元格值为:" +row.getCell(11).getStringCellValue());
ptsPlateformProduct.setDeleted(null);
// System.out.println("第"+ (row.getRowNum()+1) + "行,第13个单元格值为:" +row.getCell(12).getStringCellValue());
ptsPlateformProduct.setCreateTime(new Date());
// System.out.println("第"+ (row.getRowNum()+1) + "行,第14个单元格值为:" +row.getCell(13).getStringCellValue());
ptsPlateformProduct.setCreateUserId("");
//System.out.println("第"+ (row.getRowNum()+1) + "行,第15个单元格值为:" +row.getCell(14).getStringCellValue());
ptsPlateformProduct.setUpdateTime(new Date());
//System.out.println("第"+ (row.getRowNum()+1) + "行,第16个单元格值为:" +row.getCell(15).getStringCellValue());
ptsPlateformProduct.setUpdateUserId("");
// System.out.println("第"+ (row.getRowNum()+1) + "行,第17个单元格值为:" +row.getCell(16).getStringCellValue());
ptsPlateformProducts.add(ptsPlateformProduct);
} else {
//获取当前行数
int currentNum = row.getRowNum() + 1;
// return "第" + currentNum + "行数据出错,不能为空!"
System.out.println("第" + currentNum + "行数据出错,不能为空!");
}
}
//调用通用mapper
ptsService.insert(ptsPlateformProducts);
} else {
System.out.println("您导入的excel文件为空,请重新导入!");
// return "您导入的excel文件为空,请重新导入!";
}
} catch (IOException e) {
e.printStackTrace();
}
}
//判断导入的excel文件中row行的每个cell时候是否存在空
public static boolean isExistNull(Row row) {
Cell cell0 = row.getCell(0);
if (cell0 == null) {
return false;
} else {
cell0.setCellType(Cell.CELL_TYPE_STRING);
//可以自定义一个符合规则的StringUtils工具类,这个主要用来判断单元格内容
if (StringUtils.isBlank(cell0.getStringCellValue())) {
return false;
}
}
Cell cell1 = row.getCell(1);
if (cell1 == null) {
return false;
} else {
cell1.setCellType(Cell.CELL_TYPE_STRING);
if (StringUtils.isBlank(cell1.getStringCellValue())) {
return false;
}
}
Cell cell2 = row.getCell(2);
if (cell2 == null) {
return false;
} else {
cell2.setCellType(Cell.CELL_TYPE_STRING);
if (StringUtils.isBlank(cell2.getStringCellValue())) {
return false;
}
}
Cell cell3 = row.getCell(3);
if (cell3 == null) {
return false;
} else {
cell3.setCellType(Cell.CELL_TYPE_STRING);
if (StringUtils.isBlank(cell3.getStringCellValue())) {
return false;
}
}
Cell cell4 = row.getCell(4);
if (cell4 == null) {
return false;
} else {
cell4.setCellType(Cell.CELL_TYPE_STRING);
if (StringUtils.isBlank(cell4.getStringCellValue())) {
return false;
}
}
return true;
}
}
祝你幸福
送你一首歌 那些光——《人间世》片尾曲
附图:记录片《人间世》豆瓣9.6