List of usage examples for org.apache.poi.xssf.usermodel XSSFRichTextString getString
public String getString()
From source file:net.openchrom.xxd.processor.supplier.rscripting.ui.jobs.LoadXlsxExcelJob.java
License:Open Source License
private void loadExcel(final String file) { final File fil = new File(file); if (fil.exists()) { canRead = true;//from w ww . j a v a2 s. co m if (grid != null) { try { InputStream inp = new FileInputStream(file); try { wb = new XSSFWorkbook(inp); } catch (Exception e) { MsgDialog.message("Wrong format!\nOnly Excel *.xlsx (2007-2010) is supported!"); canRead = false; e.printStackTrace(); } // wb = new HSSFWorkbook(inp); } catch (IOException ex) { ex.printStackTrace(); } if (canRead) { for (s = 0; s < wb.getNumberOfSheets(); s++) { Display display = PlatformUI.getWorkbench().getDisplay(); display.syncExec(new Runnable() { public void run() { String name = fil.getName(); grid = new Spread().spread(SampleView.getTabFolder(), 0, 0, name); SampleView.setGrid(grid); XSSFSheet sheet = wb.getSheetAt(s); int colCount = grid.getColumnCount(); int rowCount = grid.getItemCount(); int exelRow = endOfRow(sheet); int exelColumn = endOfColumn(sheet); // System.out.println(exelRow + " " + exelColumn // + "---" + sheet.getPhysicalNumberOfRows() + // " " + // sheet.getRow(0).getPhysicalNumberOfCells()); if (colCount < exelColumn) { int diff = exelColumn - colCount; for (int i = 0; i < diff; i++) { GridColumn column = new GridColumn(grid, SWT.NONE); column.setText("C " + (i + 1 + colCount)); column.setWidth(50); } } if (rowCount < exelRow) { int diff = exelRow - rowCount; for (int i = 0; i < diff; i++) { new GridItem(grid, SWT.NONE).setHeight(16); } } // Iterate over each row in the sheet int rows = sheet.getPhysicalNumberOfRows(); for (int i = 0; i < exelRow; i++) { XSSFRow row = sheet.getRow(i); if (row == null) { for (int u = 0; u < exelColumn; u++) { grid.getItem(i).setText(u, " "); } } else { for (int u = 0; u < exelColumn; u++) { XSSFCell cell = row.getCell(u); if (cell != null) { switch (cell.getCellType()) { case XSSFCell.CELL_TYPE_NUMERIC: String val = String.valueOf(cell.getNumericCellValue()); grid.getItem(i).setText(u, val); break; case XSSFCell.CELL_TYPE_STRING: XSSFRichTextString st = cell.getRichStringCellValue(); String val2 = st.getString(); grid.getItem(i).setText(u, val2); break; case XSSFCell.CELL_TYPE_FORMULA: try { String val3 = String.valueOf(cell.getRawValue()); grid.getItem(i).setText(u, val3); } catch (Exception e) { // System.out.println(e.getMessage()); String s2 = cell.getCellFormula(); grid.getItem(i).setText(u, s2); } break; case XSSFCell.CELL_TYPE_BLANK: grid.getItem(i).setText(u, " "); break; case XSSFCell.CELL_TYPE_BOOLEAN: boolean s4 = cell.getBooleanCellValue(); if (s4) { grid.getItem(i).setText(u, "TRUE"); } else { grid.getItem(i).setText(u, "FALSE"); } break; default: break; } } else { grid.getItem(i).setText(u, " "); } } } } } }); } wb = null; } } } else { MsgDialog.message("File not found!"); } }
From source file:ru.icc.cells.ssdc.DataLoader.java
License:Apache License
private boolean hasSuperscriptText(Cell excelCell) { if (excelCell.getCellType() != Cell.CELL_TYPE_STRING) return false; RichTextString richTextString = excelCell.getRichStringCellValue(); if (null == richTextString) return false; int index = 0; int length = 0; XSSFFont font = null;// w w w .j a v a 2s . c o m XSSFRichTextString rts = (XSSFRichTextString) richTextString; XSSFWorkbook wb = (XSSFWorkbook) workbook; XSSFCellStyle style = ((XSSFCell) excelCell).getCellStyle(); font = style.getFont(); String richText = rts.getString(); if (rts.numFormattingRuns() > 1) { for (int i = 0; i < rts.numFormattingRuns(); i++) { index = rts.getIndexOfFormattingRun(i); length = rts.getLengthOfFormattingRun(i); try { font = rts.getFontOfFormattingRun(i); } catch (NullPointerException e) { font = wb.getFontAt(XSSFFont.DEFAULT_CHARSET); font.setTypeOffset(XSSFFont.SS_NONE); } String s = richText.substring(index, index + length); if (font.getTypeOffset() == XSSFFont.SS_SUPER) return true; } } else { if (font.getTypeOffset() == XSSFFont.SS_SUPER) return true; } return false; }
From source file:ru.icc.cells.ssdc.DataLoader.java
License:Apache License
private String getNotSuperscriptText(Cell excelCell) { if (excelCell.getCellType() != Cell.CELL_TYPE_STRING) return null; RichTextString richTextString = excelCell.getRichStringCellValue(); if (null == richTextString) return null; int index;//from ww w.ja va2 s .c o m int length; String text; XSSFRichTextString rts = (XSSFRichTextString) richTextString; XSSFWorkbook wb = (XSSFWorkbook) workbook; XSSFCellStyle style = ((XSSFCell) excelCell).getCellStyle(); XSSFFont font = style.getFont(); String richText = rts.getString(); StringBuilder notSuperscriptRuns = new StringBuilder(); if (rts.numFormattingRuns() > 1) { boolean wasNotSuperscriptRun = false; for (int i = 0; i < rts.numFormattingRuns(); i++) { index = rts.getIndexOfFormattingRun(i); length = rts.getLengthOfFormattingRun(i); try { font = rts.getFontOfFormattingRun(i); } catch (NullPointerException e) { font = wb.getFontAt(XSSFFont.DEFAULT_CHARSET); font.setTypeOffset(XSSFFont.SS_NONE); } String s = richText.substring(index, index + length); if (font.getTypeOffset() == XSSFFont.SS_SUPER) { if (wasNotSuperscriptRun) notSuperscriptRuns.append(" "); wasNotSuperscriptRun = false; } else { notSuperscriptRuns.append(s); wasNotSuperscriptRun = true; } } text = notSuperscriptRuns.toString(); } else { if (font.getTypeOffset() == XSSFFont.SS_SUPER) text = null; else text = richText; } return text; }