在自己使用easyUi做excel导入导出功能时,结合使用jexcelapi的API做的excel导入导出功能
在pom.xml文件加入
commons-fileupload
commons-fileupload
1.3.1
commons-io
commons-io
2.4
commons-codec
commons-codec
1.9
net.sourceforge.jexcelapi
jxl
2.6.12
function uploadExcel() {
$("#uploadExcel").form('submit');
}
/* 配置导入框 */
$("#uploadExcel").form({
type : 'post',
url : '${pageContext.request.contextPath}/LeadToExcel/LeadInUser',
dataType : "json",
onSubmit: function() {
var fileName= $('#excel').filebox('getValue');
//对文件格式进行校验
var d1=/\.[^\.]+$/.exec(fileName);
if (fileName == "") {
$.messager.alert('Excel批量用户导入', '请选择将要上传的文件!');
return false;
}else if(d1!=".xls"){
$.messager.alert('提示','请选择xls格式文件!','info');
return false;
}
$("#booten").linkbutton('disable');
return true;
},
success : function(result) {
var result = eval('(' + result + ')');
if (result.success) {
$.messager.alert('提示!', '导入成功','info',
function() {
$("#booten").linkbutton('enable');
$('#importExcel').dialog('close');
$('#user').datagrid('reload');
});
} else {
$.messager.confirm('提示',"导入失败!");
$("#booten").linkbutton('enable');
}
}
});
@RequestMapping("/LeadInUser")
public String leadInExcel(HttpServletResponse response,@RequestParam(value="excel")CommonsMultipartFile excel) throws Exception{
JSONObject jso=new JSONObject();
InputStream in;
try {
// 获取前台exce的输入流
in = excel.getInputStream();
//excel的表头与文字对应,获取excel表头
LinkedHashMap map = leadToExcelController.getLeadToFiledPublicQuestionBank();
//获取组合excle表头数组,防止重复用的
String[] uniqueFields =new String[] {"名称","密码","年龄" };
//获取需要导入的具体的表
User user = new User();
//excel转化成的list集合
ArrayList list = null;
try {
//调用excle共用类,转化成list
list=ExcelUtil.excelToList(in, user.getClass(), map, uniqueFields);
} catch (ExcelException e) {
e.printStackTrace();
}
//保存实体集合
int num = this.userService.batchInsert(list);
if(num > 0){
jso.put("success", true);
ResponseUtil.write(response, jso);
}else{
jso.put("success", false);
ResponseUtil.write(response, jso);
}
} catch (IOException e1) {
e1.printStackTrace();
jso.put("success", false);
ResponseUtil.write(response, jso);
}
return null;
}
导出功能
{
iconCls : 'icon-print',
text : '导出',
handler : function() {
var grid = $('#user');
var options = grid.datagrid('getPager').data("pagination").options;
var page = options.pageNumber;//当前页数
var rows = options.pageSize;//每页的记录数(行数)
var url = '${pageContext.request.contextPath}/LeadToExcel/LeadToUser?rows='+rows+'&page='+page
window.location.href=url;
}
}
@RequestMapping("/LeadToUser")
public String leadToExcelQuestionBank(HttpServletResponse response,@RequestParam(value="page") String page,@RequestParam(value="rows") String rows) throws Exception {
JSONObject jso=new JSONObject();
try {
// excel表格的表头,map
LinkedHashMap fieldMap = leadToExcelController.getLeadToFiledPublicQuestionBank();
// excel的sheetName
String sheetName = "用户";
// excel要导出的数据
ArrayList list = this.userService.getAllUser((Integer.parseInt(page)-1)*Integer.parseInt(rows),Integer.parseInt(rows));
// 导出
if (list == null || list.size() == 0) {
jso.put("success", false);
ResponseUtil.write(response, jso);
}else {
//将list集合转化为excle
ExcelUtil.listToExcel(list, fieldMap, sheetName, response);
}
return null;
} catch (ExcelException e) {
e.printStackTrace();
jso.put("success", false);
ResponseUtil.write(response, jso);
}
return null;
}