Apache POI实现数据的Excel导入

得到上传的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 list = ImportExcelUtil.importExcelX(fis, basicProductMsg);
                for (Object objects : list) {
                    BasicProductMsg bpmo = (BasicProductMsg) objects;
                    //判断新增的记录数据表是否已经包含
                    if(basicProductMsgService.DetctBasicProductMsg(bpmo).size()!= 0){
                        continue;
                    }
                    basicProductMsgService.addBasicProductMsg(StringUtil.objectToMap(objects));
                }
                //关闭输入流
                fis.close();
                returnStr = "Data import success";
            } catch (Exception e) {
                e.printStackTrace();
                returnStr = "Data import failure";
            }
        }
        return  StringUtil.returnPopover(returnStr,"/mainMate/productInform?a");
    } 
  

 

 

 

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 Excel中数据封装实体的集合 
	 */
	public static List importExcelX(FileInputStream fis,Object object){
		
		List objects =new ArrayList();
		Workbook wookbook = null;
		Cell cell = null;
		String json ="";
		//FileInputStream fis =null;
		
		//创建Excel工作簿
		try {
			//得到文件输入流对象
			//fis = new FileInputStream(filePath);
			//2007版本的excel,用.xlsx结尾
			wookbook = new XSSFWorkbook(fis);//得到工作簿
			fis.close();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		
		//得到第一个工作表
		Sheet sheet = wookbook.getSheetAt(0);
		
        //获得表头
        Row rowHead = sheet.getRow(0);
        //得到表头列数
        int heads =rowHead.getPhysicalNumberOfCells();
        String[] headers = new String[heads];//表头
        //遍历表头存放在数组中
        for(int i =0; i objects =importExcelX(filePath,user);
		List users =new ArrayList();
		for (Object object : objects) {
			users.add((User)object);
		}
		for (User user2 : users) {
			System.out.println(user2.toString());
		}
	}
}
 
  

 

 

实体类

 

 

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"

 

 

 

 

 

 

 

你可能感兴趣的:(Apache POI实现数据的Excel导入)