Reputation: 2917
Is it possible to read the format of a cell from an excell sheet and determinde which words are bold or italic? I can read and write to cells, and I also know that JExcel can write formatted cells. In formatted cells I mean that the text is italic, or bold.
Is it possible the read a cell data and determine which words are bold? For instance I will have this in cell:
"A sample text from one excel cell"
I want to know that the string "excel cell" is bold, and the string "sample" is Italic. Is this possible in JExcel, if not how would I do that in Java? Can somebody suggest an API? Maybe a better approach would be to pares an xml file.
Upvotes: 2
Views: 3664
Reputation: 3710
I don't know about JExcel, but I can tell you this is fairly easy to do in Apache POI. Here is a simple application to show one way it can be done. It isn't incredibly pretty, but it should be enough to get you started:
public static final void main(String... args) throws Exception
{
InputStream is = ExcelFormatTest.class.getResourceAsStream("Test.xlsx");
Workbook wb = new XSSFWorkbook(is);
Sheet sheet = wb.getSheetAt(0);
Cell cell = sheet.getRow(0).getCell(0);
XSSFRichTextString richText = (XSSFRichTextString)cell.getRichStringCellValue();
int formattingRuns = cell.getRichStringCellValue().numFormattingRuns();
for(int i = 0; i < formattingRuns; i++)
{
int startIdx = richText.getIndexOfFormattingRun(i);
int length = richText.getLengthOfFormattingRun(i);
System.out.println("Text: " + richText.getString().substring(startIdx, startIdx + length));
if(i == 0)
{
short fontIndex = cell.getCellStyle().getFontIndex();
Font f = wb.getFontAt(fontIndex);
System.out.println("Bold: " + (f.getBoldweight() == Font.BOLDWEIGHT_BOLD));
System.out.println("Italics: " + f.getItalic() + "\n");
}
else
{
Font f = richText.getFontOfFormattingRun(i);
System.out.println("Bold: " + (f.getBoldweight() == Font.BOLDWEIGHT_BOLD));
System.out.println("Italics: " + f.getItalic() + "\n");
}
}
}
Basically, you get a RichTextString
object from a cell (make sure it is a String cell first, though), then iterate over the formatting runs and check the font for each one. It looks like the first run uses the Cell's CellStyle/font, so you have to look it up that way (you get an NPE if you try to get it from the RichTextString).
Once you have the font, you can get all of its attributes. Here is the Javadoc for POI's Font.
If you are using older, non-XLSX files, replace XSSF with HSSF in the class names, and you'll have to change the RichTextString code a bit to lookup the font using the font index. Here are the JavaDocs for XSSFRichTextString and HSSFRichTextString.
Running this with the following in Sheet 1, A1: "A sample text from one excel cell" gives the following results:
Text: A
Bold: false
Italics: false
Text: sample
Bold: true
Italics: false
Text: text
Bold: false
Italics: false
Text: from
Bold: false
Italics: true
Text: one
Bold: false
Italics: false
Text: excel cell
Bold: true
Italics: true
Upvotes: 14
Reputation: 27478
Here's how I'd do it in VBA. Maybe you can translate:
Sub ListBoldStrings()
Dim cell As Excel.Range
Dim i As Long
Dim BoldChars As String
Dim BoldStrings() As String
'replace "|" with a char that will not appear in evaluated strings
Const SEPARATOR_CHAR As String = "|"
Set cell = ActiveCell
With cell
For i = 1 To .Characters.Count
If .Characters(i, 1).Font.Bold Then
BoldChars = BoldChars + .Characters(i, 1).Text
Else
BoldChars = BoldChars + SEPARATOR_CHAR
End If
If Right$(BoldChars, 2) = WorksheetFunction.Rept(SEPARATOR_CHAR, 2) Then
BoldChars = Left$(BoldChars, Len(BoldChars) - 1)
End If
Next i
End With
BoldStrings = Split(BoldChars, SEPARATOR_CHAR)
For i = LBound(BoldStrings) To UBound(BoldStrings)
Debug.Print BoldStrings(i)
Next i
End Sub
Upvotes: 3