java导入excel表将表数据合并成string类型

前端:

<li class="btn_uploads"><input type="file" id="wjsc" οnchange='importf()'><i class="Hui-iconfont">&#xe645;</i>&nbsp;导入</li>

 

js:

//导入
function importf() {
    var txt = document.getElementById("wjsc").value.substr(
            document.getElementById("wjsc").value.lastIndexOf("."))
            .toLowerCase();
    if (txt != '.xls' && txt != '.xlsx') {
        layer.alert("文件只能是xls文件或者是xlsx文件!!!");
        this.value = "";
        return false;
    } else {
        var formData = new FormData();
        formData.append("file",$("#wjsc")[0].files[0]);
        
            $.ajax({
                url : "/addproduct/lead",
                type : "post",
                 // 告诉jQuery不要去处理发送的数据
                processData : false,
                // 告诉jQuery不要去设置Content-Type请求头
                contentType : false,
                data:formData,
                success : function(result) {
                    if(result.code=="1"){
                        layer.alert("导入成功")
                        $("#isbn").val(result.data)
                    }else{
                        layer.alert("导入失败")
                    }
                }
            });
    }

 

 

controller

 

@RequestMapping(value = "/lead")
    @ResponseBody
    public Map<String, Object> readXlsx(@RequestParam("file") MultipartFile file) throws Exception {
        try {
            InputStream inputStream = file.getInputStream();
            POIFSFileSystem poifsFileSystem = new POIFSFileSystem(inputStream);
            Workbook workbook = WorkbookFactory.create(poifsFileSystem);
            Sheet hssfSheet = workbook.getSheetAt(0); // 示意访问sheet
            StringBuilder isbns = new StringBuilder();

            for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
                Row xssfRow = hssfSheet.getRow(rowNum);
                int minColIx = xssfRow.getFirstCellNum();
                int maxColIx = xssfRow.getLastCellNum();

                for (int colIx = minColIx; colIx < maxColIx; colIx++) {
                    Cell cell = xssfRow.getCell(colIx);
                    if (cell == null) {
                        continue;
                    }
                    isbns = isbns.append(cell.toString() + ",");
                }
            }
            String sb = isbns.toString().substring(0, isbns.toString().length() - 1);

            return getResultObjectData(ResultCode.SUCCESS, sb, "成功获取导入信息!");

        } catch (Exception e) {
            return getResultObjectData(ResultCode.FAILED, null, "获取导入信息失败!");
        }
    }

 

 

pom.xml

<!-- excel表格处理 -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.9</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.9</version>
        </dependency>
        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4.7</version>
        </dependency>
        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>smtp</artifactId>
            <version>1.6.0</version>
        </dependency>
        <dependency>
            <groupId>net.sourceforge.jexcelapi</groupId>
            <artifactId>jxl</artifactId>
            <version>2.6.10</version>
        </dependency>

 

效果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值