Poi-18、导入excel数据

前端代码

<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>Basic DataGrid - jQuery EasyUI Demo</title>
	<link rel="stylesheet" type="text/css" href="jquery-easyui-1.3.3/themes/default/easyui.css">
	<link rel="stylesheet" type="text/css" href="jquery-easyui-1.3.3/themes/icon.css">
	<script type="text/javascript" src="jquery-easyui-1.3.3/jquery.min.js"></script>
	<script type="text/javascript" src="jquery-easyui-1.3.3/jquery.easyui.min.js"></script>
	<script type="text/javascript" src="jquery-easyui-1.3.3/locale/easyui-lang-zh_CN.js"></script>
	<script>
		function openUploadFileDialog() {
			$("#dlg2").dialog('open').dialog('setTitle','批量导入用户数据');
		}
		
		function downloadTemplate() {
			window.open('template/userExporTemplate.xls')
		}
		
		function uploadFile() {
			$("#uploadForm").form("submit",{
				success:function(result){
					console.log(result);
					var result=eval('('+result+')');
					console.log('使用eval函数的响应结果:',result);
					if(result.errorMsg){
						$.messager.alert("系统提示",result.errorMsg);
					}else{
						$.messager.alert("系统提示","上传成功");
						$("#dlg2").dialog("close");
						$("#dg").datagrid("reload");
					}
				}
			});
		}
		
	</script>
</head>
<body>
	<table id="dg" title="用户管理" class="easyui-datagrid" style="width:700px;height:365px"
            url="user/list.do"
            toolbar="#toolbar" pagination="true"
            rownumbers="true" fitColumns="true" singleSelect="true">
        <thead>
            <tr>
            	<th field="id" width="50">编号</th>
                <th field="username" width="50">姓名</th>
                <th field="password" width="50">密码</th>
                <th field="age" width="50">年龄</th>
            </tr>
        </thead>
    </table>
    <div id="toolbar">
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newUser()">添加用户</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editUser()">编辑用户</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="deleteUser()">删除用户</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-export" plain="true" onclick="exportUser()">导出用户</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-export" plain="true" onclick="exportUser2()">使用模板导出用户</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-import" plain="true" onclick="openUploadFileDialog()">使用模板导出用户</a>
    </div>
	
	<!-- 批量导入的对话框start-->
		<div id="dlg2" class="easyui-dialog" style="width:400px;height:180px;padding:10px 20px"
            closed="true" buttons="#dlg-buttons2">
        <form id="uploadForm" action="user/import.do" method="post" enctype="multipart/form-data">
        	<table cellspacing="10px;">
        		<tr>
        			<td>下载模板</td>
        			<td><a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="downloadTemplate()">用户user模板</a></td>
        		</tr>
        		<tr>
        			<td>导入模板</td>
        			<td><input type="file" name="userUploadFile"></td>
        		</tr>
        	</table>
        </form>
	</div>
    
	<div id="dlg-buttons2">
		<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="uploadFile()">确定导入</a>
		<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg2').dialog('close')">关闭</a>
	</div>
	
	<!-- 批量导入的对话框end-->
	
</body>
</html>

后台代码

1,控制层

	@RequestMapping("/import")
	public String upload(MultipartFile userUploadFile,HttpServletResponse response)throws Exception{
		InputStream inputStream = userUploadFile.getInputStream();
		POIFSFileSystem fs=new POIFSFileSystem(inputStream);
		HSSFWorkbook wb=new HSSFWorkbook(fs);
		HSSFSheet hssfSheet=wb.getSheetAt(0);  // 获取第一个Sheet页
		if(hssfSheet!=null){
			for(int rowNum=1;rowNum<=hssfSheet.getLastRowNum();rowNum++){
				HSSFRow hssfRow=hssfSheet.getRow(rowNum);
				if(hssfRow==null){
					continue;
				}
				User user=new User();
				user.setId(Integer.valueOf(ExcelUtil.formatCell(hssfRow.getCell(0)).substring(0, ExcelUtil.formatCell(hssfRow.getCell(0)).indexOf("."))));
				user.setUsername(ExcelUtil.formatCell(hssfRow.getCell(1)));
				user.setPassword(ExcelUtil.formatCell(hssfRow.getCell(2)));
				user.setAge(Integer.valueOf(ExcelUtil.formatCell(hssfRow.getCell(3)).substring(0, ExcelUtil.formatCell(hssfRow.getCell(3)).indexOf("."))));
				userService.insert(user);
			}
		}
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("success", "true");
		return JSON.toJSONString(map);
	}

2,工具类ExcelUtil

public static String formatCell(HSSFCell hssfCell){
		if(hssfCell==null){
			return "";
		}else{
			if(hssfCell.getCellType()==HSSFCell.CELL_TYPE_BOOLEAN){
				return String.valueOf(hssfCell.getBooleanCellValue());
			}else if(hssfCell.getCellType()==HSSFCell.CELL_TYPE_NUMERIC){
				return String.valueOf(hssfCell.getNumericCellValue());
			}else{
				return String.valueOf(hssfCell.getStringCellValue());
			}
		}
	}

遇到的问题

1,使用列表显示姓名,乱码,但springmvc配置了中文乱码过滤器,找了下资料,发现这过滤器,只是过滤请求参数的字符编码格式。响应想通过response.setContentType("application/json;charset=UTF-8");无效。只能在@RequestMapping(value = "/list",produces="application/json;charset=UTF-8")才有用
2,批量导入时,到数据库是乱码。在db.properties文件jdbc.url=jdbc:mysql://localhost:3306/gj1?characterEncoding=utf-8
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值