html页面的导入文件代码,设置enctype=”multipart/form-data”
////////////////////////////////////////
java代码如下,接收的参数类型为MultipartFile xlsPath
@RequestMapping("/admin/begin_import.htm")
public ModelAndView begin_import(HttpServletRequest request, HttpServletResponse response, MultipartFile xlsPath) {
List integralGoodses = new ArrayList();
try {
System.out.println("文件不是excel类型");
if (xlsPath != null) {
System.out.println("文件不是空");
XSSFWorkbook wb = null;
InputStream ins = null;
ins =xlsPath.getInputStream();
wb = new XSSFWorkbook(ins);
Sheet sheet = wb.getSheetAt(0);
int rowNum = sheet.getLastRowNum();
int cellNum;
Cell cell;
String value = "";
for (int i = 1; i <= rowNum-1; i++) {
Row row = sheet.getRow(i);
cellNum = sheet.getRow(i).getLastCellNum();
IntegralGoods integralGoods = new IntegralGoods();
for (int j = 0; j < cellNum; j++) {
cell = row.getCell((short) j);
if(cell!=null){
cell.setCellType(Cell.CELL_TYPE_STRING);
}
if (j == 0) {
value = cell.getStringCellValue();
integralGoods.setStoreId(value);
} else if (j == 1) {
value = cell.getStringCellValue();
integralGoods.setBeginTime(CommUtil.formatDate(value, "yyyy-MM-dd"));}
else if(j == 2) {
value = cell.getStringCellValue();
integralGoods.setVoucherId(value);
} else if (j == 3) {
value = cell.getStringCellValue();
integralGoods.setVoucherNum(value);
}
}
integralGoodses.add(integralGoods);
}
}
for (IntegralGoods integralGoods : integralGoodses) {
integralGoodsService.save(integralGoods);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ModelAndView mv = new JModelAndView("admin/blue/success.html", commonRedisCacheService.getSysConfig(), 0, request, response);
mv.addObject("op_title", "数据导入成功");
return mv;
}
}
下面是代码详细解说
@RequestMapping(“/admin/begin_import.htm”)
public ModelAndView begin_import(HttpServletRequest request, HttpServletResponse response, MultipartFile xlsPath) {
List integralGoodses = new ArrayList();
//创建一个类的list容器
try {
if (xlsPath != null)
XSSFWorkbook wb = null;
InputStream ins = null;
ins =xlsPath.getInputStream();//得到excel文件的流
wb = new XSSFWorkbook(ins);//创建工作薄
Sheet sheet = wb.getSheetAt(0);//得到工作薄的第一个表
int rowNum = sheet.getLastRowNum();//得到表的行数
int cellNum;
Cell cell;
String value = “”;
for (int i = 1; i <= rowNum-1; i++) { //从有数据的一行开始(这里默认第一行是标题)
Row row = sheet.getRow(i); //得到第i行数据
cellNum = sheet.getRow(i).getLastCellNum();//一行表格中单元格数量
IntegralGoods integralGoods = new IntegralGoods();//定义类对象
for (int j = 0; j < cellNum; j++) { //一行中的每一列单元格
cell = row.getCell((short) j); //得到单元格
if(cell!=null){
cell.setCellType(Cell.CELL_TYPE_STRING);
}
if (j == 0) {
value = cell.getStringCellValue();
integralGoods.setStoreId(value);//将单元格的值付给对象属性
} else if (j == 1) {
value = cell.getStringCellValue();
integralGoods.setBeginTime(CommUtil.formatDate(value, “yyyy-MM-dd”));}
else if(j == 2) {
value = cell.getStringCellValue();
integralGoods.setVoucherId(value);
} else if (j == 3) {
value = cell.getStringCellValue();
integralGoods.setVoucherNum(value);
}
}
integralGoodses.add(integralGoods);//将对象添加到list容器
}
}
for (IntegralGoods integralGoods : integralGoodses) {
integralGoodsService.save(integralGoods);//插入数据到数据库
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ModelAndView mv = new JModelAndView("admin/blue/success.html", commonRedisCacheService.getSysConfig(), 0, request, response);
mv.addObject("op_title", "数据导入成功");
return mv;
}
}
实例二
@RequestMapping("/admin/b2ggoods_import.htm")
public ModelAndView b2ggoods_import(HttpServletRequest request, HttpServletResponse response, MultipartFile xlsPath) {
List goodses = new ArrayList();
try {
System.out.println("文件不是excel类型");
if (xlsPath != null) {
System.out.println("文件不是空");
HSSFWorkbook wb = null;
InputStream ins = null;
ins = xlsPath.getInputStream();
wb = new HSSFWorkbook(ins);
HSSFSheet sheet = wb.getSheetAt(0);
int rowNum = sheet.getLastRowNum();
int cellNum;
HSSFCell cell;
String value = "";
for (int i = 1; i <= rowNum; i++) {
HSSFRow row = sheet.getRow(i);
cellNum = sheet.getRow(i).getLastCellNum();
Goods goods = new Goods();
for (int j = 0; j < cellNum; j++) {
cell = row.getCell((short) j);
if (cell != null) {
cell.setCellType(Cell.CELL_TYPE_STRING);
}
if (j == 0) {
value = cell.getStringCellValue();
goods.setGoods_code(value);
} else if (j == 1) {
value = cell.getStringCellValue();
goods.setGoods_name(value);
} else if (j == 2) {
value = cell.getStringCellValue();
EntityWrapper sos = new EntityWrapper<>();
sos.where("className={0}", value);
GoodsClass goodsClass = goodsClassService.selectOne(sos);
if (goodsClass != null) {
goods.setGc_id(goodsClass.getId());
}
} else if (j == 3) {
value = cell.getStringCellValue();
if (StringUtils.equals(value, "普通分类")) {
goods.setIs_enterprise(false);
}
if (StringUtils.equals(value, "企业分类")) {
goods.setIs_enterprise(true);
}
}else if (j == 4) {
value = cell.getStringCellValue();
if (StringUtils.equals(value, "B2G")) {
goods.setBussiness_type(0);
}
if (StringUtils.equals(value, "B2B")) {
goods.setBussiness_type(1);
}
}
}
goodses.add(goods);
}
}
for (Goods goods : goodses) {
goods.setAddTime(new Date());
goods.setGoods_status(0);
goodsService.save(goods);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ModelAndView mv = new JModelAndView("admin/blue/success.html", commonRedisCacheService.getSysConfig(), 0, request, response);
mv.addObject("op_title", "数据导入成功");
mv.addObject("list_url", "/admin/admin/goods_self_list.htm");
return mv;
}