POI导入转到页面做检验

这篇博客讲述了如何在Java中使用POI库导入Excel数据,并非直接存入数据库,而是将数据传递到前端页面进行展示和检验。在导入过程中,确保不覆盖已输入的数据,并实现了数据的逐行合并。利用ajaxfileupload.js将后台处理的数据转换为JSON并填充到前端。文章提到了模板制作,页面代码实现以及针对IE浏览器的JSON转换问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在以前做的POI导入都是通过了之后,直接往数据库里面存,但是这个做的却是导入之后往前台页面展示,且不能冲掉到已经输入的数据,然后再做数据校验。通过这个需求,查了些资料,得到思路:先在后台获取到数据,组装成json,然后通过ajaxfileupload.js填写到前台页面上面来。(也为之后的全单导入做好铺垫)

制作的导入模板如下:


页面代码如下:

<span style="font-family: Arial, Helvetica, sans-serif;"><div class="data_list"></span><pre name="code" class="html">	<h2>
		<span class="fl">货物信息</span>
	<span class="fr">		
		<input type="button" οnclick="showImportDialog()" value="货物导入" class="gray_button"/>
		<input type="button" id="addManifestICargoTr" value="添加" class="gray_button"/>
	</span><!-- span里无内容时,此span不能删除  -->
</h2>
<div class="verflow_xy">
	<table id="manifestICargoTab" width="100%" border="0" cellspacing="1" cellpadding="0" class="list_table_s">
		<tr>
			<th nowrap="nowrap" width="7%" >商品序号</th>
			<th nowrap="nowrap" width="7%" style="color: red;">商品描述</th>
			<th nowrap="nowrap" width="7%" style="color: red;">货物件数</th>
			<th nowrap="nowrap" width="7%" style="color: red;">毛重(KG)</th>
			<th nowrap="nowrap" width="7%" style="color: red;">包装类型</th>
			<th nowrap="nowrap" width="7%">商品描述补充信息</th>
			<th nowrap="nowrap" width="7%">危险品编号</th>
			<th nowrap="nowrap" width="16%">操作</th>
		</tr>
		<s:if test="manifestIMain.manifestICargos.size() == 0">
			<tr>
				<td nowrap="nowrap" width="7%"><input type="text" style="text-align: center;" name="manifestIMain.manifestICargos.cargNo" class="inputForbidden" value="1" readonly="readonly"/></td>
				<td nowrap="nowrap" width="7%"><input type="text" name="manifestIMain.manifestICargos.cargDecri"  dataType="Require" msg="商品描述不能为空!" class="input_s_1" /></td>
				<td nowrap="nowrap" width="7%"><input type="text" name="manifestIMain.manifestICargos.packNum" οnchange="arthPackNum()" dataType="Require" msg="货物件数不能为空!" class="input_s_1" /></td>
				<td nowrap="nowrap" width="7%"><input type="text" name="manifestIMain.manifestICargos.grossWt" οnchange="arthCargoWt()" dataType="Require" msg="毛重不能为空!" class="input_s_1" /></td>
				<td nowrap="nowrap" width="7%">
					<input name="manifestIMain.manifestICargos.cargoType" id="cargoType_1" class="input_s_1" dataType="Require" msg="货物包装类型不能为空!" />
				</td>
				<td nowrap="nowrap" width="7%"><input type="text" name="manifestIMain.manifestICargos.cargDecriB" class="input_s_1" /></td>
				<td nowrap="nowrap" width="7%"><input type="text" name="manifestIMain.manifestICargos.dangeNo" class="input_s_1" οnchange="checkDangeNo()"/></td>
				<td nowrap="nowrap" width="16%" class="oper_btn">
					<input type="hidden" name="manifestIMain.manifestICargos.id" value="<s:property value='id'/>" />
					<input type="hidden" name="manifestIMain.manifestICargos.mid" value="<s:property value='mid'/>" />
					<input type="button" name="delBtn" class="button_delete" title="删除" />
				</td>
			</tr>
		</s:if>
		<s:iterator value="manifestIMain.manifestICargos" status="st">
			<tr>
				<td nowrap="nowrap" width="7%"><input type="text" style="text-align: center;" name="manifestIMain.manifestICargos.cargNo" class="inputForbidden" readonly="readonly" value="<s:property value='cargNo'/>"/></td>
				<td nowrap="nowrap" width="7%">
					<input type="text" name="manifestIMain.manifestICargos.cargDecri" class="input_s_1" dataType="Require" msg="商品描述不能为空!"
						value="<s:property value='cargDecri'/>"/></td>
				<td nowrap="nowrap" width="7%">
					<input type="text" name="manifestIMain.manifestICargos.packNum" οnchange="arthPackNum()" class="input_s_1" dataType="Require" msg="货物件数不能为空!"
						value="<s:property value='packNum'/>"/></td>
				<td nowrap="nowrap" width="7%">
					<input type="text" name="manifestIMain.manifestICargos.grossWt"  οnchange="arthCargoWt()" class="input_s_1" dataType="Require" msg="毛重不能为空!"
						value="<s:property value='grossWt'/>"/></td>
				<td nowrap="nowrap" width="7%">
					<input name="manifestIMain.manifestICargos.cargoType" id="cargoType_%{#st.index+1}" value="<s:property value='cargoType'/>" class="input_s_1" dataType="Require" msg="货物包装类型不能为空!" /> 
				</td>
				<td nowrap="nowrap" width="7%"><input type="text" name="manifestIMain.manifestICargos.cargDecriB" class="input_s_1" value="<s:property value='cargDecriB'/>"/></td>
				<td nowrap="nowrap" width="7%"><input type="text" name="manifestIMain.manifestICargos.dangeNo" class="input_s_1"  οnchange="checkDangeNo()"  value="<s:property value='dangeNo'/>"/></td>
				<td nowrap="nowrap" width="16%" class="oper_btn">
					<input type="hidden" name="manifestIMain.manifestICargos.id" value="<s:property value='id'/>" />
					<input type="hidden" name="manifestIMain.manifestICargos.mid" value="<s:property value='mid'/>" />
					<input type="button" name="delBtn" class="button_delete" title="删除" />
				</td>
			</tr>
		</s:iterator>
		</table>
	</div>
</div>

 js代码如下: 
//导入货物信息
function importCargo(){
	var file = $('#importFile').val();
	if(file != ""){
		$.ajaxFileUpload({  
		      url: "importICargoTr.action?file="+file,  
		      secureuri:false,
		      fileElementId:'importFile',
		      dataType: "JSON",
		      success: function (data) {
		      	  if(isIE || $.browser.version == '11.0'){
			    	  data = jQuery.parseJSON(data);//ie
				  }else{
			    	  data = jQuery.parseJSON(jQuery(data).text());//chrome
				  }
		    	  if(data.cargo == 'error'){
						layer.alert("导入货物信息数据有误,请检查后在上传!");
						return;
			      }
		    	  $.each(data.cargo, function(i, item) {
			    	  var template = $("<tr></tr>").append($("#manifestICargoTab tr:last").html());
					  var cargDecri = $("#manifestICargoTab tr:last input[name='manifestIMain.manifestICargos.cargDecri']").val();
					  cargDecri = $.trim(cargDecri);
					  if(cargDecri != null && cargDecri != ""){
						  $("#manifestICargoTab").append(template);
			    	  }
					  $("#manifestICargoTab tr:last input[name='manifestIMain.manifestICargos.packNum']").val(item.packNum);
					  $("#manifestICargoTab tr:last input[name='manifestIMain.manifestICargos.grossWt']").val(item.grossWt);
					  $("#manifestICargoTab tr:last input[name='manifestIMain.manifestICargos.cargDecri']").val(decodeURI(item.cargDecri));
					  $("#manifestICargoTab tr:last input[name='manifestIMain.manifestICargos.cargDecriB']").val(decodeURI(item.cargDecriB));
					  $("#manifestICargoTab tr:last input[name='manifestIMain.manifestICargos.dangeNo']").val(decodeURI(item.dangeNo));
					  //cargoTypeProc("I");//设置货物包装类型
					  $("#manifestICargoTab tr:last input[name='manifestIMain.manifestICargos.cargoType']").val(decodeURI(item.cargoType));
					  buildSeqNum('#manifestICargoTab>tbody>tr:not(:first)','manifestIMain.manifestICargos.cargNo');
		    	  });
		    	  $("#consignorName").val(decodeURI(data.consignorName));
		    	  $("#consigneName").val(decodeURI(data.consigneName));
		    	  closeImportBg();
		    	  arthPackNum();//计算货物总件数
		    	  arthCargoWt();//计算货物总毛重
		      }
		  });
	}else{
		layer.alert("请选择要上传的文件!",9 ,'提示');
	}
}
后台代码如下:

/**
     * 导入货物信息,返回json数据
     * @return
     */
    @Action(value = "importICargoTr")
    public void importICargoTr() {
    	Map<String,Object> map = new HashMap<String, Object>();
        try {
        	Workbook workBook = WorkbookFactory.create(file);
			Sheet sheet = workBook.getSheetAt(0);
            List<ManifestICargo> cargos = new ArrayList<ManifestICargo>();
            String consigneName = StringUtils.getValueEncode(sheet.getRow(0).getCell(1),HSSFCell.CELL_TYPE_STRING);//收货人
            String consignorName = StringUtils.getValueEncode(sheet.getRow(1).getCell(1),HSSFCell.CELL_TYPE_STRING);//发货人
            for(int i=3;i<=52;i++){
                Row row = sheet.getRow(i);
                if(row == null)
                	continue;
                String cargoDecri = StringUtils.getValue(row.getCell(0),HSSFCell.CELL_TYPE_STRING);
                if("".equals(cargoDecri) || cargoDecri == null)
                	continue;
                ManifestICargo cargo = new ManifestICargo();
                cargo.setCargDecri(cargoDecri);//商品描述
                cargo.setPackNum((int)Double.parseDouble(StringUtils.getValueEncode(row.getCell(1),HSSFCell.CELL_TYPE_NUMERIC)));//货物件数
                cargo.setGrossWt(Float.parseFloat(StringUtils.getValueEncode(row.getCell(2),HSSFCell.CELL_TYPE_NUMERIC)));//货物毛重
                String cargoType = StringUtils.getValueEncode(row.getCell(3),HSSFCell.CELL_TYPE_STRING);
                if(!"".equals(cargoType) && cargoType != null)
                	cargo.setCargoType(cargoType.substring(0,2));//包装类型
                cargo.setCargDecriB(StringUtils.getValueEncode(row.getCell(4),HSSFCell.CELL_TYPE_STRING));//补充信息
                cargo.setDangeNo(StringUtils.getValueEncode(row.getCell(5),HSSFCell.CELL_TYPE_STRING));//危险品编号
                cargos.add(cargo);
            }
            map.put("consigneName",consigneName);
            map.put("consignorName",consignorName);
            map.put("cargo", cargos);
        } catch (Exception e) {
            map.put("cargo", "error");
        	log.error("Insert occurred error."+e.getMessage());
        	if(Constants.IS_TEST)
    			setErrorMsg(e.getMessage());
        	e.printStackTrace();
        }finally{
            writeJson(map);
        }
    }
导入之前的页面


导入之后的页面:


能依次融合之前输入的数据,判断从哪行开始写入。还有一点代码上面有兼容IE的js代码,在IE浏览器下有转换json报错的情况。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值