Java読み出しExcel例

3512 ワード

     
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;

   /**   
         * @param filePath excel    
         * @param col       
         * @return     (         String  )
         */
        public static List<List<String>> initExcle(String filePath, int col) {
                // className = Helper.upperFirst(f.getName()).split(".xls")[0];
                File f = new File(filePath);
                List<List<String>> list = null;
                InputStream is = null;
                HSSFWorkbook wb = null;
                try {
                        is = new FileInputStream(f);
                        wb = new HSSFWorkbook(is);
                } catch (Exception e) {
                        Helper.logException("Cannot find file or file cannot be reading!",
                                        e.fillInStackTrace());
                } finally {
                        try {
                                is.close();
                        } catch (IOException e) {
                                Helper.logException("Cannot close input stream!",
                                                e.fillInStackTrace());
                        }
                }
                // deal work book.
                if (null != wb) {
                        list = new ArrayList<List<String>>();
                        Sheet sheet = wb.getSheetAt(wb.getFirstVisibleTab());
                        for (int i = sheet.getFirstRowNum(); i <= sheet.getLastRowNum(); i++) {
                                List<String> args = new ArrayList<String>();
                                Row row = sheet.getRow(i);
                                for (int j = 0; j < row.getLastCellNum(); j++) {
                                        Cell c = row.getCell(j);
                                        if (null != c) {
                                                if (c.getCellType() == 0) {
                                                        args.add(c.getNumericCellValue() + "");
                                                } else if (c.getRichStringCellValue().toString().trim()
                                                                .equals("")) {
                                                        continue;
                                                } else {
                                                        args.add(c.getRichStringCellValue().toString()
                                                                        .trim());
                                                }
                                        }
                                }
                                if (args.size() != col)
                                        continue;
                                list.add(args);
                        }
                } else {
                        Helper.logError("Excel file has nothing!");
                }
                return list;
        }