That's interesting because I found the approach to work. I simply created an
Excel workbook with a single cell populated and write into that cell some
rubbish like this - '10 * 22 and this is sime text' - explicitly formatted
the second number 2 as superscript and then saved it away. Running this code
against that workbook;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package workbookprotection;
import java.io.*;
import org.apache.poi.xssf.usermodel.*;
/**
*
* @author win user
*/
public class SuperscriptTest {
public SuperscriptTest(String filename) throws FileNotFoundException,
IOException {
File file = null;
FileInputStream fis = null;
XSSFWorkbook workbook = null;
XSSFSheet sheet = null;
XSSFRow row = null;
XSSFCell cell = null;
XSSFRichTextString rts = null;
XSSFFont font = null;
int runLength = 0;
int runIndex = 0;
try {
file = new File(filename);
fis = new FileInputStream(file);
workbook = new XSSFWorkbook(fis);
fis.close();
fis = null;
sheet = workbook.getSheetAt(0);
row = sheet.getRow(0);
cell = row.getCell(0);
rts = cell.getRichStringCellValue();
if(rts.numFormattingRuns() > 1) {
for(int i = 0; i < rts.numFormattingRuns(); i++) {
// Firstly pull out that piece of text the font relates
to
runLength = rts.getLengthOfFormattingRun(i);
runIndex = rts.getIndexOfFormattingRun(i);
System.out.println(rts.toString().substring(runIndex,
(runIndex + runLength)));
// Now, get at the font information using the index
// number of the formatting run. Note that it appears as
// though if the default font is applied to the run
// then a null object will be returned for the font. Can
// handle this in the slightly ugly manner shown below.
try {
font = rts.getFontOfFormattingRun(i);
}
catch(NullPointerException npe) {
// Create an XSSFFont object to 'stand in' for the
missing
// default font object. Note that the superscript
property
// has to be set off manually. If this is not done
then
// the font is regarded as being a superscripted
one.
font = workbook.getFontAt(XSSFFont.DEFAULT_CHARSET);
font.setTypeOffset(XSSFFont.SS_NONE);
}
if(font.getTypeOffset() == XSSFFont.SS_SUPER) {
System.out.println("\tSuperscripted");
}
else {
System.out.println("\tNOT Superscripted");
}
}
}
}
finally {
if(fis != null) {
fis.close();
fis = null;
}
}
}
}
did interpret the contents of the cell correctly, detecting that the second
number 2 had been superscripted. There are a few wrinkles that I am unhappy
about at the moment - not the least being that there was no font object
returned for the first formatting run in the cell and I consequently assumed
that to be the default font for the workbook but this is an assumption.
How has/have the superscripted character(s) been created in the cells of the
workbook you are processing? I do know that Excel can 'default' certain
features into cells whilst the user is typing and wonder if that might be
the case here. Just out of interest, could you create a test workbook that
resembles mine and uses the same approach - populate just cell A1 of sheet
one and subscript one or more parts of the cells contents using the font
menu. Run this through 'my' code and see what happens?
Yours
Mark B
--
View this message in context:
http://apache-poi.1045710.n5.nabble.com/Reading-superscript-from-data-cell-tp3414964p3423121.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]