Ejemplo n.º 1
0
  private void importDataFromExcel(final File fileToLoad, final long dateOfFile) {
    Workbook workbook = null;
    jxl.Workbook jxlWorkbook = null;
    BufferedInputStream bis = null;
    try {
      // use POI first
      bis = new BufferedInputStream(new FileInputStream(fileToLoad));
      workbook = WorkbookFactory.create(bis);
      jxlWorkbook = null;
    } catch (Exception e) {
      try {
        // then try JXL
        workbook = null;
        jxlWorkbook = jxl.Workbook.getWorkbook(fileToLoad);
      } catch (Exception ex) {
      }
    } finally {
      if (bis != null) {
        try {
          bis.close();
          bis = null;
        } catch (IOException ex) {
          Logger.getLogger(LoadDataToDB.class.getName()).log(Level.SEVERE, null, ex);
        }
      }
    }

    List<String[]> dataList = null;
    if (jxlWorkbook != null) {
      ExcelImportJXL excelImportJXL =
          new ExcelImportJXL(jxlWorkbook, DEALER_DETAILS_SHEET_NAME, START_ROW, START_COL);
      dataList = excelImportJXL.loadRows();
    } else if (workbook != null) {
      final String fileToLoadName = fileToLoad.getPath();
      final int pos = fileToLoadName.lastIndexOf(".");
      String fileExtension = "";
      if (pos >= 0) {
        fileExtension = fileToLoadName.substring(pos);
      }
      ExcelImportPOI excelImportPOI =
          new ExcelImportPOI(
              workbook, DEALER_DETAILS_SHEET_NAME, START_ROW, START_COL, fileExtension);
      dataList = excelImportPOI.loadRows();
    }

    if (dataList != null) {
      final List<ExcelRowDataModel> dataModels = createExcelRowDataModel(dataList);
      for (ExcelRowDataModel dataModel : dataModels) {
        insertOneRow(dataModel, dateOfFile);
      }
      dataList.clear();
      dataList = null;
    }

    workbook = null;
    jxlWorkbook = null;
    System.gc();
  }
Ejemplo n.º 2
0
  public static void test(final File fileToLoad) {
    Workbook workbook = null;
    jxl.Workbook jxlWorkbook = null;
    BufferedInputStream bis = null;
    try {
      // use POI first
      bis = new BufferedInputStream(new FileInputStream(fileToLoad));
      workbook = WorkbookFactory.create(bis);
      jxlWorkbook = null;
    } catch (Exception e) {
      e.printStackTrace();
      try {
        // then try JXL
        workbook = null;
        jxlWorkbook = jxl.Workbook.getWorkbook(fileToLoad);
      } catch (Exception ex) {
        ex.printStackTrace();
      }
    } finally {
      if (bis != null) {
        try {
          bis.close();
          bis = null;
        } catch (IOException ex) {
          Logger.getLogger(LoadDataToDB.class.getName()).log(Level.SEVERE, null, ex);
        }
      }
    }

    List<String[]> dataList = null;
    if (jxlWorkbook != null) {
      ExcelImportJXL excelImportJXL =
          new ExcelImportJXL(jxlWorkbook, DEALER_DETAILS_SHEET_NAME, START_ROW, START_COL);
      dataList = excelImportJXL.loadRows();
    } else if (workbook != null) {
      final String fileToLoadName = fileToLoad.getPath();
      final int pos = fileToLoadName.lastIndexOf(".");
      String fileExtension = "";
      if (pos >= 0) {
        fileExtension = fileToLoadName.substring(pos);
      }
      ExcelImportPOI excelImportPOI =
          new ExcelImportPOI(
              workbook, DEALER_DETAILS_SHEET_NAME, START_ROW, START_COL, fileExtension);
      dataList = excelImportPOI.loadRows();
    }

    System.out.println("==== dataList: " + dataList);
  }