java maven导入导出_Java +EasyUI+SpringMvc实现Excle导入导出(上)

标签:

前言

先介绍下项目的开发环境,采用java开发语言进行开发,前台UI使用的是EasyUI框架,接下来是SpringMvc与Ejb结合来开发,整个项目管理采用的是Maven管理,服务器用的是Jboss6.2.0企业版。

正文

配置

对于Excel上传文件我们首先要引入commons-fileupload.jar包,由于我们的项目采用Maven进行项目管理,所以对于jar包的引入是在pom.xml文件中添加的依赖,添加commons-fileupload.jar的坐标:

commons-fileupload

commons-fileupload

1.3.1

由于后台代码的ExcelUtil工具类所在的项目不再当前的web项目中,而在itoo-exam-too项目中,因此我们需要将ExcelUtil工具类所在的itoo-exam-too项目进行引入,也就是在maven的pom.xml文件中添加itoo-exam-too的项目依赖:

com.tgb

itoo-exam-tool

0.0.1-SNAPSHOT

接下来SpringMvc的配置文件也需要做些配置,这样可以将前台文件进行解析,传到后台:

class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

具体实现

Excel的导入我们可以进行分解,包括三个步骤:

1.上传Excel文件

2.读取Excle文件中的数据,将Excle数据转化成list集合

3.将list集合存入数据库

首先我们开始看我们前台easyui上传Excle文件的代码,采用的是一个form表单,表单里包含一个easyui的上传文件的控件easyui-filebox:

选择文件: 

导入题库

前台代码比较简单,我们使用是EasyUI的filebox进行文件上传,这里需要注意的是form表单的enctype属性必须是""multipart/form-data",接下来我们用js代码对Excle的格式进行基本的校验,然后将文件提交到后台Controller,

 //导入excel

function uploadExcel(){

//得到上传文件的全路径

var fileName= $('#uploadExcel').filebox('getValue');

//获取题型

var id= $('#questionType').combobox('getValue');

var questionTypes=encodeURI(id);

if(questionTypes !=""){

//进行基本校验

if(fileName==""){

$.messager.alert('提示','请选择上传文件!','info');

}else{

//对文件格式进行校验

var d1=/\.[^\.]+$/.exec(fileName);

if(d1==".xls"){

//获取题型

var id= $('#questionType').combobox('getValue')

var questionTypes=encodeURI(id);

//获取课程

var courseTypeId =$('#courseTypeId').combobox('getValue')

var courseType=encodeURI(courseTypeId);

//提交表单

document.getElementById("questionTypesManage").action="${pageContext.request.contextPath}/leadtoQuestionTypes/leadInExcelQuestionBank?questionType="+questionTypes+"&courseType="+courseType;

document.getElementById("questionTypesManage").submit();

$.messager.alert('提示','操作成功!','info');

}else{

$.messager.alert('提示','请选择xls格式文件!','info');

$('#uploadExcel').filebox('setValue','');

}

}

}else{

$.messager.alert('提示','请选择课程题型!','info');

}

}

前台代码完毕以后,我们接下来看controller的代码,这里需要注意的是,前台easyui的filebox的name标签的名字是作为参数传到后台的,后台以流的形式接收到前台传过来的excle文件,我们使用Excle工具类将Excle转换成list集合,最后将list集合保存到数据库中。我们看下后台的代码实现:

/**

* 导入题库Excel

*

* @param uploadExcel 上传的excel文件

*

* @param request 请求

*

* @param resposne 响应

*

* @throws UnsupportedEncodingException 编码异常

*

*/

@RequestMapping("/leadInExcelQuestionBank")

public String leadInExcelQuestionBank(

@RequestParam("uploadExcel") CommonsMultipartFile uploadExcel,

HttpServletRequest request, HttpServletResponse response)

throws UnsupportedEncodingException {

// 获取前台传来的题型和课程

String questionType = request.getParameter("questionType").trim();

String courseType = request.getParameter("courseType").trim();

String questionTypeNameId = new String(

questionType.getBytes("iso-8859-1"), "utf-8");

String courseTypeId = new String(courseType.getBytes("iso-8859-1"),

"utf-8");

InputStream in;

boolean flag = false;

try {

// 获取前台exce的输入流

in = uploadExcel.getInputStream();

//获取sheetName名字

String sheetName = leadToInQuestionTypesManageBean.getSheetName(questionTypeNameId);

// excel的表头与文字对应,获取excel表头

LinkedHashMap map = leadToInQuestionTypesManageBean.getMapLeadInExcelQuestionBank(questionTypeNameId);

//获取组合excle表头数组,防止重复用的

String[] uniqueFields =leadToInQuestionTypesManageBean.getUniqueFields(questionTypeNameId);

//获取需要导入的具体的表

Class class1=leadToInQuestionTypesManageBean.getClassName(questionTypeNameId);

//excel转化成的list集合

List list = null;

try {

//调用excle共用类,转化成list

list=ExcelUtil.excelToList(in, sheetName, class1, map, uniqueFields);

} catch (ExcelException e) {

e.printStackTrace();

}

//保存实体集合

flag= leadToInQuestionTypesManageBean.leadInExcelQuestionBank(questionTypeNameId, courseTypeId, list);

} catch (IOException e1) {

e1.printStackTrace();

}

System.out.println("执行结果:" + flag);

return "/LeadToQuestionTypeBank";

}

到这里关于Excle导入就已经完成了,关于Excle的导出,和ExcelUtil类的编写,我们下篇文章继续,敬请期待。

标签:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值