List of usage examples for org.apache.poi.ss.usermodel Cell getStringCellValue
String getStringCellValue();
For numeric cells we throw an exception.
From source file:Algorithm.Method2.java
public String[] MethodTest(String Path) { StringBuffer keyword1 = new StringBuffer(); try {/*from ww w. j a v a 2 s . co m*/ /** * ??excel * src * map? * result */ String srcFilePath = Path; String mapFilePath = "??.xlsx"; String newFilePath = Path.replace("???.xlsx", "method2.xlsx"); FileInputStream srcFile = new FileInputStream(new File(srcFilePath)); FileInputStream mapFile = new FileInputStream(new File(mapFilePath)); XSSFWorkbook srcWorkbook = new XSSFWorkbook(srcFile); XSSFWorkbook mapWorkbook = new XSSFWorkbook(mapFile); XSSFWorkbook newWorkbook = new XSSFWorkbook(); XSSFSheet srcSheet = srcWorkbook.getSheetAt(0); XSSFSheet mapSheet = mapWorkbook.getSheetAt(0); XSSFSheet newSheet = newWorkbook.createSheet("test"); Iterator<Row> srcRowIterator = srcSheet.iterator(); Iterator<Row> mapRowIterator = mapSheet.iterator(); //map.xlst???mapList ArrayList mapList = new ArrayList(); while (mapRowIterator.hasNext()) { Row row = mapRowIterator.next(); Iterator<Cell> cellIterator = row.cellIterator(); while (cellIterator.hasNext()) { Cell cell = cellIterator.next(); String test = cell.getStringCellValue(); mapList.add(test); } } //???mapArray,? int mapListLength = mapList.size(); String[] mapArray = (String[]) mapList.toArray(new String[mapListLength]); //??mapResult,mapArray?src //,mapArray[i]src,mapResult[i]1?0 int[] mapResult = new int[mapListLength]; for (int i = 0; i < mapListLength; i++) { mapResult[i] = 0; } //?src.xlsx,?mapArray while (srcRowIterator.hasNext()) { Row row = srcRowIterator.next(); Iterator<Cell> cellIterator = row.cellIterator(); while (cellIterator.hasNext()) { Cell cell = cellIterator.next(); String test = cell.getStringCellValue().trim(); for (int i = 0; i < mapListLength; i++) { if (test.equals(mapArray[i])) mapResult[i] = 1; } } } //?mapResult,mapArrayresult int newRowNum = 0; for (int i = mapListLength - 1; i >= 0; i--) { if (mapResult[i] == 1) { Row newRow = newSheet.createRow(newRowNum++); Cell newCell = newRow.createCell(0); String test = mapArray[i]; keyword1.append(test + " "); newCell.setCellValue(test); } } srcFile.close(); mapFile.close(); //FileOutputStream fileOut = new FileOutputStream(new File(newFilePath)); // newWorkbook.write(fileOut); // fileOut.close(); // System.out.print("method2"); } catch (Exception e) { System.out.println(e); } return keyword1.toString().split(" "); }
From source file:Algorithm.SegmentationAndNounFilter.java
public String[] SegmentationNounFilter(String filepath, String filename) { try {/* w ww . j a v a2s.c o m*/ String mingciPath = filepath.replace(filename, "???.xlsx"); ICTCLAS50 testICTCLAS50 = new ICTCLAS50(); String argu = "."; if (testICTCLAS50.ICTCLAS_Init(argu.getBytes("GB2312")) == false) { System.out.println("Init Fail"); } else { System.out.println("Init Succeed!"); } StringBuffer input = new StringBuffer(); FileInputStream file = new FileInputStream(new File(filepath)); XSSFWorkbook workbook = new XSSFWorkbook(file); XSSFSheet sheet1 = workbook.getSheetAt(0); Iterator<Row> rowIterator = sheet1.iterator(); while (rowIterator.hasNext()) { Row row = rowIterator.next(); //Row rowNew =sheetNew.createRow(rowNumNew++); //For each row, iterate through all the columns Iterator<Cell> cellIterator = row.cellIterator(); while (cellIterator.hasNext()) { Cell cell = cellIterator.next(); // Cell cellNew =rowNew.createCell(cellNumNew++); //Check the cell type and format accordingly switch (cell.getCellType()) { case Cell.CELL_TYPE_NUMERIC: break; case Cell.CELL_TYPE_STRING: input.append(cell.getStringCellValue()); } } //System.out.println(""); } //? byte nativeBytes[] = testICTCLAS50.ICTCLAS_ParagraphProcess(input.toString().getBytes("GB2312"), 0, 1); String nativeStr = new String(nativeBytes, 0, nativeBytes.length, "GB2312"); //xlsx XSSFWorkbook workbookNew = new XSSFWorkbook(); XSSFSheet sheetNew = workbookNew.createSheet("test"); int rowNum = 0; // WritableWorkbook book = Workbook.createWorkbook(new File("n.xls")); // WritableSheet sheet = book.createSheet("num1", 0); Scanner in = new Scanner(nativeStr); int i = 0;// while (in.hasNext()) { String ss = in.next(); Pattern pattern = Pattern.compile("(.+?)/n.*"); Matcher matcher = pattern.matcher(ss); if (matcher.find() && matcher.group(1).length() > 1 && !isDigit(matcher.group(1))) { //label = new jxl.write.Label(0, i, matcher.group(1));//? //sheet.addCell(label); Row rowNew = sheetNew.createRow(rowNum++); Cell cellNew = rowNew.createCell(0); cellNew.setCellValue(matcher.group(1)); //i++; } } // book.write(); // book.close(); FileOutputStream fileOut = new FileOutputStream(new File(mingciPath));//new file workbookNew.write(fileOut); fileOut.close(); //?? file.close(); testICTCLAS50.ICTCLAS_Exit(); } catch (Exception ex) { } return null; }
From source file:ambit2.core.io.IteratingXLSReader.java
License:Open Source License
public Object next() { IAtomContainer mol = null;//from w w w . ja v a 2s. c o m Map properties = new Hashtable(); try { Row row = (Row) iterator.next(); for (int col = 0; col < getNumberOfColumns(); col++) { Cell cell = row.getCell(col); Object value = null; if (cell != null) switch (cell.getCellType()) { case Cell.CELL_TYPE_BOOLEAN: value = cell.getBooleanCellValue(); break; case Cell.CELL_TYPE_NUMERIC: value = cell.getNumericCellValue(); break; case Cell.CELL_TYPE_STRING: value = cell.getStringCellValue(); break; case Cell.CELL_TYPE_BLANK: value = ""; break; case Cell.CELL_TYPE_ERROR: value = ""; break; case Cell.CELL_TYPE_FORMULA: try { value = cell.getStringCellValue(); break; } catch (Exception x) { try { value = cell.getNumericCellValue(); } catch (Exception z) { logger.log(Level.WARNING, x.getMessage(), x); } } } else value = ""; try { if (smilesIndex == col) { try { mol = sp.parseSmiles(value.toString()); properties.put(AmbitCONSTANTS.SMILES, value.toString()); } catch (InvalidSmilesException x) { logger.warning("Invalid SMILES!\t" + value); properties.put(AmbitCONSTANTS.SMILES, "Invalid SMILES"); } } else if (col < getNumberOfColumns()) properties.put(getHeaderColumn(col), value); } catch (Exception x) { logger.log(Level.WARNING, x.getMessage(), x); } } if (mol == null) mol = SilentChemObjectBuilder.getInstance().newInstance(IMolecule.class); mol.setProperties(properties); processRow(mol); } catch (Exception x) { logger.log(Level.SEVERE, x.getMessage(), x); } return mol; }
From source file:ambit2.core.io.IteratingXLSReader.java
License:Open Source License
protected void processHeader(Row row) { Iterator cols = row.cellIterator(); TreeMap columns = new TreeMap(); while (cols.hasNext()) { Cell cell = (Cell) cols.next(); String value = cell.getStringCellValue(); if (value.equals(defaultSMILESHeader)) smilesIndex = cell.getColumnIndex(); columns.put(new Integer(cell.getColumnIndex()), value); }// ww w . jav a2s. c o m Iterator i = columns.keySet().iterator(); while (i.hasNext()) { Integer key = (Integer) i.next(); setHeaderColumn(key.intValue(), columns.get(key).toString()); } }
From source file:android_connector.ExcelReader.java
/** * Gibt eine vernnftige Darstellung einer Zelle als String zurck. * @param cell die Zelle/*w w w . ja va 2 s . c om*/ * @return z.B. bei Zelle, die eine Gleichung enthlt, deren Ergebnis */ private String differCellType(Cell cell) { String returnValue = ""; switch (cell.getCellType()) { case Cell.CELL_TYPE_BOOLEAN: returnValue = String.valueOf(cell.getBooleanCellValue()); break; case Cell.CELL_TYPE_NUMERIC: returnValue = String.valueOf(cell.getNumericCellValue()); break; case Cell.CELL_TYPE_STRING: returnValue = cell.getStringCellValue(); break; case Cell.CELL_TYPE_FORMULA: FormulaEvaluator evaluator = this.wb.getCreationHelper().createFormulaEvaluator(); CellValue cellValue = evaluator.evaluate(cell); returnValue = cellValue.getStringValue(); break; case Cell.CELL_TYPE_ERROR: returnValue = String.valueOf(cell.getErrorCellValue()); break; case Cell.CELL_TYPE_BLANK: returnValue = ""; break; default: returnValue = "default value at (" + cell.getRowIndex() + ";" + cell.getColumnIndex() + ") !"; break; } return returnValue; }
From source file:annualleave.PersonelTara.java
static public void Detection(int ilk, int son, String URL) throws FileNotFoundException, IOException { char gun[] = new char[100]; int gunler[] = new int[32]; for (int j = ilk; j <= son; j++) { gunler[j] = j;/*from ww w. j a v a2 s.c o m*/ } String oncekiAd; // C:\\Users\\talha\\Documents\\NetBeansProjects\\AnnualLeave\\src\\annualleave\\Mays 23.xlsx String excelFilePath = URL; FileInputStream inputStream = new FileInputStream(new File(excelFilePath)); Workbook workbook = new XSSFWorkbook(inputStream); Sheet firstSheet = workbook.getSheetAt(0); int sonuncuIndex = firstSheet.getLastRowNum(); Iterator<Row> iterator = firstSheet.iterator(); while (iterator.hasNext()) { Row nextRow = iterator.next(); Iterator<Cell> cellIterator = nextRow.cellIterator(); if (cellIterator.hasNext()) { Cell cell = cellIterator.next(); if (cell.getRowIndex() >= 6) { oncekiAd = ad; cell = cellIterator.next(); cell = cellIterator.next(); cell = cellIterator.next(); ad = cell.getStringCellValue(); cell = cellIterator.next(); cell = cellIterator.next(); tarih = cell.getStringCellValue(); if (ad != oncekiAd && i != 0 && !(oncekiAd.equals("Personel Ad Soyad")) && !(oncekiAd.isEmpty()) && !(ad.isEmpty()) && !(ad.equals("Personel Ad Soyad")) || cell.getRowIndex() == sonuncuIndex) { for (int j = ilk; j <= son; j++) { if (gunler[j] != 0) { GETIR[z] = oncekiAd + " " + gunler[j]; z++; test = 1; } if (isBuilt) { int left = tarih.indexOf("."); int right = tarih.lastIndexOf("."); String sub = tarih.substring(left + 1, right); ay = Integer.parseInt(sub); int left2 = tarih.lastIndexOf("."); int right2 = tarih.lastIndexOf(""); String sub2 = tarih.substring(left2 + 1, right2); yil = Integer.parseInt(sub2); } Build(); } if (test == 1) { yeniAd[c] = oncekiAd; for (int j = ilk; j <= son; j++) { if (gunler[j] != 0) { Calendar date = Calendar.getInstance(); date.set(yil, ay - 1, gunler[j]); if (date.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || date.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) { } else { yeniAd[c] += " " + gunler[j]; } } } c++; test = 0; } for (int j = ilk; j <= son; j++) { gunler[j] = j; } } if (!(cell.getStringCellValue().isEmpty()) && !(ad.equals("Personel Ad Soyad"))) { int left = tarih.indexOf(0); int right = tarih.indexOf("."); String sub = tarih.substring(left + 1, right); gunler[Integer.parseInt(sub)] = 0; i++; } } } } }
From source file:apm.common.utils.excel.ImportExcel.java
License:Open Source License
/** * ??/*from www . j a va2 s. co m*/ * @param row ? * @param column ??? * @return ? */ public Object getCellValue(Row row, int column) { Object val = ""; try { Cell cell = row.getCell(column); if (cell != null) { if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) { val = cell.getNumericCellValue(); } else if (cell.getCellType() == Cell.CELL_TYPE_STRING) { val = cell.getStringCellValue(); } else if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) { val = cell.getCellFormula(); } else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) { val = cell.getBooleanCellValue(); } else if (cell.getCellType() == Cell.CELL_TYPE_ERROR) { val = cell.getErrorCellValue(); } } } catch (Exception e) { return val; } return val; }
From source file:at.mukprojects.exclycore.dao.XLSXReader.java
License:Open Source License
private ExclyString readString(Cell cell, int type) throws Exception { ExclyString output = null;/*from w w w . ja va 2 s .c o m*/ if (type == Cell.CELL_TYPE_STRING) { output = new ExclyString(cell.getStringCellValue()); } else if (type == Cell.CELL_TYPE_ERROR) { output = new ExclyStringError(); } else if (type == Cell.CELL_TYPE_FORMULA) { int formulaType = cell.getCachedFormulaResultType(); output = readString(cell, formulaType); } else if (type == Cell.CELL_TYPE_BLANK) { output = new ExclyString(""); } else if (type == Cell.CELL_TYPE_BOOLEAN) { Boolean data = cell.getBooleanCellValue(); if (data) { output = new ExclyString("WAHR"); } else { output = new ExclyString("FALSCH"); } } else if (DateUtil.isCellDateFormatted(cell)) { Date data = cell.getDateCellValue(); output = new ExclyString(data.toString()); } else if (type == Cell.CELL_TYPE_NUMERIC) { double cellValue = cell.getNumericCellValue(); String data = String.valueOf(cellValue); if (cellValue % 1 == 0 && data.endsWith(".0")) { data = data.substring(0, data.length() - 2); } output = new ExclyString(data); } else { log.warn("The reader was unable to find a valid parser for the cell [Row, Column] (" + cell.getRowIndex() + ", " + cell.getColumnIndex() + ")"); output = new ExclyStringError(); } return output; }
From source file:at.mukprojects.exclycore.dao.XLSXReader.java
License:Open Source License
private ExclyDouble readDouble(Cell cell, int type) throws Exception { ExclyDouble output = null;/*w w w . java 2 s. c o m*/ if (type == Cell.CELL_TYPE_STRING) { String data = cell.getStringCellValue(); if (isNumericGerman(data)) { Number number = NumberFormat.getNumberInstance(Locale.GERMAN).parse(data); output = new ExclyDouble(number.doubleValue()); } else if (isNumericUK(data)) { Number number = NumberFormat.getNumberInstance(Locale.UK).parse(data); output = new ExclyDouble(number.doubleValue()); } else if (data.equals("") || data.equals(" ") || data.equals("-")) { output = new ExclyDoubleBlank(); } else { output = new ExclyDoubleError(); log.warn("The reader has expected a numeric value, but found a string value. [Row, Column] (" + cell.getRowIndex() + ", " + cell.getColumnIndex() + ")"); } } else if (type == Cell.CELL_TYPE_BLANK) { output = new ExclyDoubleBlank(); } else if (type == Cell.CELL_TYPE_FORMULA) { int formulaType = cell.getCachedFormulaResultType(); output = readDouble(cell, formulaType); } else if (type == Cell.CELL_TYPE_BOOLEAN) { Boolean data = cell.getBooleanCellValue(); if (data) { output = new ExclyDouble(1); } else { output = new ExclyDouble(0); } } else if (type == Cell.CELL_TYPE_NUMERIC) { double data = cell.getNumericCellValue(); output = new ExclyDouble(data); } else if (type == Cell.CELL_TYPE_ERROR) { output = new ExclyDoubleError(); } else { log.warn("The reader was unable to find a valid parser for the cell [Row, Column] (" + cell.getRowIndex() + ", " + cell.getColumnIndex() + ")"); output = new ExclyDoubleError(); } return output; }
From source file:at.mukprojects.exclycore.dao.XLSXReader.java
License:Open Source License
private ExclyInteger readInteger(Cell cell, int type) throws Exception { ExclyInteger output = null;//from w w w. j ava2 s .co m if (type == Cell.CELL_TYPE_STRING) { String data = cell.getStringCellValue(); if (isNumericGerman(data)) { Number number = NumberFormat.getNumberInstance(Locale.GERMAN).parse(data); output = new ExclyInteger(number.intValue()); } else if (isNumericUK(data)) { Number number = NumberFormat.getNumberInstance(Locale.UK).parse(data); output = new ExclyInteger(number.intValue()); } else if (data.equals("") || data.equals(" ") || data.trim().equals("-")) { output = new ExclyIntegerBlank(); } else { output = new ExclyIntegerError(); log.warn("The reader has expected a numeric value, but found a string value. [Row, Column] (" + cell.getRowIndex() + ", " + cell.getColumnIndex() + ")"); } } else if (type == Cell.CELL_TYPE_BLANK) { output = new ExclyIntegerBlank(); } else if (type == Cell.CELL_TYPE_FORMULA) { int formulaType = cell.getCachedFormulaResultType(); output = readInteger(cell, formulaType); } else if (type == Cell.CELL_TYPE_BOOLEAN) { Boolean data = cell.getBooleanCellValue(); if (data) { output = new ExclyInteger(1); } else { output = new ExclyInteger(0); } } else if (type == Cell.CELL_TYPE_NUMERIC) { double data = cell.getNumericCellValue(); output = new ExclyInteger(data); } else if (type == Cell.CELL_TYPE_ERROR) { output = new ExclyIntegerError(); } else { log.warn("The reader was unable to find a valid parser for the cell [Row, Column] (" + cell.getRowIndex() + ", " + cell.getColumnIndex() + ")"); output = new ExclyIntegerError(); } return output; }