对excel的导入操作

①在页面点击按钮后弹出导入框

//导入交易明细
function doImportTransactionDetail(){
    
    url=ctx + "/extract_catch/importTransactionDetail.action";        
    var screenWidth = screen.width;
    var leftWidth=(screenWidth-725)/2;    
    
    var result=window.showModalDialog(url,window,"dialogWidth=600px;dialogHeight=250px;dialogLeft="+leftWidth+"px;dialogTop=200px;center=yes;status=no;scroll='no' ");
    window.close();
}


<td><input name="" type="button" style="width:100px"   οnclick="doImportTransactionDetail()"   value="交易明细导入"></td>



②生成弹出来的jsp

<script type="text/javascript">

function check(){
    var file=$("#file").val();
    if(file==null||file==''){
        alert("请选择要导入的文件");
        return false;
    }
    return true;
}
$(function(){
    var result=$("#result").html();
    var msg=$($("#msg")).html();    
    if(result!=''){
        var isGoon = confirm(msg+ ",是否继续添加?");
        if(result){
            if(isGoon) {
                window.location.href = ctx + "/extract_catch/importTransactionDetail.action";
            } else {
                dialogArguments.location.href=dialogArguments.location.href ;
                window.close();
            }
        }
    }
});
</script>


<body background="white">
<div class="webmain" id="main">
    
    <div class="websearch" id=""  style="text-align: center;font-size:13px;margin-top:5px;margin-left:80px; width:100%;padding-left:0px;">
                <form id="importform" action="${ctx}/extract_catch/doImportTransactionDetail.action" method="post" enctype="multipart/form-data" οnsubmit="return check()">
                    <table class="form1" style="border-bottom: 1px;width:300px;text-align: left;border:1">
                        <tr>
                            <td class="info-td" nowrap="nowrap" style="line-height:25px;height:25px;">选择文件</td>
                            <td class="info-td" nowrap="nowrap" style="line-height:25px;height:25px;">
                                <input type="file" name="file" id="file" class="button"/>&nbsp;&nbsp;
                                <input name="submitBtn" type="submit" class="submit_btn" value="确定" />
                                
                            </td>
                        </tr>
                        <tr>
                            <td class="info-td" nowrap="nowrap" style="line-height:25px;height:25px;"></td>
                            <td class="info-td" nowrap="nowrap" style="line-height:25px;height:25px;">
                            <a href="${ctx }/activity_merchant/download.action?file=merchantimport.xls" ><span style="color:red;font-size:12px;display:none">下载模板</span></a>
                            </td>
                        </tr>
                        <tr style="height:100px"><td><span style="display:none" id="result">${result}</span></td><td><span id="msg"  style="display:none">${msg}</span></td></tr>
                    </table>
                </form>
            </div>
        </div>
</body>


③跳转到controller


    @RequestMapping("importTransactionDetail")
    public String importTransactionDetail(ModelMap m){        
            return BaseUtil.baseReturn(m,"activity/ticket/importTransaction");    
    }
    @RequestMapping("doImportTransactionDetail")
    public String doImportTransactionDetail(ModelMap m,HttpServletRequest request){        
        try{
            MultipartHttpServletRequest fileRequest = (MultipartHttpServletRequest) request;
            MultipartFile file = fileRequest.getFile("file");
            if (file == null) {
                return BaseUtil.baseReturn(m, "activity/ticket/importTransaction");
            }
            log.info(file.getOriginalFilename());
            String fileName = file.getOriginalFilename();
            if (StringUtils.isEmpty(fileName)) {
                m.put("result", false);
                m.put("msg", "请选择要上传的文件");
                return BaseUtil.baseReturn(m, "activity/ticket/importTransaction");
            }            
            String type = fileName.substring(fileName.lastIndexOf(".") + 1,fileName.length());
            List<ExtractCashRecord> extractCashRecords=null;
            if ("xls".equals(type)) {
                extractCashRecords = this.getImportTransactionDetail(file.getInputStream());
                if(extractCashRecords!=null&&extractCashRecords.size()>0){
                    //对获取到的数据在数据库中修改
                    extractCashRecordService.changetTransactionDetail(extractCashRecords);
                    m.put("msg", "导入成功");
                    m.put("result", true);            
                }else{
                    m.put("result", false);
                    m.put("msg", "没有要导入的数据");
                    return BaseUtil.baseReturn(m, "activity/ticket/importTransaction");
                }
            }else{
                m.put("result", false);
                m.put("msg", "请选择上传的文件为excel文件");
                return BaseUtil.baseReturn(m, "activity/ticket/importTransaction");
            }            
        }catch(Exception e){
            m.put("result", false);
            m.put("msg", "导入失败");
            log.info("导入失败:"+e.getMessage());
        }
        return BaseUtil.baseReturn(m, "activity/ticket/importTransaction");
    }

    
    private List<ExtractCashRecord> getImportTransactionDetail(InputStream orginalInputStream) {
        List<ExtractCashRecord> extractCashRecords=new ArrayList<ExtractCashRecord>();
        InputStream is = null;
        try{
            is = orginalInputStream;
            Workbook rwb = Workbook.getWorkbook(is);
            Sheet st[] = rwb.getSheets();
            for (int i = 4; i < st[0].getRows(); i++) {
                
                //测试记录的信息是否为需要处理的文件信息
                String regex=Constants.FILE_PREFIX;
                String num=st[0].getCell(1, i).getContents().trim();
                boolean flag=num.contains(regex);
                
                if (flag) {
                    //文件名    序号    交易时间    户名    账号    交易金额    交易结果    失败原因    手续费
                    ExtractCashRecord extractCashRecord=new ExtractCashRecord();
                    extractCashRecord.setExtractFileName(st[0].getCell(1, i).getContents().trim()+".xls");
                    extractCashRecord.setAccountName(st[0].getCell(4, i).getContents().trim());
                    extractCashRecord.setBankAccount(st[0].getCell(5, i).getContents().trim());
                    float amount=new Float(st[0].getCell(6, i).getContents().trim());
                    extractCashRecord.setAmount(String.valueOf((long) (amount*100)));
                    
                    String result=st[0].getCell(7, i).getContents().trim();                
                    if("成功".equals(result.trim())){
                        extractCashRecord.setBankResult(1);
                    }
                    else if("失败".equals(result.trim()))    {
                        extractCashRecord.setBankResult(3);
                        extractCashRecord.setFailureResean(st[0].getCell(8, i).getContents().trim());
                    }
                    else if("已发送银行".equals(result.trim()))    {
                        extractCashRecord.setBankResult(2);
                        extractCashRecord.setFailureResean(st[0].getCell(8, i).getContents().trim());                
                    }                
                    extractCashRecords.add(extractCashRecord);
                    log.info("商户代付业务交易明细 ==>"+ st[0].getCell(1, i).getContents().trim()+ "=====");
                }else {
                    log.error("商户代付业务交易明细,此条信息不符合");
                }
            }
            
            if(extractCashRecords.size()==0){
                return null;
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return extractCashRecords;
    }













  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值