zoukankan      html  css  js  c++  java
  • java导入Excel

    前台:

     1   <form id = "import_form">
     2         <input id="import_file_name" name="" value="" type="text"/>
     3         <input id="import_file" onchange="showFileName()" name="file" value="" type="file"/>
     4         <input type="button" id = "import_box_sure" value="确定"/>
     5     </form>
     6     
     7     <script>
     8         function showFileName(){
     9             $("#import_file_name").val($("#import_file").val());
    10         }
    11         
    12         $(function(){
    13             
    14             $('#import_box_sure').live('click', function() {
    15                if(!$("import_file").val()){
    16                    alert("请导入文件");
    17                    return;
    18                }
    19               });
    20             
    21             $("#import_form").ajaxSubmit({
    22                 type:'post',
    23                 url:"./importUsers",
    24                 success:function(data){
    25                     if(data && data.code=="200"){
    26                         alert("导入成功");
    27                         //弹窗隐藏
    28                     }
    29                 }
    30             });
    31             
    32         }
    33         
    34     </script>

    后台:

     1 @RequestMapping(value="/importUsers",method = RequestMethod.POST)
     2     @ResponseBody
     3     public Object importUsers(@RequestParam MultipartFile file){
     4         excelToUserList(is);
     5     }
     6     
     7     public static  List<User> excelToUserList(InputStream is){
     8         final ArrayList<User> resultList = new ArrayList<User>();
     9         XSSFWorkbook wb = new XSSWorkbook(is);
    10         
    11         XSSFSheet sheet = wb.getSheetAt(0);
    12          
    13         for(int currentRowNum=0;currentRowNum<sheet.getLastRowNum();currentRowNum++){
    14             XSSFRow row = sheet.getRow(currentRowNum);
    15             
    16             if(null==row){
    17                 //遇到真正空行就退出
    18                 break;
    19             }
    20             //第一行是标题,第二行是表头,从第三行开始才是数据
    21             if(currentRowNum<2){
    22                 continue;
    23             }
    24             
    25             Map<String,Object> resp = rowIsEmpty(row);
    26             int code = (int)resp.get("code");
    27             //等于1说明当前行中没有有效数据,也认为是空行
    28             if (code==1) {
    29                 break;
    30             }
    31             
    32             //这里才开始真正遍历每行的单元格数据todo,方法参考 rowIsEmpty方法
    33             
    34         }
    35             
    36         
    37         
    38     }
    39     
    40     
    41     
    42     
    43     //判断当前行中是否有有效数据。当前行中每一个单元格中都没有有效数据(如空串""),则认为当前行没有数据
    44     private static Map<String,Object> rowIsEmpty(XSSFRow row){
    45         HashMap<String, Object> map = new HashMap<String,Object>();
    46         int cellNum = row.getLastCellNum();
    47         for(int j=0;j<cellNum;j++){
    48             XSSCell cell = row.getCell(j);
    49             if(cell==null || "".equals(cell.getStringCellValue())){
    50                 continue;
    51             }else{
    52                 //只要有一个单元格不满足,则此行不为空
    53                 map.put("code", 0);
    54                 return map;
    55             }
    56         }
    57         //当前行全部单元格都没有有效数据
    58         map.put("code", 1);
    59         return map;
    60     }
    61 }
  • 相关阅读:
    OpenGL纹理数据块
    MFC应用真彩色图标资源
    PictureConverter命令行图片批量转换工具
    Google Earth6.1 tree
    OpenGL Render On Window Process
    纹理滤波(Texture Filter)
    使用开源OpenCTM进行常用三维格式互导
    《搅基辞》
    访问WebDAV服务
    linux 挂载一个文件夹到另一个文件夹
  • 原文地址:https://www.cnblogs.com/libin6505/p/8601648.html
Copyright © 2011-2022 走看看