首先了解一下:POI 中的CellType类型以及值的对应关系
CellType | 类型 | 值 |
---|---|---|
CELL_TYPE_NUMERIC | 数值型 | 0 |
CELL_TYPE_STRING | 字符串型 | 1 |
CELL_TYPE_FORMULA | 公式型 | 2 |
CELL_TYPE_BLANK | 空值 | 3 |
CELL_TYPE_BOOLEAN | 布尔型 | 4 |
CELL_TYPE_ERROR | 错误 | 5 |
记住几种转换的格式:string 、Integer、date、double 掌握这几种转换类型后可以不变应万变。
//字符串类型
if(row.getCell(5)!=null){//第7列
row.getCell(5).setCellType(Cell.CELL_TYPE_STRING);
Type.setLng(row.getCell(5).getStringCellValue());
}
//Integer类型
if(row.getCell(6)!=null){//第8列
row.getCell(6).setCellType(Cell.CELL_TYPE_STRING);
Type.setCoordinate(Integer.parseInt(row.getCell(6).getStringCellValue()));
}
// 转换为日期类型date类型
if(row.getCell(7)!=null){//第8列
row.getCell(7).setCellType(Cell.CELL_TYPE_NUMERIC);
Type.setInstalltime( HSSFDateUtil.getJavaDate(row.getCell(7).getNumericCellValue()));
}
//double类型
if(row.getCell(14)!=null){//第15列
row.getCell(14).setCellType(Cell.CELL_TYPE_NUMERIC);
Type.setX(row.getCell(14).getNumericCellValue());
}
第一步:添加Dependence
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.8</version>
</dependency>
第二步:编写Controller类
package com.hot.analysis.controller.poi;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.hot.analysis.bean.dc.deviceType;
import com.hot.analysis.service.dc.DCService;
@RestController
public class Excelfile {
@Autowired
private DCService dcService;
//表单导入
@RequestMapping("/dc/Excelfile")
public String upload(MultipartFile file, HttpServletRequest request) {
try {
List<deviceType> typeLists = new ArrayList<deviceType>();
System.out.println("开始");
//使用POI解析Excel文件
//如果是xls,使用HSSFWorkbook;2003年的excel 如果是xlsx,使用XSSFWorkbook 2007年excel
HSSFWorkbook workbook = new HSSFWorkbook(file.getInputStream());
//根据名称获得指定Sheet对象
HSSFSheet hssfSheet = workbook.getSheetAt(0);
for (Row row : hssfSheet) {
deviceType Type = new deviceType();
int rowNum = row.getRowNum();
if(rowNum == 0){//跳出第一行 一般第一行都是表头没有数据意义
continue;
}
if(row.getCell(1)!=null){//第2列数据
row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
Type.setName(row.getCell(1).getStringCellValue());
}
if(row.getCell(2)!=null){//第3列
row.getCell(2).setCellType(Cell.CELL_TYPE_STRING);
Type.setCode(row.getCell(2).getStringCellValue());
}
// 转换为Integer类型
if(row.getCell(3)!=null){//第4列
row.getCell(3).setCellType(Cell.CELL_TYPE_STRING);
Type.setAdduserid(Integer.parseInt(row.getCell(3).getStringCellValue()));
}
// 转换为日期类型
if(row.getCell(4)!=null){//第5列
row.getCell(4).setCellType(Cell.CELL_TYPE_NUMERIC);
Type.setAddtime( HSSFDateUtil.getJavaDate(row.getCell(4).getNumericCellValue()));
}
typeLists.add(Type);
}
//调用service执行保存typeLists的方法
dcService.saveExcelList(typeLists);
}catch(Exception e){
e.printStackTrace();
}
return "操作成功!";
}
}
第三步:编写service
void saveExcelList(List<deviceType> typeLists);
第四步:编写serviceImpl
@Override
public void saveExcelList(List<deviceType> typeLists) {
for (deviceType type : typeLists) {
//调用mapper的保存方法
dcmapper.insertSelective(type);
}
}
第五步:编写Mapper
void insertSelective(deviceType type);
第六步:编写Mapper.xml
<insert id="insertSelective">
INSERT INTO t_module(name,code,adduserid,addtime) VALUES(#{name},#{code},#{adduserid},#{addtime});
</insert>
测试:
结果数据库:
测试成功!!
总结其实表格导入并不难:有几种转换的格式要记住