java mvc 导入,springmvc 导入excel

springmvc 导入excel

测试代码

@RequestMapping(value="/AddImportCooperation")

public void AddImportCooperation(

ModelMap model, HttpServletResponse response,HttpServletRequest request) throws IOException{

MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;

MultipartFile multipartFile = multipartRequest.getFile("file");

String sourceName = multipartFile.getOriginalFilename(); // 原始文件名

String fileType = sourceName.substring(sourceName.lastIndexOf("."));

//System.out.println("上传的文件名为:"+sourceName+"类型为:"+fileType);

//System.out.println("项目Id为:"+proId+"项目名称为:"+proName);

String base = request.getSession().getServletContext().getRealPath("/WEB-INF/views/upload");

File file = new File(base);

if(!file.exists()){

file.mkdirs();

}

String path=base + File.separator + sourceName;

multipartFile.transferTo(new File(path));

//service.insert("insertAttachment", attach);

//上传成功后读取Excel表格里面的数据

System.out.println("路径是"+path);

// File read=new File(path);

File read=new File(path);

Map map=FileUtil.readExcelFile(path);

JSONObject json = DWZUtil.returnSuccessJson("导入成功", "", "closeCurrent", "coo/initCooperationList");

DWZUtil.writer(response, json);

}

public static Map readExcelFile(String fileName){

//创建对Excel工作薄文件的引用

HashMap map=new HashMap();

try {

InputStream is = new FileInputStream(fileName);

HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);

// 循环工作表Sheet

for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {

HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);

if (hssfSheet == null) {

continue;

}

// 循环行Row

for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {

HSSFRow hssfRow = hssfSheet.getRow(rowNum);

if (hssfRow == null) {

continue;

}

HSSFCell no = hssfRow.getCell(0);

HSSFCell name = hssfRow.getCell(1);

HSSFCell age = hssfRow.getCell(2);

HSSFCell score = hssfRow.getCell(3);

System.out.println(no+""+name+age+score+"测试数据");

// xlsDto.setCj(Float.parseFloat(getValue(cj)));

// list.add(xlsDto);

}

}

} catch (Exception e) {

e.printStackTrace();

}finally{

}

return map;

}

参考如下

http://www.cnblogs.com/hongten/archive/2012/02/22/java2poi.html

http://www.cnblogs.com/hongten/p/java_poi_excel_xls_xlsx.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值