springMVC+mybatis 把excel文件导入oracle数据库

package cn.ffcs.system.controller;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import jxl.*;
import jxl.read.biff.BiffException;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.dubbo.config.annotation.Reference;

import cn.ffcs.system.common.StringUtils;
import cn.ffcs.uam.service.StatisticsService;

/**
 * 综合统计数据导入管理
 * */
@Controller
@RequestMapping(value="/admin/statistics/excel")
public class StatisticsImportController {
	

	@Reference(registry = "real-registry")
	private StatisticsService statisticsService;
	
	private String[] statisticsCols = {
            "TITLE","UNIT","S1","S2","S3", "S4","STYPE",
            "REMARK","SYEAR","SMONTH","OPDATE","TREE_ID"
    };
	SimpleDateFormat formate = new SimpleDateFormat("yyyy/MM/dd");
	private int listLimits = 200;
	
	@RequestMapping(value="/doImport", method=RequestMethod.POST)
    @SuppressWarnings({ "rawtypes", "unchecked"})
    public String doImport(HttpSession session, HttpServletRequest request,
    		@RequestParam("excelFile") MultipartFile excelFile, ModelMap map) {

        String returnPage = "/statistics/importResult.ftl";
		Workbook rwb = null;
		WorkbookSettings wbs = new WorkbookSettings();
		//String rootFolder = Constants.RESOURSE_SAVE_ROOT_PATH+"statistics/import-failure";
		String path = request.getSession().getServletContext().getRealPath("upload");  
		String fileName = excelFile.getOriginalFilename();
		wbs.setEncoding("UTF-8");
		//wbs.setWriteAccess(null);
		//wbs.setUseTemporaryFileDuringWrite(true);
		File targetFile = new File(path, fileName); 
		if(!targetFile.exists()){  
            targetFile.mkdirs();  
        }
		try {  
			excelFile.transferTo(targetFile);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }
		//wbs.setTemporaryFileDuringWriteDirectory(targetFile);//临时文件夹的位置
		try{
            rwb = Workbook.getWorkbook(targetFile, wbs);
            Sheet sheet = rwb.getSheet(0); 
            if(sheet.getRows()<2){
            	map.put("tipErrMessage", "没有需要导入的数据");
            	return returnPage;
            }
            List list = null;
            int rows = sheet.getRows();
            int index = 0;
            for(int i = 0; i < rows; i++) {
                String checkStatus = "";
                Cell[] cells = sheet.getRow(i);
                if(!isEmptyRow(cells)) {//检查非空行的数据
                    checkStatus = checkRow(cells); //检查数据有效性
                }
                if(new StringUtils().isEmpty(checkStatus)) continue;
                else {
                    map.addAttribute("tipErrMessage", "导入失败,第"+(i+1)+"行,"+checkStatus);
                    return returnPage;
                }
            }

            //int records = this.getRightRows(sheet) - 1;//正确记录数 = 总记录数 - 空行 - 表头
            for(int i = 1; i < sheet.getRows(); i++){
            	 Cell[] cells = sheet.getRow(i);
                 if(index == 0) list = new ArrayList();
                 if(!isEmptyRow(cells)) {//跳过空行
                     Map rowMap = new HashMap<String,String>();
                     for(int j = 0; j < statisticsCols.length; j++) {
                         String colName = statisticsCols[j];
                         String colValue = "";
                         try {
                             Cell cell = sheet.getCell(j,i);
                             if(cell.getType() == CellType.DATE){
                                 DateCell dateCell = (DateCell)cell;
                                 colValue = formate.format(dateCell.getDate());
                             } else if(cell.getType() == CellType.EMPTY) {
                                 colValue = "";
                             } else {
                                 colValue = cell.getContents().trim();
                                 colValue = colValue.replaceAll("\"","");
                             }
                             if("OPDATE".equals(colName)) { //检查日期
                                 colValue = colValue.toString().replaceAll("-","/");
                                 colValue = formate.format(formate.parse(colValue.toString()));
                             }
                             rowMap.put(colName, colValue);
                         }catch(Exception e){
                         }
                     }
                     list.add(rowMap);
                     index++;
                 }
            }
            rwb.close();
            if(list.size() > 0){
	            int temp = statisticsService.insertByExcel(list);
	            map.addAttribute("tipErrMessage", temp !=0 ? "导入成功,共导入"+temp+"条数据":"导入失败");
            }
            
        }catch(IOException e){
        	return returnPage;
        }catch(BiffException e) {
        	map.addAttribute("tipErrMessage", "excel版本错误!请使用2003版excel");
            return returnPage;
        }
        
		return return
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值