首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

java读Excel2003跟2007

2013-07-09 
java读Excel2003和2007首先要去http://poi.apache.org/download.html下载poi包 下最新的就可以。附件里也有

java读Excel2003和2007
首先要去http://poi.apache.org/download.html下载poi包 下最新的就可以。
附件里也有,是3.9版本的。可以读取2003 和2007的excel.具体的代码在下面.


public static void importUser(File file,UserService userService) throws Exception{        if(file.getName().endsWith("xlsx")){            importUser2007(file,userService);        }else if (file.getName().endsWith("xls")){            importUser2003(file,userService);        }    }


private static void importUser2003(File file,UserService userService) throws Exception{        FileInputStream fs = null;        try {            fs = new FileInputStream(file);        } catch (FileNotFoundException e) {            e.printStackTrace();            return;        }        // 创建对Excel工作簿文件的引用        HSSFWorkbook workbook = ExcelTool.getWorkbook(fs);        //获取sheet数        int sheetNum = ExcelUtils.getNumberOfSheets(workbook);        /**         * 遍历excle的每一个工作薄         */        for (int numSheets = 0; numSheets < sheetNum; numSheets++) {            if (null != workbook.getSheetAt(numSheets)) {                //获得一个sheet                HSSFSheet aSheet = ExcelTool.getSheetAt(workbook, numSheets);                int lastRowNum = ExcelUtils.getLastRowNum(aSheet);                if(lastRowNum == 0)continue;                int startRow = 2;//第一行是行头,所以直接取第二行                for (int rowNumOfSheet = startRow; rowNumOfSheet <= lastRowNum; rowNumOfSheet++) {                    User user = new User();                    HSSFRow aRow = aSheet.getRow(rowNumOfSheet);                    String name = "" ;                    HSSFCell docIdCell = ExcelTool.getCell(aRow, 0);                    if (null != docIdCell) {                        String cellStrValue = ExcelUtils.getCellStrValue(docIdCell);                        name = cellStrValue;                    }                    user.setName(name);                    String age = "0";                    HSSFCell ageCell = ExcelTool.getCell(aRow, 1);                    if (null != ageCell) {                        String cellStrValue = ExcelUtils.getCellStrValue(ageCell);                        age = cellStrValue;                    }                    userService.addUser(user);//增加用户                }            }        }    }


private static void importUser2007(File file,UserService userService) throws Exception{        FileInputStream fs = null;        try {            fs = new FileInputStream(file);        } catch (FileNotFoundException e) {            e.printStackTrace();            return;        }        // 创建对Excel工作簿文件的引用        XSSFWorkbook workbook = new XSSFWorkbook(fs);        //获取sheet数        int sheetNum = ExcelUtils.getNumberOfSheets(workbook);        /**         * 遍历excle的每一个工作薄         */        for (int numSheets = 0; numSheets < sheetNum; numSheets++) {            if (null != workbook.getSheetAt(numSheets)) {                //获得一个sheet                XSSFSheet aSheet = workbook.getSheetAt(numSheets);                int lastRowNum = ExcelUtils.getLastRowNum(aSheet);                if(lastRowNum == 0)continue;                int startRow = 2;//第一行是行头,所以直接取第二行                for (int rowNumOfSheet = startRow; rowNumOfSheet <= lastRowNum; rowNumOfSheet++) {                    User user = new User();                    XSSFRow aRow = aSheet.getRow(rowNumOfSheet);                    String name = "" ;                    XSSFCell docIdCell = aRow.getCell(0);                    if (null != docIdCell) {                        String cellStrValue = ExcelUtils.getCellStrValue(docIdCell);                        name = cellStrValue;                    }                    user.setName(name);                    String age = "0";                    XSSFCell ageCell = aRow.getCell(1);                    if (null != ageCell) {                        String cellStrValue = ExcelUtils.getCellStrValue(ageCell);                        age = cellStrValue;                    }                    userService.addUser(user);//增加用户                }            }        }    }

热点排行