zoukankan      html  css  js  c++  java
  • Ajax上传.xlsx文件并解析

    1、首先必须配置bean

    <bean id="multipartResolver"
              class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
            <property name="maxUploadSize" value="10485760"/>
            <property name="resolveLazily" value="true"/>
            <property name="maxInMemorySize" value="4096"/>
            <property name="defaultEncoding" value="UTF-8"/>
        </bean>
    

    2、ftl页面代码

    <form id="fileForm" method="post" enctype="multipart/form-data">
            <input type="text" id="someText" name="someText">
            <input type="file" name="file" id="file">
    </form>
    

    3、js代码

    var someText= $("#someText").val();
    var file = $("#file")[0].files[0]; //获取标签值好像函数与浏览器不兼容,网上的大多是通过$("#file").files[0]获取,          
                          我这里必须通过$("#file")[0].files[0]才能获取到值        
    var formData = new FormData();    
    formData.append('someText', someText);
    formData.append('file', file);
    
    $.ajax({
                 type:"post",
                 url:'请求路径',
                 data:formData,
                 dataType:'json',
                 cache:false,
                 contentType: false, 
                 processData: false,
                 success: function(data) {
             	// 结果处理
             			}
            });    
    

    4、后台代码

    @RequestMapping(value = "路径", method = { RequestMethod.GET, RequestMethod.POST })
    @ResponseBody
    public Object uploadFile(@RequestParam(value = "someText", required = false) String someText, 
    @RequestParam(value = "file", required = false) MultipartFile file, 
    HttpServletRequest request) {//required = false才能获取到值            
    	XSSFWorkbook workbook = new XSSFWorkbook(file.getInputStream());
      XSSFWorkbook workbook = new XSSFWorkbook(file.getInputStream());        
      for (int numSheet = 0; numSheet < workbook.getNumberOfSheets(); numSheet++) {
           XSSFSheet sheetAt = workbook.getSheetAt(numSheet);
           if (sheetAt == null) {
               continue;
               }
           // 循环行Row
           for (int rowNum = 0; rowNum <= sheetAt.getLastRowNum(); rowNum++) {
                XSSFRow row = sheetAt.getRow(rowNum);
                if (row == null) {
                    continue;
                }
                // 循环列Cell
          for (int cellNum = 0; cellNum <= row.getLastCellNum(); cellNum++) {
              //在此可进行获取单元格值并入库
    										getValue(row,cellNum);
           	}
          		}
       	}  
    }   
    //单元格类型转换并获取值
    private String getValue(XSSFRow row, int cellNum) {
            XSSFCell cell = row.getCell(cellNum);
            if (cell.getCellType() == cell.CELL_TYPE_BOOLEAN) {
                return String.valueOf(cell.getBooleanCellValue());
            } else if (cell.getCellType() == cell.CELL_TYPE_NUMERIC) {
                return String.valueOf((int) cell.getNumericCellValue());
            } else {
                return String.valueOf(cell.getStringCellValue()).equals("") ? "" : String
                        .valueOf(cell.getStringCellValue());
            }
    }

                       

      

  • 相关阅读:
    P3478 [POI2008]STA-Station
    P2015 二叉苹果树
    P2014 选课 (树型背包模版)
    求树的每个子树的重心
    求树的直径
    Javascript--防抖与节流
    JavaScript中call和apply的区别
    解决谷歌浏览器“此Flash Player与您的地区不相容,请重新安装Flash”问题(最新版)
    matlab实验代码(总)
    表达式树
  • 原文地址:https://www.cnblogs.com/yiyibinbin/p/9467926.html
Copyright © 2011-2022 走看看