需要的jar包
poi-3.9-20121203.jar
poi-ooxml-3.9-20121203.jar
poi-ooxml-schemas-3.9-20121203.jar
xmlbeans-2.3.0_poi.jar
-
-
-
-
-
-
-
- public List<String[]> readByPoi(String fileName,int sheetNumber) {
- org.apache.poi.ss.usermodel.Workbook workbook = null;
- List<String[]> list = new ArrayList<String[]>();
- try {
- String fileType=fileName.substring(fileName.lastIndexOf(".")+1,fileName.length());
- if (fileType.equals("xls")) {
- workbook = new HSSFWorkbook(new FileInputStream(fileName));
- }
- else if(fileType.equals("xlsx"))
- {
- workbook = new XSSFWorkbook(new FileInputStream(fileName));
- }
- else
- {
- log.info("您的文档格式不正确!");
- }
-
- org.apache.poi.ss.usermodel.Sheet sheet = workbook.getSheetAt(sheetNumber);
- int firstRowIndex = sheet.getFirstRowNum();
- int lastRowIndex = sheet.getLastRowNum();
- int coloumNum=sheet.getRow(0).getPhysicalNumberOfCells();
- for(int rIndex = firstRowIndex; rIndex <= lastRowIndex; rIndex ++){
- Row row = sheet.getRow(rIndex);
- if(row != null){
- int firstCellIndex = row.getFirstCellNum();
- int lastCellIndex = row.getLastCellNum();
- String[] s=new String[coloumNum];
- for(int cIndex = firstCellIndex; cIndex < lastCellIndex; cIndex ++){
- Cell cell = row.getCell(cIndex);
- String value = "";
- int type = cell.getCellType();
-
- if(cell != null){
- value = cell.toString();
- if (Cell.CELL_TYPE_NUMERIC == type) {
-
- Double val = cell.getNumericCellValue();
- if(val == val.longValue()){
- value= "" + val.longValue();
- }
- }
-
- s[cIndex]=value;
- }
- }
- list.add(s);
- }
- }
-
- } catch (Exception e) {
-
- e.printStackTrace();
-
- }
- return list;
- }
|