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"
onclick="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;
}

jxl 导入excel的更多相关文章

  1. Java jxl导入excel文件,导入的数字、身份证号码、手机号变成了科学计数法,解决方案

    原文出自:https://blog.csdn.net/seesun2012 这是一个execl文件导入数据库操作,使用jxl解析execl导入数据库过程出现了科学计数法,与想要导入的数据不匹配,以下是 ...

  2. jxl 导入excel以及日期格式处理

    先建一个excel文件abc.xls.放到E盘根目录下.形如下: name secondName hot1 leave1 hot2 leave2 然后在数据库里建表. CREATE TABLE `na ...

  3. jxl导入/导出excel

    1.jxl导入/导出excel案例,黏贴即可运行 package junit.test; import java.io.File; import java.io.IOException; import ...

  4. jxl导入/导出excel(网上的案例)

    jxl导入/导出excel 1.jxl导入/导出excel案例,黏贴即可运行 package junit.test; import java.io.File; import java.io.IOExc ...

  5. java利用jxl实现Excel导入功能

    本次项目实践基于Spring+SpringMvc+MyBatis框架,简单实现了Excel模板导出.和Excel批量导入的功能.实现过程如下:. 1.maven导入所需jar包 <depende ...

  6. 利用poi,jxl将Excel数据导入数据库

    需求:‘需要将本地的Excel中的数据经过验证之后导入数据库,在导入数据库之前在页面上展示出来 思路:将Excel导入存到session里面 去判断有没有不合法数据  如果有阻止提交 工具类: imp ...

  7. 1、jxl导入/导出excel案例,黏贴即可运行

    package junit.test; import java.io.File; import java.io.IOException; import java.util.ArrayList; imp ...

  8. JSP导入EXCEL样式

    http://demo.gcpowertools.com.cn/spreadjs/exceliosample/exceliosample/ Java实现导入Excel: 1.做一个jsp页面,页面包括 ...

  9. jxl读写excel, poi读写excel,word, 读取Excel数据到MySQL

    这篇blog是介绍: 1. java中的poi技术读取Excel数据,然后保存到MySQL数据中. 2. jxl读写excel 你也可以在 : java的poi技术读取和导入Excel了解到写入Exc ...

随机推荐

  1. Http Pipeline详细分析(下)

    Http Pipeline详细分析(下) 文章内容 接上面的章节,我们这篇要讲解的是Pipeline是执行的各种事件,我们知道,在自定义的HttpModule的Init方法里,我们可以添加自己的事件, ...

  2. SZU:B47 Big Integer II

    Judge Info Memory Limit: 32768KB Case Time Limit: 10000MS Time Limit: 10000MS Judger: Normal Descrip ...

  3. 【IOS开发】如何画1像素的线

    最近在项目中画了一根1像素的线,我是通过直接花一个但是通过PS查看,画了不止1个像素. 原代码语句: label1 = [[UILabel alloc] initWithFrame:CGRectMak ...

  4. 10.25最后的模拟赛DAY1 answer

    QAQ太困了,大概说一下自己的思路: 其实这题很容易看错题目或是想错,就比如我个傻逼,一开始以为p+q一定等于n.... 咳咳...其实这题不用想太多,我们可以通过这n个字符串一个个假设正确或是不正确 ...

  5. c# 发邮件功能

    using System;using System.Collections.Generic;using System.Data;using System.Data.SqlClient;using Sy ...

  6. WINCE 电池状态(C#)

    WINCE 电池状态(C#) 分类:             电量              2013-04-18 12:08     397人阅读     评论(1)     收藏     举报   ...

  7. MVC4中使用SignalR

    MVC4中使用SignalR 前言 周末在偶尔翻阅微软官网的时候看到Getting Started with SignalR and MVC 4此篇文章,知道了signalr这个东西,貌似这个出来很长 ...

  8. HashMap源码剖析

    HashMap源码剖析 无论是在平时的练习还是项目当中,HashMap用的是非常的广,真可谓无处不在.平时用的时候只知道HashMap是用来存储键值对的,却不知道它的底层是如何实现的. 一.HashM ...

  9. Oracle日志文件的管理与查看

    --Oracle日志文件管理与查看 select * from v$sql (#查看最近所作的操作) --select * fromv $sqlarea(#查看最近所作的操作) -- 1.查询系统使用 ...

  10. 数据访问模式之Repository模式

    数据访问模式之Repository模式   数据访问层无非就是对数据进行增删改查,其中增.删.改等我们可以抽象出来写一个公共的接口或抽象类来定义这些方法,并采用一个基类实现这些方法,这样该基类派生的子 ...