List of usage examples for org.apache.poi.ss.usermodel Row getLastCellNum
short getLastCellNum();
From source file:javacommon.excel.ExcelReader.java
/** * ?Excel? Null// w ww . j a v a 2 s . co m * * @param sheetIndex 0 * @param rowIndex 0 * @return ? */ public List<Object> getRowData(int sheetIndex, int rowIndex) { Row row = getRow(sheetIndex, rowIndex); if (row == null) { return null; } List<Object> result = new Vector<Object>(); int cols = row.getLastCellNum(); // boolean isBlank = true; //? Object obj = null; for (int i = 0; i <= cols; i++) { obj = getCellValue(row.getCell(i)); result.add(obj); if (obj != null) { isBlank = false; } } return isBlank ? null : result; }
From source file:javacommon.excel.ExcelReader.java
/** * ???//w w w.ja va2 s . c o m * * @param sheetIndex 0 * @param rowIndex 0 * @param columnIndex 0 * @return */ public Object getCellValue(int sheetIndex, int rowIndex, int columnIndex) { Row row = getRow(sheetIndex, rowIndex); if (row != null && columnIndex <= row.getLastCellNum()) { return getCellValue(row.getCell(columnIndex)); } return null; }
From source file:javacommon.excel.ExcelReader.java
/** * ???/*from w w w. jav a 2s. com*/ * * @param sheetIndex 0 * @param rowIndex 0 * @param columnIndex 0 * @return */ public String getCellStringValue(int sheetIndex, int rowIndex, int columnIndex) { Row row = getRow(sheetIndex, rowIndex); if (row != null && columnIndex <= row.getLastCellNum()) { return getCellStringValue(row.getCell(columnIndex)); } return null; }
From source file:javacommon.excel.ExcelReader.java
/** * ?Excel? Null//from ww w . jav a 2 s . c o m * * @param sheetIndex 0 * @param rowIndex 0 * @return ? */ public List<String> getRowStingFormatData(int sheetIndex, int rowIndex) { List<String> result = new Vector<String>(); Row row = getRow(sheetIndex, rowIndex); if (row == null) { return null; } int cols = row.getLastCellNum(); // boolean isBlank = true;//? String str = null; for (int i = 0; i < cols; i++) { str = getCellStringFormatValue(row.getCell(i)); if (str != null && str.trim().length() > 0) { isBlank = false; } result.add(str); } return isBlank ? null : result; }
From source file:jexcel4py.Jexcel4py.java
public int getCols() { Sheet rdSheet = wbRead.getSheetAt(0); Row row = rdSheet.getRow(0); int rdCols = row.getLastCellNum(); return rdCols; }
From source file:jexcel4py.Jexcel4py.java
public void deliveryExcel(String strInExcelName) throws IOException { wbRead = readExcel(strInExcelName);/*from w w w .j av a 2 s . c o m*/ wbWrite = new HSSFWorkbook(); Sheet wrSheet = wbWrite.createSheet(); Sheet rdSheet = wbRead.getSheetAt(0); int rdRows = rdSheet.getLastRowNum();// ? for (int i = 0; i <= rdRows; i++) { Row wrRow = wrSheet.createRow(i); Row row = rdSheet.getRow(i); for (int j = row.getFirstCellNum(); j < row.getLastCellNum(); j++) { Cell rdCell = row.getCell(j); // System.out.print(rdCell + "\t"); Cell wrCell = wrRow.createCell(j); copyCell(rdCell, wrCell); } // System.out.println(); } // File fileOut = new File(strOutExcelName); // if(fileOut.exists()){ // fileOut.delete(); // } // FileOutputStream fosFileOut = new FileOutputStream(strOutExcelName); // wbWrite.write(fosFileOut); // fosFileOut.close(); // wbRead.close(); // wbWrite.close(); }
From source file:kaflib.utils.FileUtils.java
License:Open Source License
/** * Reads a spreadsheet to a set of matrices (one per worksheet). * @param file//from w ww. j ava 2 s . c om * @return * @throws Exception */ public static Map<String, Matrix<String>> readXLSX(final File file, final boolean columnTitles) throws Exception { Map<String, Matrix<String>> matrices = new HashMap<String, Matrix<String>>(); Workbook workbook = null; workbook = new XSSFWorkbook(new FileInputStream(file)); for (int i = 0; i < workbook.getNumberOfSheets(); i++) { String name = workbook.getSheetName(i); Sheet sheet = workbook.getSheetAt(i); if (sheet == null) { continue; } Matrix<String> matrix = new Matrix<String>(); int start = 0; if (columnTitles) { Row row = sheet.getRow(0); if (row != null) { List<String> labels = new ArrayList<String>(); for (int k = 0; k < row.getLastCellNum(); k++) { labels.add(row.getCell(k).toString()); } matrix.setColumnLabels(labels); } start = 1; } for (int j = start; j <= sheet.getLastRowNum(); j++) { Row row = sheet.getRow(j); if (row == null) { continue; } for (int k = 0; k <= row.getLastCellNum(); k++) { Cell cell = row.getCell(k); if (cell != null) { matrix.set(j - start, k, cell.toString()); } } } matrices.put(name, matrix); } workbook.close(); return matrices; }
From source file:massiveanalyserxryv2.ServiceTaskSearch.java
@Override protected Task createTask() { Task task;/*from w w w . j a v a 2 s. co m*/ task = new Task() { @Override protected Object call() throws Exception { // rcupration de la liste des mots cls // si il s'agit d'une base de donne slectionn dans la liste String path; if (modelDataSearch.getAbosoluthPathDb() == null) { path = System.getProperty("user.dir"); path = path + "/db/"; path = path + modelDataSearch.getNameDb(); } else { // sinon on cre le path avec le chemin absolu (fichier import) path = modelDataSearch.getAbosoluthPathDb(); } ArrayList<String> keyWords = new ArrayList<String>(); keyWords.clear(); // lecture for (String line : Files.readAllLines(Paths.get(path))) { keyWords.add(line); } // rcupration de la liste des contents du tableau excel Workbook book = WorkbookFactory.create(modelDataSearch.getFile()); // rcupration du sheet Sheet sheet = book.getSheet(modelDataSearch.getNameSheet()); // rcupration de la colonne int top = sheet.getFirstRowNum(); int down = sheet.getLastRowNum(); Row row = sheet.getRow(top); // on parse les column jusqu'a ce que le nom soit le meme que celui dans le modele short start = row.getFirstCellNum(); short end = row.getLastCellNum(); int indiceColumn = -1; for (short i = start; i <= end; i++) { if (row.getCell(i).getStringCellValue().equals(modelDataSearch.getNameColumn())) { // on connait l'indice de column indiceColumn = i; break; } } // cration de la liste des contents ArrayList<DataContent> listContent = new ArrayList<DataContent>(); listContent.clear(); for (int j = top; j <= down; j++) { if (sheet.getRow(j) != null) { if ((sheet.getRow(j).getCell(indiceColumn).getCellType() == CellType.STRING.getCode())) { DataContent data = new DataContent(j, sheet.getRow(j).getCell(indiceColumn).getStringCellValue()); listContent.add(data); } } } // recherches for (DataContent content : listContent) { for (String key : keyWords) { if (key.isEmpty()) continue; int res = content.getContent().toLowerCase().indexOf(key.toLowerCase()); if (res != -1) { DataResultat data = new DataResultat(content.getNumRow() + 1, content.getContent(), key); // +1 car dans le fichie excel les row commence 1 et pas 0 ob.add(data); } } } // Fermeture du workbook if (book != null) book.close(); //tableauResultat.setItems(ob); return (Object) ob; } }; return task; }
From source file:misuExcel.excelAdd.java
License:Open Source License
private void addTarget() { if (target != null) { if (saveType == 3) { Log.info(" "); Sheet sheet = target.getSheetAt(sheetNum_target); names = new ArrayList<String>(); for (int i = addJpanel.ignore_Rowtar; i <= sheet.getLastRowNum(); i++) { Row row = sheet.getRow(i); if (row != null) { Cell cell = row.getCell(cellNum_target); if (cell != null) { names.add(getCellString(cell)); Log.info(" " + getCellString(cell)); }/*from w ww . j av a2 s . c om*/ } } //end for } else if (saveType == 4) { Log.info(" "); Sheet sheet = target.getSheetAt(sheetNum_target); names = new ArrayList<String>(); Row row = sheet.getRow(cellNum_target); if (row != null) { for (int i = addJpanel.ignore_Celltar; i < row.getLastCellNum(); i++) { Cell cell = row.getCell(i); if (cell != null) { names.add(getCellString(cell)); Log.info(" " + getCellString(cell)); } } //end for } } Log.info("names size:" + names.size()); Log.info("splitTarget is already"); } }
From source file:misuExcel.excelAdd.java
License:Open Source License
private void examExcel02() { Log.info("excamExcel02"); if (excel != null) { if (names != null && names.size() > 0) { Sheet sheet_add = excel.getSheetAt(sheetNum); Sheet sheet = target.getSheetAt(sheetNum_target); Row row_add = sheet_add.getRow(cellNum); Row row = sheet.getRow(cellNum_target); initList(names.size());// w ww .j av a 2 s . co m nones = new ArrayList<Integer>(); Boolean isAdd = false; if (row != null) { for (int i = addJpanel.ignore_Cell; i < row.getLastCellNum(); i++) { Cell cell = row.getCell(i); if (cell != null) { String str = getCellString(cell); Log.info(" " + str); for (int k = addJpanel.ignore_Celltar; k < row_add.getLastCellNum(); k++) { Cell cell2 = row_add.getCell(k); if (cell2 != null && str.equals(getCellString(cell2))) { isAdd = true; addList.get(0).add(k); break; } } } } //end names for if (!isAdd) { nones.add(row.getRowNum()); } isAdd = false; } // }//end for Log.info("examExcel is already"); } else { Log.warm("target is none"); } } else { Log.warm("excel is not exit"); } }