I think that I may have the problem cracked now and it proved to be a
combination of a few different things. Anyway, take a look at the code, give
it a through test and let me know if it works for you. It performed
successfully against a file containing a few tricky tests - including one
where I superscripted just part of a word.
public static final void xlsSuperscriptTest(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();
// The following will work only for cells that contain
Strings
// so test for that here.
if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
// Get the RichTextString from the cell.
rts = cell.getRichStringCellValue();
// If there is one or more formatting runs in the
// rich text string then process them
if (rts.numFormattingRuns() > 0) {
// Iterate through the formatting runs
for (formattingRunIndex = 0; formattingRunIndex
< rts.numFormattingRuns(); formattingRunIndex++) {
System.out.println("Cell contents: " +
rts.toString());
// Get that point in the cells text where
the
// next formatting run will begin. Use this
to
// display the substring the format will
apply to.
// There is no option to recover the length
of a
// formatting run using HSSF and so it is
// necessary to always look ahead.
toIndex =
rts.getIndexOfFormattingRun(formattingRunIndex);
System.out.println("Substring [" +
rts.toString().substring(fromIndex,
toIndex) +
"]");
// It was proving problematic to obtain the
font
// object using the index number of the
formatting
// so, this time, the font information is
obtained
// using the position of the starting point
of
// the substring within the cells contents.
font =
workbook.getFontAt(rts.getFontAtIndex(fromIndex));
if (font.getTypeOffset() ==
HSSFFont.SS_SUPER) {
System.out.println("\tSuperscripted");
} else {
System.out.println("\tNOT
Superscripted");
}
// Make sure that the starting point for the
next
// substring will be the end point of the
last
fromIndex = toIndex;
}
// Handle the very final part of the cells
contents.
toIndex = rts.length();
System.out.println("Substring [" +
rts.toString().substring(fromIndex,
toIndex) +
"]");
font =
workbook.getFontAt(rts.getFontAtIndex(fromIndex));
if (font.getTypeOffset() == HSSFFont.SS_SUPER) {
System.out.println("\tSuperscripted");
} else {
System.out.println("\tNOT Superscripted");
}
} else {
// If there are no formatting runs in the
RichTextString
// then that string has only a single format
applied
// to it. In this case, all information can be
// obtained from the font information contained
within the
// cell style object.
System.out.print("The String [" +
rts.toString());
style = cell.getCellStyle();
font = style.getFont(workbook);
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;
}
}
}
If anmything is not clear, just drop a message on to the list.
Yours
Mark B
PS am going to see if I can convert this to work for both the XSSF and SS
streams. If so, it may be worth creating an entry in the quick guide as I am
sure you cannot be alone in needing to know how to strip this sort of
information out of a cell.
--
View this message in context:
http://apache-poi.1045710.n5.nabble.com/Reading-superscript-from-data-cell-tp3414964p3478171.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]