zoukankan      html  css  js  c++  java
  • jxl 导入excel

    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;
    }

    当能力支撑不了野心时,就该静下心来学习!
  • 相关阅读:
    ajax打开新窗口防止被浏览器阻止问题
    网站安全
    纠结点
    haven单词5个
    jQuery 鼠标停留样式
    js new date
    XML解析特殊字符出错
    个人对JQuery Proxy()函数的理解
    sql server 数据库 杀掉死锁进程
    转载一个:【C#4.0】中的dynamic与var的区别西南烟雨
  • 原文地址:https://www.cnblogs.com/1234cjq/p/6188003.html
Copyright © 2011-2022 走看看