得到上传的MultipartFile 文件转换成FileInputStream ,将输入流传入importExcelX解析返回List,然后遍历插入数据库。
ExportDataController.java
@RequestMapping(value = "ImportExcelProduct.do",method = RequestMethod.POST)
public String ImportExcelProduct(@RequestParam("files") MultipartFile file){
BasicProductMsg basicProductMsg = new BasicProductMsg();
String fileName =file.getOriginalFilename() ;
String returnStr = StringUtil.RETURN_ERROR_DATA_FORMAT;//这个是一个返回提示内容:上传格式错误!
//文件不为空并且后缀等于xlsx就将上传的文件写入临时的文件夹中
if (!file.isEmpty() && fileName.endsWith(".xlsx")) {
try {
//得到文件的输入流
FileInputStream fis = (FileInputStream) file.getInputStream();
//解析Excel后得到的 List集合
List
ImportExxcelUtil.java
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
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 com.alibaba.fastjson.JSON;
import com.sd.xm003.model.User;
/**
* 导入Excel表格的工具类
*/
public class ImportExcelUtil {
/**
* POI:解析Excel文件中的数据并把每行数据封装成一个实体
* @param filePath 文件绝对路径
* @return List
实体类
import java.util.Date;
public class BasicProductMsg {
private Integer id;
private String productNo;
private String productName;
private String figureNo;
private String model;
private String unit;
private String venderFigureNo;
private String customer;
private String remarks;
private String companyFlag;
private String operator;
private String operatingTime;
private String flag;
private Integer minStock;
private Integer maxStock;
private Integer unitPackageNumber;
private Integer number;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getProductNo() {
return productNo;
}
public void setProductNo(String productNo) {
this.productNo = productNo == null ? null : productNo.trim();
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName == null ? null : productName.trim();
}
public String getFigureNo() {
return figureNo;
}
public void setFigureNo(String figureNo) {
this.figureNo = figureNo == null ? null : figureNo.trim();
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model == null ? null : model.trim();
}
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit == null ? null : unit.trim();
}
public String getVenderFigureNo() {
return venderFigureNo;
}
public void setVenderFigureNo(String venderFigureNo) {
this.venderFigureNo = venderFigureNo == null ? null : venderFigureNo.trim();
}
public String getCustomer() {
return customer;
}
public void setCustomer(String customer) {
this.customer = customer == null ? null : customer.trim();
}
public String getRemarks() {
return remarks;
}
public void setRemarks(String remarks) {
this.remarks = remarks == null ? null : remarks.trim();
}
public String getCompanyFlag() {
return companyFlag;
}
public void setCompanyFlag(String companyFlag) {
this.companyFlag = companyFlag == null ? null : companyFlag.trim();
}
public String getOperator() {
return operator;
}
public void setOperator(String operator) {
this.operator = operator == null ? null : operator.trim();
}
public String getOperatingTime() {
return operatingTime;
}
public void setOperatingTime(String operatingTime) {
this.operatingTime = operatingTime;
}
public String getFlag() {
return flag;
}
public void setFlag(String flag) {
this.flag = flag == null ? null : flag.trim();
}
public Integer getMinStock() {
return minStock;
}
public void setMinStock(Integer minStock) {
this.minStock = minStock;
}
public Integer getMaxStock() {
return maxStock;
}
public void setMaxStock(Integer maxStock) {
this.maxStock = maxStock;
}
@Override
public String toString() {
return "BasicProductMsg{" +
"id=" + id +
", productNo='" + productNo + '\'' +
", productName='" + productName + '\'' +
", figureNo='" + figureNo + '\'' +
", model='" + model + '\'' +
", unit='" + unit + '\'' +
", venderFigureNo='" + venderFigureNo + '\'' +
", customer='" + customer + '\'' +
", remarks='" + remarks + '\'' +
", companyFlag='" + companyFlag + '\'' +
", operator='" + operator + '\'' +
", operatingTime='" + operatingTime + '\'' +
", flag='" + flag + '\'' +
", minStock=" + minStock +
", maxStock=" + maxStock +
'}';
}
public Integer getUnitPackageNumber() {
return unitPackageNumber;
}
public void setUnitPackageNumber(Integer unitPackageNumber) {
this.unitPackageNumber = unitPackageNumber;
}
public Integer getNumber() {
return number;
}
public void setNumber(Integer number) {
this.number = number;
}
}
导入的文件列头要和实体类一样,如果要列头是中文在就在帮助类58行下面写一个判断如果 headers[j] =“苹果”,name head = "apple"