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. 反射导出excel案例

    1.代码案例: protected void btnExportExcel_Click(object sender, EventArgs e) { SetSearchValue(); Dictiona ...

  2. Redis API与常用数据类型简介

    Redis API与常用数据类型简介 一.Redis API For .Net 首先,不得不说Redis官方提供了众多的API开发包,但是目前Redis官方版本不支持.Net直接进行连接,需要使用一些 ...

  3. (转载)Log4Net 在多层项目中的使用小记

    (原创)Log4Net 在多层项目中的使用小记 这几天刚好在调整一个项目,把一些自己不是很清楚的东西先试验一下,这篇文章主要是对我在项目中需要使用Log4Net的一些记录.网上有很多相关的教程,但是各 ...

  4. C#里CheckListBox的全选

    for (int i = 0; i < chkLSelect.Items.Count; i++)            {                if (chkCheck.Checked ...

  5. [转]iOS Tutorial – Dumping the Application Heap from Memory

     Source:https://blog.netspi.com/ios-tutorial-dumping-the-application-heap-from-memory/ An essential ...

  6. 关于grub的那些事(二)

    上回说到/etc/default/grub文件,我直接抄了人家的文章,感觉那Wiki确实写的很详细,所以就用上拿来主义了. 这次是分析该文件,因为这是grub必读的文件,也记录着控制grub工作的环境 ...

  7. 关于Grunt可视化的尝试

    关于Grunt可视化的尝试 使用Grunt遇到的问题? 必须要安装NodeJS 必须安装grunt-cli 需要编写复杂的Gruntfile.js规则 每个项目中必须存在nodejs的grunt模块 ...

  8. js框架漫谈

    现在实际项目中可供选择的javascript框架很多,热门的有jquery,dojo,mootools,ext等.这些框架按照不同的标准有不同的分类方法,比如按照扩展方式便可分为prototype式的 ...

  9. jquery animate stop函数解析

    今天我们来看看jquery中动画操作的stop函数.其实我至今不是很明白啊,所以此文算是求救以及抛砖引玉. 在jquery 1.7版本以前,stop支持两个参数,分别是clearQueue和jumpT ...

  10. Easyui + asp.net mvc + sqlite 开发教程(录屏)适合入门

    Easyui + asp.net mvc + sqlite 开发教程(录屏)适合入门 第一节: 前言(技术简介) EasyUI 是一套 js的前端框架 利用它可以快速的开发出好看的 前端系统 web ...