博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
jxl 导入excel
阅读量:5922 次
发布时间:2019-06-19

本文共 4348 字,大约阅读时间需要 14 分钟。

jsp部分

<div class="modal-dialog">

<div class="modal-content">
<div class="modal-body">
<h3>
<i class="ace-icon fa fa-home home-icon"></i>
Excel导入客户数据 </h3>
<hr/>
<br/>
<form action="${ctx}/customer/importcustomer" method="post" enctype="multipart/form-data" id="imp">

<div class="form-group">

<input type="file" name="import" />
<input type="hidden" name="table" value="tablename" />
</div>
<!-- /.box-body -->
<br/>
<div class="form-group">
&nbsp;&nbsp;<a href="#">导入模板下载</a>         //href为服务器绝对地址    事前存放好的文件路径   直接获取
</div>
<!-- /.box-footer -->
</form>
<br/>
<!-- data-dismiss="modal" 影响ajax提交 -->
<div class="modal-footer">
<button class="btn btn-sm btn-success" data-dismiss="modal"
id="import"><i
class="ace-icon fa fa-check"></i> 导入
</button>&nbsp;&nbsp;
<button class="btn btn-sm btn-warning"
οnclick="toclosecustomergroup();">
<i class="ace-icon fa fa-times"></i> 取消
</button>
</div>
</div>
</div>

$(document).ready(function() {

$('#import').click(function() {
var formData = new FormData($("#imp")[0]);
var file = $("input[name='import']").val();
var suffix = file.split(".")[1];                        //根据小数点截取   第二个字符串   也就是后缀名
if(suffix == null){
layer.msg("请选择上传的Excl文件!");
return;
}else if(suffix!="xls" && suffix!="xlsx" ){
layer.msg("格式不正确,请选择上传的Excl文件!");
return;
}
$.ajax({
url: "${ctx}/customer/importcustomer",
type: 'post',
data: formData,
async: false,
cache: false,
contentType: false,
processData: false,
error : function() {
layer.msg('网络错误!');
},
success: function(data) {
if(data==1){
layer.msg("导入成功");
parent.location.reload();
}else{
layer.msg(data);
}
}
});
});
});

------------------------------------------------------------------------------------------------------------------------------------------

controller部分       先上传再读取   上传文件可以不保存  读取上传的文件流

//客户导入

@RequestMapping(value = "/importcustomer", method = RequestMethod.POST)
@ResponseBody
public String importcustomer(
HttpServletRequest request, Model model,HttpSession session) {
//获取店id
Object objshopid = request.getSession().getAttribute("shopid");
Integer shopid = (Integer) objshopid;
String resultName = "";
String newFileName = "";
String str="";
CommonsMultipartFile cf=null;   //很重要
// 文件流
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
Iterator item = multipartRequest.getFileNames();
while (item.hasNext()) {
String fileName = (String) item.next();
MultipartFile file = multipartRequest.getFile(fileName);
cf = (CommonsMultipartFile) file;
//截取不带扩展名的文件名
fileName = file.getOriginalFilename().substring(0,
file.getOriginalFilename().lastIndexOf("."));
// 检查扩展名
String fileExt = file.getOriginalFilename()
.substring(file.getOriginalFilename().lastIndexOf(".") + 1)
.toLowerCase();
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
// 新文件名为原名+日期+随机数
newFileName = df.format(new Date()) + "_"
+ new Random().nextInt(1000) + "." + fileExt;
resultName = resultName + newFileName + ";";
}
//读取Excel文件
Workbook workbook = null;
List<Customer> list = new ArrayList<Customer>();
try {
InputStream is = cf.getInputStream();    //获取文件流
//获取工作薄和第一个工作单
workbook = Workbook.getWorkbook(is);
Sheet sheet = workbook.getSheet(0);
SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd");
int row = sheet.getRows();
//定义开始的一行
int rowStart = 1;
//循环行,标题行的下一行开始
for (int i = rowStart; i < row; i++) {
Customer customer = new Customer();
Cell[] cells = sheet.getRow(i);
customer.setCustomername(cells[0].getContents());
customer.setTel(cells[1].getContents());
String birthday=cells[2].getContents();
Date time =format2.parse(birthday);
customer.setBirthday(time);
//分组名称
String groupname=cells[3].getContents();
CustomerGroup customergroup =customerservice.findgroupbyshopidandname(shopid, groupname);
if(customergroup!=null){
customer.setGroupid(customergroup.getId());
}else{
CustomerGroup cugp=new CustomerGroup();
cugp.setGroupname(groupname);
cugp.setShopid(shopid);
customerservice.insertgroupbyname(cugp);
customer.setGroupid(cugp.getId());
}
customer.setSex(2);
customer.setShopid(shopid);
list.add(customer);
}
for(Customer customeror:list){
Customer ccc=customerservice.findcbyshopidandtel(customeror.getShopid(),customeror.getTel());
if(ccc==null){
customerservice.insertcustomer(customeror);
}
}
} catch (Exception e) {
str="数据格式错误!";
return str;
}finally{
workbook.close();
}
str="1";
return str;
}

转载于:https://www.cnblogs.com/1234cjq/p/6188003.html

你可能感兴趣的文章
elk
查看>>
sudo 限制普通用户权限
查看>>
微软影片索引器更新!找影片更简单
查看>>
openstack-3:准备工作
查看>>
Failed to read candidate component class: URL [];
查看>>
动物界的房地产故事
查看>>
Java电商项目面试题(三)
查看>>
python-----requests
查看>>
动态字节码技术 javassist 初探
查看>>
CPU的汇编启动代码
查看>>
CentOS 7.3 部署LVS + Keepalived 高可用集群
查看>>
零基础怎样进行大数据的入门级学习?
查看>>
由点及面分辨矿机性能
查看>>
python服务器环境搭建(3)——参数配置
查看>>
冷启动问题
查看>>
软件编程的那些事(1)-前言
查看>>
jmeter高级用法例子,如何扩展自定义函数
查看>>
Part1——政府机构面临的安全威胁
查看>>
exp
查看>>
oracle教程之oralce非归档模式下的恢复(二)
查看>>