zoukankan      html  css  js  c++  java
  • 【转】上传并解析excel

    【转载自】http://blog.csdn.net/u011563331/article/details/51322523

    通过解析excel,将数据存储到数据库中。现在将方法保存下来。

    使用的是apache的poi组件。

    public List<String[]> paseUserStoryFile(MultipartFile file) {
            List<String[]> list = new ArrayList<String[]>();
            try {
                POIFSFileSystem pois = new POIFSFileSystem(file.getInputStream());
                //新建WorkBook
                HSSFWorkbook wb = new HSSFWorkbook(pois);
                //获取Sheet(工作薄)总个数
                int sheetNumber = wb.getNumberOfSheets();
                for (int i = 0; i < sheetNumber; i++) {
                    //获取Sheet(工作薄)
                    HSSFSheet sheet = wb.getSheetAt(i);
                    //开始行数
                    int firstRow = sheet.getFirstRowNum();
                    //结束行数
                    int lastRow = sheet.getLastRowNum();
                    //判断该Sheet(工作薄)是否为空
                    boolean isEmpty = false;
                    if(firstRow == lastRow){
                        isEmpty = true;
                    }
                     
                    if(!isEmpty){
                        for (int j = firstRow+1; j <= lastRow; j++) {
                            //获取一行
                            HSSFRow row = sheet.getRow(j);
                            //开始列数
                            int firstCell = row.getFirstCellNum();
                            //结束列数
                            int lastCell = row.getLastCellNum();
                            //判断该行是否为空
                            String[] value = new String[lastCell]; 
                            if(firstCell != lastCell){
                                for (int k = firstCell; k < lastCell; k++) {
                                    
                                    
                                    
                                    //获取一个单元格
                                    HSSFCell cell = row.getCell(k);
                                    Object str = null;
                                    //获取单元格,值的类型
                                    int cellType = cell.getCellType();
                                    
                                    if(cellType == 0){
                                        str = cell.getNumericCellValue();
                                    }else if(cellType == 1){
                                        str = cell.getStringCellValue();
                                    }else if(cellType == 2){
                                    }else if(cellType == 4){
                                        str = cell.getBooleanCellValue();
                                    }
                                  value[k] = (String) str;
                                }
                                 
                            }
                            //每一行循环完对应的就是一个用户故事的所有属性全部拿到
                            list.add(value);  
                        }
                         
                    }
                     
                }
            } catch (IOException e) {
                
                e.printStackTrace();
            }
            return list;
        }

  • 相关阅读:
    Ubuntu 修改 ssh 登录后的欢迎信息
    Hbase的配置和安装
    python起的 simpleHTTPServer服务传输文件
    hadoop修改MR的提交的代码程序的副本数
    Fair Scheduler 队列设置经验总结
    调度系统任务创建---创建一个JoinTrigger的依赖任务(五)
    调度系统任务创建---创建一个MultiJob的任务(四)
    调度系统任务创建---创建一个有上下游依赖的任务(三)
    调度系统任务创建---创建一个简单调度任务(二)
    SSM框架新特性关于用Java配置类完全代替XML
  • 原文地址:https://www.cnblogs.com/nelucifer/p/5713174.html
Copyright © 2011-2022 走看看