springMVC 读取excel内容并写入数据库

1,jsp页面

<form id="fileUpload" action="${path}/break/uploadGoodsStore.jhtml" enctype="multipart/form-data" method="post">
      <input id="excelFile" name="file" type="file"/>
      <input type="button" value="提交" οnclick="submitExcel()" 
      style="background:#faaa70; padding:2px 8px; color:white;margin-top:10px; text-align:center; border-radius:2px;border:1px #fa7b1e solid;"
      />
 </form>

2,js代码

<script>
function submitExcel(){
var excelFile = $("#excelFile").val();
    if(excelFile=='') {alert("请选择需上传的文件!");return false;}
    if(excelFile.indexOf('.xls')==-1){alert("文件格式不正确,请选择正确的Excel文件(后缀名.xls)!");return false;}
    $("#fileUpload").submit();
}
</script>

3,后台代码

@RequestMapping("uploadGoodsStore.jhtml")
public String uploadGoodsStore( @RequestParam MultipartFile file,HttpServletRequest request) throws BiffException, IOException{
logger.info("进入上传excel方法【uploadGoodsStore】");
jxl.Workbook excel = null;

             // 创建一个excel文件对象
  try {
excel = jxl.Workbook.getWorkbook(file.getInputStream());
// 获得第一个excel文件的sheet
jxl.Sheet sheet = excel.getSheet(0);
// 获取sheet单元的总行数
int rows = sheet.getRows();
// 获得sheet单元的总列数

int columns = sheet.getColumns();
List<GoodsStoreVo> goodsStroeErrorList=new ArrayList<GoodsStoreVo>();
List<GoodsStoreVo> goodsStroeRightList=new ArrayList<GoodsStoreVo>();
// 读取数据

for (int r = 2; r < rows; r++) {
// 每行数据
GoodsStoreVo goodsStoreVo=new GoodsStoreVo();
  //for (int c =0; c < columns; c++) {
// jxl.Cell cell = sheet.getCell(c, r);
// String cellValue = cell.getContents();
//   System.out.println(cellValue);
//}
String productCode=sheet.getCell(0,r).getContents();

String productName=sheet.getCell(1,r).getContents();
String unit=sheet.getCell(2,r).getContents();
String specifications=sheet.getCell(3,r).getContents();
String brand=sheet.getCell(4,r).getContents();
int amount=0;
if(StringUtils.isNotEmpty(sheet.getCell(5,r).getContents())){

amount=Integer.parseInt(sheet.getCell(5,r).getContents());
}
goodsStoreVo.setProductCode(productCode); 
goodsStoreVo.setProductName(productName);
goodsStoreVo.setUnit(unit);
goodsStoreVo.setSpecifications(specifications);
goodsStoreVo.setBrand(brand);

goodsStoreVo.setAmount(amount);
goodsStoreVo.setLineNumber(r+1);
if(StringUtils.isNotEmpty(sheet.getCell(6,r).getContents())){
goodsStoreVo.setPrice(new BigDecimal(sheet.getCell(6,r).getContents()));
}else{

goodsStoreVo.setPrice(null);
}
goodsStoreVo.setStoreLoc(sheet.getCell(7,r).getContents());
goodsStoreVo.setRemark(sheet.getCell(8,r).getContents());
if(StringUtils.isEmpty(productCode)||StringUtils.isEmpty(productName)||StringUtils.isEmpty(unit)||StringUtils.isEmpty(specifications)||StringUtils.isEmpty(brand)||amount==0){
goodsStroeErrorList.add(goodsStoreVo);

}else{
goodsStroeRightList.add(goodsStoreVo);
}
}
// 关闭excel对象
excel.close();
if(goodsStroeErrorList.size()>0){
request.setAttribute("goodsStroeErrorList", goodsStroeErrorList);

return "goods/outInfo/goodsStoreTemplateImport";
}else {
for(int i=0;i<goodsStroeRightList.size();i++){
GoodsStoreVo goodsStoreVo=goodsStroeRightList.get(i);
goodsStoreVo.setProjectteamId(getCurrentUser(request).getCurrentPtid());
int m=overflowinfoService.updateGoodByImport(goodsStoreVo);
if(m==0){
goodsStoreVo.setGoodId(IdGenerator.genUUid("g"));
goodsStoreVo.setCreateUser(getCurrentUser(request).getUserId());
goodsStoreVo.setCreateTime(new Date());
goodsStoreVo.setUpdateUser(getCurrentUser(request).getUserId());
goodsStoreVo.setUpdateTime(new Date());
goodsService.addGoods(goodsStoreVo);

}
}
request.getSession().setAttribute("suc","保存数据成功!");

return "redirect:../jsp/goods/outInfo/goodsStoreTemplateImport.jsp"; 
}

} catch (Exception e) {
e.printStackTrace();
}
return "goods/outInfo/goodsStoreTemplateImport";

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值