导入到数据库关键在于将excel文件中的数据读取为一个list集合
1.jsp
<%@page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@include file="../../../jsp/title.jsp" %>
2.controller 控制转向层
package com.youge.shop.action;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
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.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.youge.api.service.ProductCenterApiService;
import com.youge.shop.service.ShopService;
import com.youge.util.JSONSerializer;
import com.youge.util.PageUtil;
import com.youge.util.StringTools;
import com.youge.util.UploadUtil;
import com.youge.util.WebUtil;
/**
* 经销商管理
*
*/
@Controller
@RequestMapping("shop")
public class ShopController {
private final static Log log = LogFactory.getLog(ShopController.class);
@Autowired
private ShopService shopService;
@Autowired
private ProductCenterApiService productCenterService;
/**
* 导入excel
*
* @return
*/
@RequestMapping("excel")
public String excel(HttpServletRequest request, HttpServletResponse response) {
// 验证是否登录和是否有权限(跳转页面专用)
String returnUrl = WebUtil.getReturn(request, "");
// 如果长度大于0则验证失败
if (returnUrl.length() > 0) {
return returnUrl;
}
try {
} catch (Exception e) {
log.error("导入excel:" + e.getMessage());
}
return "shop/excel2";
}
// 导入excel
@RequestMapping(value = "/import", method = RequestMethod.POST)
@ResponseBody
public String importExcel(@RequestParam(value = "file", required = false) MultipartFile file,
HttpServletRequest request, HttpServletResponse response) {
String ret_data = "";
// 返回值(ajax专用)
Map returnMap = WebUtil.getReturnMap_A(request, "");
// 验证是否登录和是否有权限
if ((boolean) returnMap.get(WebUtil.RETURM_ERR)) {
return JSONSerializer.serialize(returnMap);
}
try {
String result = shopService.readExcelFile(file);
returnMap.put("describe", result);
if (result.equals("上传成功")) {
returnMap.put("msg", true);
} else {
returnMap.put("msg", false);
}
} catch (Exception e) {
log.error(e.getMessage());
}
//4.map转json
ret_data = JSONSerializer.serialize(returnMap);
return ret_data;
}
}
3.service层
public String readExcelFile(MultipartFile file) {
String result = "";
// 创建处理EXCEL的类
ReadExcel2 readExcel = new ReadExcel2();
// 解析excel,获取上传的事件单
List
4.最后是读取文件工具类
package com.youge.shop.service;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
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 org.springframework.web.multipart.MultipartFile;
/**
*
* @author hewangtong
*
*/
public class ReadExcel2 {
// 总行数
private int totalRows = 0;
// 总条数
private int totalCells = 0;
// 错误信息接收器
private String errorMsg;
// 构造方法
public ReadExcel2() {
}
// 获取总行数
public int getTotalRows() {
return totalRows;
}
// 获取总列数
public int getTotalCells() {
return totalCells;
}
// 获取错误信息
public String getErrorInfo() {
return errorMsg;
}
/**
* 读EXCEL文件,获取信息集合
*
* @param fielName
* @return
*/
public List
5.本文使用到的jar包
(1)dom4j-1.6.1.jar
(2)poi-3.8-20120326.jar
(3)poi-examples-3.8-20120326.jar
(4)poi-excelant-3.8-20120326.jar
(5)poi-ooxml-3.8-20120326.jar
(6)poi-ooxml-schemas-3.8-20120326.jar
(7)poi-scratchpad-3.8-20120326.jar
(8)xmlbeans-2.6.0.jar
导入时可能遇到的问题:电话号码格式被转换成了1.3E的格式存入数据库,使用以下读取excel的格式即可
if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC){ //数字
if(String.valueOf(cell.getNumericCellValue()).indexOf("E")==-1){
map.put("shop_phone", String.valueOf(cell.getNumericCellValue()));
}else {
map.put("shop_phone", new DecimalFormat("#").format(cell.getNumericCellValue()));
}
}