您的位置:首页 > 编程语言 > Java开发

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

2015-03-11 16:35 369 查看
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="提交" onclick="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";

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  redirect springmvc excel