SpringMVC + jxl.jar 解析Ecexl 文件

页面测试代码:

<form  method="post" enctype="multipart/form-data" action="/formexecl/fe">
        <input type="file" name="testfile">
        <input type="submit" value="提交">
</form>

SpringMVC 后台接受代码:

package com.flow.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;



@Controller
//注释 映射请求URL
@RequestMapping(value="/formexecl")
public class Form_ExeclController {
	/**
	 * 
	 * @param response
	 * @param file
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value  = "/fe")
	public String jxExecl(HttpServletRequest req)throws Exception{
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) req;           
		// 获得文件:     
		Map<String,MultipartFile> files = multipartRequest.getFileMap(); 
		MultipartFile mfile = (MultipartFile) files.get("testfile");
		
		try {
			Workbook rwb = Workbook.getWorkbook(mfile.getInputStream());
			Sheet rs = rwb.getSheet(0);
			int clos = rs.getColumns();//得到所有的列
			int rows = rs.getRows();//得到所有的行
			for (int j = 0; j < rows; j++) {
				for (int i = 0; i < clos; i++) {
					  Cell cell = rs.getCell(i, j);
					  String result = cell.getContents();
			          System.out.print(result);
				}
				 System.out.println("");
			}
			rwb.close();
		} catch (Exception e) {
            System.out.println(e);
        }
		return "url";
	}
	
	@RequestMapping(value  = "/page")
	public ModelAndView intoPage() throws Exception{
		ModelAndView mav = new  ModelAndView();
		mav.setViewName("links/formExecl");
		return mav;
	}
	
	
}

 

posted @ 2016-09-07 09:52  Moka<TI  阅读(643)  评论(0编辑  收藏  举报