List of usage examples for org.apache.poi.hssf.usermodel HSSFWorkbook getSheetName
@Override public String getSheetName(int sheetIndex)
From source file:tournoi.ExcelToBDD.java
public boolean insertTable(String filePath) { boolean flag = true; db = new DBConnection(); con = db.getConnection();/* ww w .java 2 s .c o m*/ try { // ??excel FileInputStream fin = new FileInputStream(filePath); // HSSFWorkbook workbook = new HSSFWorkbook(fin); //Get the workbook instance for XLS file HSSFSheet sheet = workbook.getSheetAt(2);//Get first sheet from the workbook System.out.println(workbook.getSheetName(2)); HSSFRow row = null;// excel HSSFCell cell = null; HSSFCell cell2 = null; //HSSFCell cell3 = null; int totalRow = sheet.getLastRowNum();// excel? System.out.println(totalRow); // ? for (int i = 1; i <= totalRow; i++) { row = sheet.getRow(i); cell = row.getCell(0); int id_table = (int) cell.getNumericCellValue(); cell2 = row.getCell(1); int id_salle = (int) cell2.getNumericCellValue(); // cell3 = row.getCell(2); // String prenom = cell3.getRichStringCellValue().toString(); // cell4 = row.getCell(3); // int national_point = (int) cell4.getNumericCellValue(); // cell5 = row.getCell(4); // String categorie = cell5.getRichStringCellValue().toString(); //String sql = "INSERT INTO text1(ID,BM,AQ,CQ,DQ) VALUES (?,?,?,?,?)"; // " String sql = "INSERT INTO tables VALUES('" + id_table + "','" + id_salle + "')"; pst = con.prepareStatement(sql); pst.execute(); System.out.println("Import rows " + i); } //con.commit(); fin.close(); System.out.println("Success import excel to mysql table"); } catch (FileNotFoundException e) { flag = false; System.out.println("MYSQL ERROR:" + e.getMessage()); } catch (IOException ex) { flag = false; System.out.println("MYSQL ERROR:" + ex.getMessage()); } catch (SQLException exx) { flag = false; System.out.println("MYSQL ERROR:" + exx.getMessage()); } finally { try { pst.close(); con.close(); } catch (SQLException e) { System.out.println("MYSQL ERROR:" + e.getMessage()); } } return flag; }
From source file:ubic.BAMSandAllen.AllenDataLoaders.AllenTop50DataLoader.java
License:Apache License
public AllenTop50DataLoader() { try {//w ww. ja v a2 s . c o m NeuroNamesMappingLoader NNLoader = new NeuroNamesMappingLoader(); allenRegions = new HashSet<String>(); POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(spreadSheetLocation)); HSSFWorkbook allen = new HSSFWorkbook(fs); HSSFSheet sheet; for (int i = 0; i < allen.getNumberOfSheets(); i++) { sheet = allen.getSheetAt(i); allenRegions.add(allen.getSheetName(i)); // System.out.println( allen.getSheetName( i ) ); } Set<String> allenRegions = getAllenRegions(); Set<NomenClatureEntry> dongEntries = NNLoader.getDongEntries(); allenEnrichedEntries = new HashSet<NomenClatureEntry>(); for (String allenEnrichedAcro : allenRegions) { for (NomenClatureEntry dongEntry : dongEntries) { if (allenEnrichedAcro.equals(dongEntry.acro)) { allenEnrichedEntries.add(dongEntry); } } } // make entry <-> gene links for (NomenClatureEntry dongEntry : allenEnrichedEntries) { sheet = allen.getSheet(dongEntry.acro); // System.out.println(dongEntry.acro); for (short i = 2; true; i++) { String gene = ExcelUtil.getValue(sheet, i, (short) 0); if (gene == null) break; dongEntry.expressedGenes.add(gene); // System.out.println( gene ); } } } catch (Exception e) { e.printStackTrace(); System.exit(1); } }
From source file:vone.HSSFReadWrite.java
License:Apache License
/** * Method main//from w ww . j a va2 s. c om * * Given 1 argument takes that as the filename, inputs it and dumps the cell * values/types out to sys.out.<br/> * * given 2 arguments where the second argument is the word "write" and the * first is the filename - writes out a sample (test) spreadsheet see * {@link HSSFReadWrite#testCreateSampleSheet(String)}.<br/> * * given 2 arguments where the first is an input filename and the second an * output filename (not write), attempts to fully read in the spreadsheet * and fully write it out.<br/> * * given 3 arguments where the first is an input filename and the second an * output filename (not write) and the third is "modify1", attempts to read * in the spreadsheet, deletes rows 0-24, 74-99. Changes cell at row 39, col * 3 to "MODIFIED CELL" then writes it out. Hence this is "modify test 1". * If you take the output from the write test, you'll have a valid scenario. */ @SuppressWarnings("deprecation") public static void main(String[] args) { if (args.length < 1) { System.err.println("At least one argument expected"); return; } String fileName = args[0]; try { if (args.length < 2) { HSSFWorkbook wb = HSSFReadWrite.readFile(fileName); System.out.println("Data dump:\n"); for (int k = 0; k < wb.getNumberOfSheets(); k++) { HSSFSheet sheet = wb.getSheetAt(k); int rows = sheet.getPhysicalNumberOfRows(); System.out.println("Sheet " + k + " \"" + wb.getSheetName(k) + "\" has " + rows + " row(s)."); for (int r = 0; r < rows; r++) { HSSFRow row = sheet.getRow(r); if (row == null) { continue; } // int cells = row.getPhysicalNumberOfCells(); int cells = row.getLastCellNum(); System.out.println("\nROW " + row.getRowNum() + " has " + cells + " cell(s)."); for (int c = 0; c < cells; c++) { HSSFCell cell = row.getCell(c); String value = null; if (cell == null) { System.out.println("CELL col=" + c + " VALUE= Empty"); continue; } switch (cell.getCellTypeEnum()) { case FORMULA: value = "FORMULA value=" + cell.getCellFormula(); break; case NUMERIC: value = "NUMERIC value=" + cell.getNumericCellValue(); break; case STRING: value = "STRING value=" + cell.getStringCellValue(); break; default: } System.out.println("CELL col=" + cell.getColumnIndex() + " VALUE=" + value); } } } wb.close(); } } catch (Exception e) { e.printStackTrace(); } }
From source file:war.controller.WorkboardController.java
License:Open Source License
/** * Extracts and construct an engineering model asset from the given Excel sheet * @return EngineeringModelAsset: the asset object extracted from the file *//* w w w.ja v a 2 s. co m*/ private EngineeringModelAsset extractEngineeringOutputAsset(HSSFWorkbook workbook) { HSSFSheet sheet = workbook.getSheetAt(0); // Supposedly always 1 sheet only (index 0) String assetCode = workbook.getSheetName(0); // The asset code is the name of the sheet Row row = sheet.getRow(25); // Line 25 is the 1st row containing data // Extract data from columns H to U int assetYear = (int) (row.getCell(7).getNumericCellValue()); String assetDescription = row.getCell(8).getStringCellValue(); String assetZone = row.getCell(9).getStringCellValue(); Double assetDistanceFromCoast = row.getCell(10).getNumericCellValue(); String assetExposureClass = row.getCell(11).getStringCellValue(); String assetCarbonationClass = row.getCell(12).getStringCellValue(); String assetChlorideClass = row.getCell(13).getStringCellValue(); Double assetCover = row.getCell(15).getNumericCellValue(); Double assetDMember = row.getCell(16).getNumericCellValue(); Double assetFPrimeC = row.getCell(17).getNumericCellValue(); Double assetWc = row.getCell(18).getNumericCellValue(); Double assetCe = row.getCell(19).getNumericCellValue(); Double assetDbar = row.getCell(20).getNumericCellValue(); // Creates the Asset object EngineeringModelAsset asset = new EngineeringModelAsset(assetCode, assetDescription, assetYear, assetZone, assetDistanceFromCoast, assetExposureClass, assetCarbonationClass, assetChlorideClass, assetCover, assetDMember, assetFPrimeC, assetWc, assetCe, assetDbar); return asset; }