EXCEL 가져오기

1734 단어 ExcelJ#

public static List<List> readExcel(File file, int beginRow, int endRow,
   String encoding) {
  List<List> list = new ArrayList<List>();
  Workbook workbook = null;
  try {
   WorkbookSettings wbs = new WorkbookSettings();
   //
   // excel is default ISO-8859-1 encoding
   // setEncoding("UTF-8"); utf-8 no support in excel
   //
   if (encoding == null || encoding.trim().equals("")) {
    wbs.setEncoding("ISO-8859-1");
   } else {
    wbs.setEncoding(encoding);
   }
   workbook = Workbook.getWorkbook(file, wbs);
     Sheet sheet = workbook.getSheet(0);
   int columns = sheet.getColumns();
   int rows = sheet.getRows();
   for (int j = 0; j < rows; j++) {
    //  beginRow 
    if (beginRow > 1 && beginRow > (j + 1)) {
     continue;
    }
    //  endRow 
    if (endRow < (j + 1)) {
     break;
    }
    List<Object> row = new ArrayList<Object>();
    String key = "";
    for (int i = 0; i < columns; i++) {
     key += (getValue(sheet.getCell(i, j)) == null ? ""
       : getValue(sheet.getCell(i, j)).toString().trim());
     row.add(getValue(sheet.getCell(i, j)));
    }
    if (!key.trim().equals("")) {
     // System.out.println("key--->:"+key);
     list.add(row);
    }
   }
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   if (workbook != null) {
    workbook.close();
   }
  }
  return list;
 }

좋은 웹페이지 즐겨찾기