您的位置:首页 > 编程语言 > Java开发

java excel文件导入

2017-11-01 21:00 218 查看
很久没有写博客了,关于写这个导入,真是把我给痛苦死了。有找过一些资料,但是不是怎么全,后来,经历了种种,终于写好了。对于这种菜鸟的级别,决定要把它记录下来同时分享一下。好了,直接进入正题。

首先上效果图:



1、先讲讲思路吧;首先这个批量导入功能呢!由于需求是需要添加页面显示的这些字段,用来与excl表格里面的数据一起存入数据库。所以页面要显示这些字段以用来传到后台一起存入数据库。

2、如何获取数据呢,当然先要获取这个文件,然后读取到文件里面的数据,读到了之后呢,就把他们存入数据库。总体的流程就是这样。下面看代码。

先从jsp吧,直观一点: 这是点击按钮

<button type="button" class="btn btn-success" href="#export-account" data-toggle="modal"
data-dismiss="modal">
<i class="fa fa-plus-square">xls导入渠道商户</i>
</button>


弹出窗:

<div class="modal" id="export-account" role="dialog" data-backdrop="static">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" onclick="resetForm('add_acct_export');" data-dismiss="modal"
aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h5 class="modal-title">导入渠道商户</h5>
</div>
<div class="modal-body">
<form class="form-horizontal" id="add_acct_export">
<div class="box-body">
<div class="box-body">

<div class="form-group">
<label for="a_autoSync" class="col-sm-2 control-label">渠道列表</label>
<div class="col-sm-4">
<select class="form-control pull-left" id="cup_channel_id_export" onchange="getByCupChannelProduct(this.value,'cup_channel_product_id_export')" name="cup_channel_id_export">
</select>
</div>
<label for="a_autoSync" class="col-sm-2 control-label">产品列表</label>
<div class="col-sm-4">
<select class="form-control pull-left" id="cup_channel_product_id_export" name="cup_channel_product_id_export">
</select>
</div>
</div>
<div class="form-group">
<label for="a_autoSync" class="col-sm-2 control-label">费率公式</label>
<div class="col-sm-4">
<input name='mer_fee_formula_export' type="text" class="form-control input-sm" id="mer_fee_formula_export">
</div>
<label for="a_autoSync" class="col-sm-2 control-label">文件</label>
<div
fb8c
class="col-sm-4">
<input id="apkFile"  name="apkFile" type="file" />
</div>
</div>
</div>
</div>
<!-- /.box-body -->
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger pull-left btn-sm" onclick="resetForm('add_acct_export');" data-dismiss="modal">取消</button>
<button type="button" class="btn btn-primary btn-sm" onclick="exportCupChannelMerInfo()">导入</button>
</div>
</div>
</div>
<!-- /.modal-content -->
</div>


上传文件的插件(一定要加,还要注意位置问题,如果位置不对,也是不可以的,之前由于忘记加了,页面一直报 js上传文件那个方法is not a function):

<script src="<%=request.getContextPath() %>/plugins/ajaxfileupload/ajaxfileupload.js"></script>


js:

function exportCupChannelMerInfo(){
//获取页面的值
var cup_channel_id = $("#cup_channel_id_export").val();
var cup_channel_product_id = $("#cup_channel_product_id_export").val();
var mer_fee_formula = $("#mer_fee_formula_export").val();
$.ajaxFileUpload({
//采用拼接的方法把参数传到后台
url : 'cupchannelmerinfo/fileUpload.do?cup_channel_id='+cup_channel_id+'&cup_channel_product_id='+cup_channel_product_id+'&mer_fee_formula='+mer_fee_formula,
secureuri : false,
fileElementId : 'apkFile',// file标签的id
dataType : 'text',// 返回数据的类型
//本来是用这个方法的,但是传不过去,感兴趣的朋友可以试试,当然,你们可能也可以传
//datta:{"cup_channel_id":cup_channel_id,"cup_channel_product_id":cup_channel_product_id,"mer_fee_formula":mer_fee_formula},
type : 'POST',
success : function(data, status) {
//          var result = data.replace("<pre>","").replace("</pre>");
//
//对返回过来的数据作处理
var result = data.toString().substr(5, data.length - 11);
var json = $.parseJSON(result);
if (!json.result) {
alertErrorAtBottomRight(json.errorMsg);
} else {
var data = json.data;
alertSuccessAtBottomRight("文件导入成功,"+data);
//上传成功后,对文本框的数据清空
resetForm('add_acct_export');
//上传成功后,对弹出框作隐藏处理
$("#export-account").modal("hide");

}
},
error : function(data, status, e) {
alertErrorAtBottomRight(e);
}
});
}


接下来就是controller了:

//根据传过来的id=apkFile  拿到文件 以及获取前台页面传过来的值
@RequestMapping(value="fileUpload.do", method = RequestMethod.POST)
public @ResponseBody AjaxProcessResult fileUpload(@RequestParam(value = "apkFile") MultipartFile apkFile,HttpServletRequest request){
String cup_channel_id = request.getParameter("cup_channel_id");
String cup_channel_product_id = request.getParameter("cup_channel_product_id");
String mer_fee_formula = request.getParameter("mer_fee_formula");

long cup_channel_id_ = Long.valueOf(cup_channel_id);
long cup_channel_product_id_ = Long.valueOf(cup_channel_product_id);
//判断文件是否为空
if (!apkFile.isEmpty()) {
try {
//拿到文件 把它转成流的形式 因为读取文件是以流的形式读取的,只有把它转变成流的形式,程序才能辨别
String fileName = apkFile.getOriginalFilename();
LOG.info("fileName : "+fileName);
InputStream is = apkFile.getInputStream();

List<CupChannelMerinfo> listMer = new ArrayList<CupChannelMerinfo>();
//调用Excel工具类里读取文件的方法,此方法是以流的形式读取的
List<Row> list = ExcelUtil.getExcelRead(fileName,is, true);
//首先是读取行 也就是一行一行读,然后在取到列,遍历行里面的行,根据行得到列的值
for (Row row : list) {
Cell cell_1 = row.getCell(1);
Cell cell_2 = row.getCell(2);
//得到列的值,也就是你需要解析的字段的值
String term_code = ExcelUtil.getValue(cell_1);
String mer_code = ExcelUtil.getValue(cell_2);
//最后把你得到值装进你需要保存的数据库里,也就是其对应的实体类。由于我这边是读取文件里的两个值,加上页面传过来的,一共是五个,你读取的数据需要与你的数据库匹配,也就是你数据库里有什么值,就读取什么
CupChannelMerinfo channelMerinfo = new CupChannelMerinfo();
channelMerinfo.setCup_channel_id(cup_channel_id_);
channelMerinfo.setCup_channel_product_id(cup_channel_product_id_);
channelMerinfo.setMer_fee_formula(mer_fee_formula);
channelMerinfo.setTerm_code(term_code);
channelMerinfo.setMer_code(mer_code);
listMer.add(channelMerinfo);
}
//最后做个批量插入,也就是把你读取出来的数据保存到数据库中
int saveCount = cupChannelMerinfoService.addBatchCupChannelMerinfo(listMer, new Date(), new Date());
return AjaxProcessResult.getSuccessResult("影响条数:"+saveCount);
} catch (Exception e) {
return AjaxProcessResult.getErrorResult(e.getMessage());
}
}else{
return AjaxProcessResult.getErrorResult("获取文件失败");
}
}


什么service就贴了,service也就是写一个add方法,把你读到的数据保存到数据库。最后写的就是读取文件的工具类:

读取文件的方法
/**
* 获取解析文件行数据
* @param fileAddr : 文件地址
* @param isTitle  : 是否过滤第一行解析
* @return
* @throws Exception
*/
public static List<Row> getExcelRead(String fileName,InputStream is,boolean isTitle) throws Exception{
try {
//判断其兼容版本 调用了判断版本的方法
Workbook workbook = getWorkbook(fileName,is);
Sheet sheet = workbook.getSheetAt(0);
int count = 0;
List<Row> list = new ArrayList<Row>();
for (Row row : sheet) {
// 跳过第一行的目录
if (count == 0 && isTitle) {
count++;
continue;
}
list.add(row);
}
return list;
} catch (Exception e) {
throw e;
}
}

//判断版本的方法

public static Workbook getWorkbook(String fileName,InputStream is) throws Exception{
Workbook workbook = null;
try {
/** 判断文件的类型,是2003还是2007 */
boolean isExcel2003 = true;
if (isExcel2007(fileName)) {
isExcel2003 = false;
}

if (isExcel2003) {
workbook = new HSSFWorkbook(is);
} else {
workbook = new XSSFWorkbook(is);
}
} catch (Exception e) {
throw e;
}
return workbook;
}
得到celL值的方法:

public static String getValue(Cell cell){
if(cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN){
return String.valueOf(cell.getBooleanCellValue());
}else if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
double value = cell.getNumericCellValue();
return new BigDecimal(value).toString();
}else if (cell.getCellType() ==HSSFCell.CELL_TYPE_STRING){
return String.valueOf(cell.getStringCellValue());
}else{
return String.valueOf(cell.getStringCellValue());
}
}


好了,一个导入功能的关键都在这里了,还有就是记得引入poi的包 。差不多可以了,小伙伴们,如果你看到了这篇文章,说明你是幸运的,我讲的很详细,之前搜了 一些,都看的我模棱两可,然后我就想着如果我把这个做出来了,一定要好好的写一篇,让别人不那么迷糊。一看就懂,哈哈,希望能够帮到大家,那样就达到我的目的了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 导入excel