Yesterday, whilst watching Italy beat France in a wonderful game of Rugby, I
became convinced there had to be way to modify the code slightly and get the
font from the formatting run rather than based upon the position of the text
within the rich text string as the previous example did. Do not know if this
makes matters any cleaner or clearer but it has satisfied an itch and the
key was to get the font information for the first part of the rich text
sting from the style object applied to the cell. Anway, hoping we win this
afternoon's match, will be inconsolable otherwise. Hope this helps and
thanks for the heads up re the 'missing' first formatting run. Would not
have made real headway without that observation.
public static final void xlsSuperscriptTest2(String filename) throws
IOException {
File file = null;
FileInputStream fis = null;
HSSFWorkbook workbook = null;
HSSFSheet sheet = null;
HSSFRow row = null;
HSSFCell cell = null;
HSSFRichTextString rts = null;
HSSFCellStyle style = null;
HSSFFont font = null;
Iterator cellIter = null;
Iterator rowIter = null;
int fromIndex = 0;
int toIndex = 0;
int formattingRunIndex = 0;
try {
file = new File(filename);
fis = new FileInputStream(file);
workbook = new HSSFWorkbook(fis);
fis.close();
fis = null;
sheet = workbook.getSheetAt(0);
rowIter = sheet.rowIterator();
while (rowIter.hasNext()) {
row = (HSSFRow) rowIter.next();
cellIter = row.cellIterator();
while (cellIter.hasNext()) {
fromIndex = 0;
toIndex = 0;
cell = (HSSFCell) cellIter.next();
if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
rts = cell.getRichStringCellValue();
System.out.println("The cell contains [" +
rts.toString() +
"]");
style = cell.getCellStyle();
font = style.getFont(workbook);
if (rts.numFormattingRuns() > 0) {
for (formattingRunIndex = 0; formattingRunIndex
< rts.numFormattingRuns(); formattingRunIndex++) {
toIndex =
rts.getIndexOfFormattingRun(formattingRunIndex);
System.out.println("\tSubstring [" +
rts.toString().substring(fromIndex,
toIndex) +
"]");
if(font.getTypeOffset() ==
HSSFFont.SS_SUPER) {
System.out.println("\t\tSuperscripted");
}
else {
System.out.println("\t\tNOT
Superscripted");
}
font =
workbook.getFontAt(rts.getFontOfFormattingRun(formattingRunIndex));
fromIndex = toIndex;
}
toIndex = rts.length();
System.out.println("\tSubstring [" +
rts.toString().substring(fromIndex,
toIndex) +
"]");
if(font.getTypeOffset() == HSSFFont.SS_SUPER) {
System.out.println("\t\tSuperscripted");
}
else {
System.out.println("\t\tNOT
Superscripted");
}
}
else {
System.out.print("The String [" +
rts.toString());
if (font.getTypeOffset() == HSSFFont.SS_SUPER) {
System.out.print("] is ");
}
else {
System.out.print("] is not ");
}
System.out.println("superscripted.");
}
}
else {
System.out.println("The cell at row number " +
cell.getRowIndex() +
" and column number " +
cell.getColumnIndex() +
" does not contain a String.");
}
}
}
}
finally {
if (fis != null) {
fis.close();
fis = null;
}
}
}
--
View this message in context:
http://apache-poi.1045710.n5.nabble.com/Reading-superscript-from-data-cell-tp3414964p3555400.html
Sent from the POI - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]