POI解析Excel导入数据到mysql

本文介绍了一种使用Java处理Excel文件的方法,通过读取Excel表格数据并将其转换为Map集合,便于进一步的数据处理和存储。文章详细展示了如何解析不同类型的单元格数据,包括数值、日期、字符串等,并提供了完整的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

所用jar poi-3.7.jar  poi-oxml-3.7.jar  poi-oxml-schemas-3.7.jar 
        xmlbeans-2.6.0.jar dom4j-1.6.1.jar

代码 
 
@RequestMapping("/importExcel")
@ResponseBody
public Map importExcel(HttpServletRequest request, MultipartFile file) throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();
		Map<String, Object> param = new HashMap<String, Object>();
		List<Map<String, Object>> list = getList(file);
		if (list.size() > 0) {
			try {
				for (int i = 0; i < list.size(); i++) {
					param = list.get(i);
					visitService.saveStatAdv(param);
				}
				result.put("msg", "保存成功");
			} catch (Exception e) {
				result.put("msg", "保存失败");
				e.printStackTrace();
			}
		}
		return result;
	}

 private List<Map<String, Object>> getList(MultipartFile file) throws Exception {
		// TODO Auto-generated method stub
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		InputStream stream = file.getInputStream();
//			String ext=file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
		Workbook wb = WorkbookFactory.create(stream);
		Sheet sheet = wb.getSheetAt(0);

		for (Row row : sheet) {
			int w = row.getRowNum();
			if (w >= 1) {
				Map<String, Object> map = new HashMap<String, Object>();
				for (Cell cell : row) {
					int j = cell.getColumnIndex();
					
					Object obj;
					switch (cell.getCellType()) {
					case Cell.CELL_TYPE_NUMERIC:// 数值型
						if (HSSFDateUtil.isCellDateFormatted(cell)) {// date类型
							obj = cell.getDateCellValue();
						} else {// 纯数字
							obj = cell.getNumericCellValue();
						}
						break;
					case Cell.CELL_TYPE_STRING:// 字符串型
						obj = cell.getRichStringCellValue().getString();
						break;
					case Cell.CELL_TYPE_BOOLEAN:// 布尔
						obj = cell.getBooleanCellValue();
						break;
					case Cell.CELL_TYPE_FORMULA:// 公式型
						obj = cell.getCellFormula();
						break;
					case Cell.CELL_TYPE_BLANK:// 空值
						obj = "";
						break;
					case Cell.CELL_TYPE_ERROR: // 故障
						obj = "";
						break;
					default:
						obj = "";
					}
					if (j == 0) {
						map.put("date", obj);
					} else if (j == 1) {
						map.put("channel", obj);
					} else if (j == 2) {
						map.put("product", obj);
					} else if (j == 3) {
						map.put("qtyview", obj);
					} else if (j == 4) {
						map.put("qtyrequest", obj);
					} else if (j == 5) {
						map.put("qtyshow", obj);
					} else if (j == 6) {
						map.put("qtyadshow", obj);
					} else if (j == 7) {
						map.put("ratecover", String.format("%.4f", obj));
					} else if (j == 8) {
						map.put("qtyclk", obj);
					} else if (j == 9) {
						map.put("rateclk", String.format("%.4f", obj));
					} else if (j == 10) {
						map.put("cpc", obj);
					} else if (j == 11) {
						map.put("ecpm", String.format("%.4f", obj));
					} else if (j == 12) {
						map.put("ratevisible", String.format("%.4f", obj));
					} else if (j == 13) {
						map.put("revenue", obj);
					}
				}
				list.add(map);
			}
		}
		return list;
	}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值