spring boot-整合poi Excel表单导入实现

本文介绍如何使用Apache POI库解析并导入Excel数据到Java应用程序中,包括不同数据类型的转换及数据库存储过程。

首先了解一下: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>

测试:

结果数据库:

测试成功!!

总结其实表格导入并不难:有几种转换的格式要记住

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值