Setting Some part of cell content to bold/italic using apache-poi 4.0.1 libraryDiscover titles/paragraphs in word docsApache POI: Partial Cell fontsSetting a part of the cell contents to bold using apache poi?HTML Formatted Cell value from Excel using Apache POISetting a part of the cell contents to Underline using Apache POI?How to underline text and how to hide table border in Apache POIApache POI setCellType not correctly setting cell type for numeric valueApache POI: How to set font style for a field (PAGE, PAGENUM, PAGEREF…)How to set bold font using the latest apache poi?Apache POI Wrong Cells Font Being Set

Piece of fabric in planter, how to use it?

My current job follows "worst practices". How can I talk about my experience in an interview without giving off red flags?

Importance of moon phases for Apollo missions

Why are Oscar, India, and X-Ray (O, I, and X) not used as taxiway identifiers?

What kind of vegetable has pink and white concentric rings?

Can "Taking algebraic closure" be made into a functor?

How do you structure large embedded projects?

Why is the UH-60 tail rotor canted?

Did Don Young threaten John Boehner with a 10 inch blade to the throat?

Acoustic guitar chords' positions vs those of a Bass guitar

Plotting maxima within a simplex

What does the following chess proverb mean: "Chess is a sea where a gnat may drink from and an elephant may bathe in."

How to pass array of values in lualatex?

Why was Quirrell said to be in the Black Forest if Voldemort was actually in Albania?

"It is what it is"

What are "the high ends of castles" called?

How should I handle a question regarding my regrets during an interview?

Count the identical pairs in two lists

What do Unicorns want?

As the Ferris wheel turns

Can a creature sustain itself by eating its own severed body parts?

Would using carbon dioxide as fuel work to reduce the greenhouse effect?

Strange LED behavior

Can I use Sitecore's Configuration patching mechanics for my Identity Server configuration?



Setting Some part of cell content to bold/italic using apache-poi 4.0.1 library


Discover titles/paragraphs in word docsApache POI: Partial Cell fontsSetting a part of the cell contents to bold using apache poi?HTML Formatted Cell value from Excel using Apache POISetting a part of the cell contents to Underline using Apache POI?How to underline text and how to hide table border in Apache POIApache POI setCellType not correctly setting cell type for numeric valueApache POI: How to set font style for a field (PAGE, PAGENUM, PAGEREF…)How to set bold font using the latest apache poi?Apache POI Wrong Cells Font Being Set






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I want to set the content of cell value with a combination of bold and italic.
like "This is Sample content."



However, this is not working using XSSFRichTextString.



I am using apache poi library version 4.0.1. I have tried making my content bold and italic combination using XSSFRichTextString. I have appended the string using by passing two arguments in method cell1Value.append("sample ", fontBold);i.e. String and Font.



 XSSFRichTextString cell1Value= new XSSFRichTextString("This is ");
XSSFFont fontBold= wb.createFont();
fontBold.setBold(true); //set bold
fontBold.setUnderline(HSSFFont.U_SINGLE);


XSSFFont fontItalic= wb.createFont();
fontItalic.setItalic(true); //set italic

cell1Value.append("sample ",fontBold);
cell1Value.append("content", fontItalic);

System.err.println(cell1Value.getCTRst());

Cell cell1 = row.createCell(0);
cell1.setCellValue(cell1Value);


I am expecting "sample" to be bold and "content" to be italic. However, the underline is working fine and my "sample" word is underlined properly. Please suggest what I am missing?










share|improve this question

















  • 2





    Some (rather silly) checks: Did you enlarge the view (zoom in), as maybe in the shown font size boldness might not be shown. Also the font might not be available in bold.

    – Joop Eggen
    Mar 26 at 13:33











  • @JoopEggen Yes, I checked the sheet by zooming it as well. when I apply the font style(only bold) using cell style method to a particular cell then It shows the content bold as well. cellStyle.setFont(setFontStyle(wb)); private XSSFFont setFontStyle(XSSFWorkbook wb) XSSFFont font = wb.createFont(); font.setBold(true); return font; Cell cell1 = row.createCell(0); cell1.setCellValue("Some value"); cell1.setCellStyle(cellStyle);; But I need combination of both i.e bold and italic content in the cell.

    – Bharti Gulati
    Mar 26 at 13:38


















0















I want to set the content of cell value with a combination of bold and italic.
like "This is Sample content."



However, this is not working using XSSFRichTextString.



I am using apache poi library version 4.0.1. I have tried making my content bold and italic combination using XSSFRichTextString. I have appended the string using by passing two arguments in method cell1Value.append("sample ", fontBold);i.e. String and Font.



 XSSFRichTextString cell1Value= new XSSFRichTextString("This is ");
XSSFFont fontBold= wb.createFont();
fontBold.setBold(true); //set bold
fontBold.setUnderline(HSSFFont.U_SINGLE);


XSSFFont fontItalic= wb.createFont();
fontItalic.setItalic(true); //set italic

cell1Value.append("sample ",fontBold);
cell1Value.append("content", fontItalic);

System.err.println(cell1Value.getCTRst());

Cell cell1 = row.createCell(0);
cell1.setCellValue(cell1Value);


I am expecting "sample" to be bold and "content" to be italic. However, the underline is working fine and my "sample" word is underlined properly. Please suggest what I am missing?










share|improve this question

















  • 2





    Some (rather silly) checks: Did you enlarge the view (zoom in), as maybe in the shown font size boldness might not be shown. Also the font might not be available in bold.

    – Joop Eggen
    Mar 26 at 13:33











  • @JoopEggen Yes, I checked the sheet by zooming it as well. when I apply the font style(only bold) using cell style method to a particular cell then It shows the content bold as well. cellStyle.setFont(setFontStyle(wb)); private XSSFFont setFontStyle(XSSFWorkbook wb) XSSFFont font = wb.createFont(); font.setBold(true); return font; Cell cell1 = row.createCell(0); cell1.setCellValue("Some value"); cell1.setCellStyle(cellStyle);; But I need combination of both i.e bold and italic content in the cell.

    – Bharti Gulati
    Mar 26 at 13:38














0












0








0








I want to set the content of cell value with a combination of bold and italic.
like "This is Sample content."



However, this is not working using XSSFRichTextString.



I am using apache poi library version 4.0.1. I have tried making my content bold and italic combination using XSSFRichTextString. I have appended the string using by passing two arguments in method cell1Value.append("sample ", fontBold);i.e. String and Font.



 XSSFRichTextString cell1Value= new XSSFRichTextString("This is ");
XSSFFont fontBold= wb.createFont();
fontBold.setBold(true); //set bold
fontBold.setUnderline(HSSFFont.U_SINGLE);


XSSFFont fontItalic= wb.createFont();
fontItalic.setItalic(true); //set italic

cell1Value.append("sample ",fontBold);
cell1Value.append("content", fontItalic);

System.err.println(cell1Value.getCTRst());

Cell cell1 = row.createCell(0);
cell1.setCellValue(cell1Value);


I am expecting "sample" to be bold and "content" to be italic. However, the underline is working fine and my "sample" word is underlined properly. Please suggest what I am missing?










share|improve this question














I want to set the content of cell value with a combination of bold and italic.
like "This is Sample content."



However, this is not working using XSSFRichTextString.



I am using apache poi library version 4.0.1. I have tried making my content bold and italic combination using XSSFRichTextString. I have appended the string using by passing two arguments in method cell1Value.append("sample ", fontBold);i.e. String and Font.



 XSSFRichTextString cell1Value= new XSSFRichTextString("This is ");
XSSFFont fontBold= wb.createFont();
fontBold.setBold(true); //set bold
fontBold.setUnderline(HSSFFont.U_SINGLE);


XSSFFont fontItalic= wb.createFont();
fontItalic.setItalic(true); //set italic

cell1Value.append("sample ",fontBold);
cell1Value.append("content", fontItalic);

System.err.println(cell1Value.getCTRst());

Cell cell1 = row.createCell(0);
cell1.setCellValue(cell1Value);


I am expecting "sample" to be bold and "content" to be italic. However, the underline is working fine and my "sample" word is underlined properly. Please suggest what I am missing?







java excel apache apache-poi






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 26 at 13:24









Bharti GulatiBharti Gulati

85 bronze badges




85 bronze badges







  • 2





    Some (rather silly) checks: Did you enlarge the view (zoom in), as maybe in the shown font size boldness might not be shown. Also the font might not be available in bold.

    – Joop Eggen
    Mar 26 at 13:33











  • @JoopEggen Yes, I checked the sheet by zooming it as well. when I apply the font style(only bold) using cell style method to a particular cell then It shows the content bold as well. cellStyle.setFont(setFontStyle(wb)); private XSSFFont setFontStyle(XSSFWorkbook wb) XSSFFont font = wb.createFont(); font.setBold(true); return font; Cell cell1 = row.createCell(0); cell1.setCellValue("Some value"); cell1.setCellStyle(cellStyle);; But I need combination of both i.e bold and italic content in the cell.

    – Bharti Gulati
    Mar 26 at 13:38













  • 2





    Some (rather silly) checks: Did you enlarge the view (zoom in), as maybe in the shown font size boldness might not be shown. Also the font might not be available in bold.

    – Joop Eggen
    Mar 26 at 13:33











  • @JoopEggen Yes, I checked the sheet by zooming it as well. when I apply the font style(only bold) using cell style method to a particular cell then It shows the content bold as well. cellStyle.setFont(setFontStyle(wb)); private XSSFFont setFontStyle(XSSFWorkbook wb) XSSFFont font = wb.createFont(); font.setBold(true); return font; Cell cell1 = row.createCell(0); cell1.setCellValue("Some value"); cell1.setCellStyle(cellStyle);; But I need combination of both i.e bold and italic content in the cell.

    – Bharti Gulati
    Mar 26 at 13:38








2




2





Some (rather silly) checks: Did you enlarge the view (zoom in), as maybe in the shown font size boldness might not be shown. Also the font might not be available in bold.

– Joop Eggen
Mar 26 at 13:33





Some (rather silly) checks: Did you enlarge the view (zoom in), as maybe in the shown font size boldness might not be shown. Also the font might not be available in bold.

– Joop Eggen
Mar 26 at 13:33













@JoopEggen Yes, I checked the sheet by zooming it as well. when I apply the font style(only bold) using cell style method to a particular cell then It shows the content bold as well. cellStyle.setFont(setFontStyle(wb)); private XSSFFont setFontStyle(XSSFWorkbook wb) XSSFFont font = wb.createFont(); font.setBold(true); return font; Cell cell1 = row.createCell(0); cell1.setCellValue("Some value"); cell1.setCellStyle(cellStyle);; But I need combination of both i.e bold and italic content in the cell.

– Bharti Gulati
Mar 26 at 13:38






@JoopEggen Yes, I checked the sheet by zooming it as well. when I apply the font style(only bold) using cell style method to a particular cell then It shows the content bold as well. cellStyle.setFont(setFontStyle(wb)); private XSSFFont setFontStyle(XSSFWorkbook wb) XSSFFont font = wb.createFont(); font.setBold(true); return font; Cell cell1 = row.createCell(0); cell1.setCellValue("Some value"); cell1.setCellStyle(cellStyle);; But I need combination of both i.e bold and italic content in the cell.

– Bharti Gulati
Mar 26 at 13:38













3 Answers
3






active

oldest

votes


















2














import java.io.FileOutputStream;
import java.io.OutputStream;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class TextBoldItalic

public static void main(String[] args) throws Exception

XSSFWorkbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet();
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);

XSSFFont fontBold = wb.createFont();
fontBold.setBold(true);
XSSFFont fontItalic = wb.createFont();
fontItalic.setItalic(true);
XSSFFont fontBoldItalic = wb.createFont();
fontBoldItalic.setBold(true);
fontBoldItalic.setItalic(true);

XSSFRichTextString cellValue = new XSSFRichTextString();
cellValue.append("This is ", fontBold);
cellValue.append("sample ", fontItalic);
cellValue.append("content", fontBoldItalic);
cell.setCellValue(cellValue);

OutputStream fileOut = new FileOutputStream("TextBoldItalic.xlsx");
wb.write(fileOut);
wb.close();




This code works for me and gives me this result in LibreOffice. OpenOffice is fine, too. No MS Excel here for a test, sorry. Of course tools like this Online-Excel-Viewer will not do it right. So, please try my code and make a report.






share|improve this answer























  • Thanks, It works with Libra office, but not with WPS spreadsheet. There might be some issue with WPS spreadsheet to pick both styles. Again thanks for your help. :)

    – Bharti Gulati
    Mar 28 at 6:34


















0














As the code looks plausible, just doing the complete run:



The following I have tested with



  • org.apache.poi / poi / 3.16

  • org.apache.poi / poi-ooxml / 3.16

It worked.



try (XSSFWorkbook wb = new XSSFWorkbook()) 
XSSFSheet sheet = wb.createSheet("With Rich Text");
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);

XSSFFont fontPlain = wb.createFont();

XSSFFont fontBoldItalic = wb.createFont();
fontBoldItalic.setBoldItalic(true);
fontBoldItalic.setItalic(true);

XSSFFont fontItalic = wb.createFont();
fontItalic.setItalic(true);

XSSFRichTextString cell1Value= new XSSFRichTextString("This is ");
cell1Value.applyFont(fontPlain);
cell1Value.append("sample ", fontBoldItalic);
cell1Value.append("content", fontItalic);

cell.setCellValue(cell1Value);
wb.write(new FileOutputStream(xlsxFile));
catch (IOException e)
e.printStackTrace();



My guess is a variable mix-up or something trivial. Maybe the font.






share|improve this answer

























  • What apache poiversion provides method XSSFFont.setBoldItalic?

    – Axel Richter
    Mar 27 at 4:58











  • @Joop Egeen, It didn't work. The cell content is not getting bold and italic. :(

    – Bharti Gulati
    Mar 27 at 5:37











  • I have tested and now added the entire code. It probably has nothing to do with the library. Maybe special font handling in your code?

    – Joop Eggen
    Mar 27 at 9:37






  • 1





    @JoopEggen The problem was with WPS spreadsheet. I have checked the same code is working with Libra office now, but now with WPS spreadsheet. Thanks for your kind help. :)

    – Bharti Gulati
    Mar 28 at 6:55


















0














The problem using WPS Spreadsheets is that they claim to be most compatible to Excel but sometimes they totally fail. This time they misinterpret all Boolean font settings (bold, italic, strike) if they explicitly are set true.



Office Open XML provides Boolean elements having val attribute. Example: <b val="true"/> or <b val="false"/> or also <b val="1"/> or <b val="0"/>. But for set bold font having <b/> would be enough. And for not set bold font simply not having the b element at all would be enough.



Apache poi always sets <b val="true"/> for bold and <b val="false"/> for not bold. But WPS Spreadsheets now seems to misinterpret <b val="true"/>. It expects <b/> only.



The following code is the most compatible code for creating rich text strings for Excel. It supports Office Open XML (*.xlsx) as well as BIFF (*.xls) and it corrects the <Boolean val="true"/> to <Boolean/> only.



import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import org.apache.xmlbeans.XmlObject;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBooleanProperty;

class CreateExcelRichText

static RichTextString createRichTextString(Workbook workbook, String[] textParts, Font[] fonts)
CreationHelper creationHelper = workbook.getCreationHelper();
RichTextString richTextString = creationHelper.createRichTextString(String.join("", textParts));
int start = 0;
int end = 0;
for (int tp = 0; tp < textParts.length; tp ++)
Font font = null;
if (tp < fonts.length) font = fonts[tp];
end += textParts[tp].length();
if (font != null) richTextString.applyFont(start, end, font);
start += textParts[tp].length();

if (richTextString instanceof XSSFRichTextString)
//unset val="true" for boolean objects
XSSFRichTextString xSSFRichTextString = (XSSFRichTextString)richTextString;
String[] boolenanObjectsToUnset = new String[]"b", "i", "strike";
for (String boolenanObjectToUnset : boolenanObjectsToUnset)
XmlObject[] xmlObjects = xSSFRichTextString.getCTRst().selectPath(
"declare namespace main='http://schemas.openxmlformats.org/spreadsheetml/2006/main' " +
".//main:" + boolenanObjectToUnset);
for (XmlObject xmlObject : xmlObjects)
CTBooleanProperty booleanProperty = (CTBooleanProperty)xmlObject;
if (booleanProperty.getVal()) booleanProperty.unsetVal();



return richTextString;


public static void main(String[] args) throws Exception

Workbook workbook = new XSSFWorkbook();
//Workbook workbook = new HSSFWorkbook();

Font font = workbook.createFont();

Font fontBoldItalic = workbook.createFont();
fontBoldItalic.setBold(true);
fontBoldItalic.setItalic(true);

Font fontItalic = workbook.createFont();
fontItalic.setItalic(true);

Font fontStrikeout = workbook.createFont();
fontStrikeout.setStrikeout(true);

String[] textParts = new String[]"This is ", "Sample ", "content. ", "This is crossed out.";
Font[] fonts = new Font[]font, fontBoldItalic, fontItalic, fontStrikeout;

RichTextString richTextString = createRichTextString(workbook, textParts, fonts);

Sheet sheet = workbook.createSheet();
sheet.createRow(0).createCell(0).setCellValue(richTextString);

String fileName = (workbook instanceof XSSFWorkbook)?"Excel.xlsx":"Excel.xls";
FileOutputStream out = new FileOutputStream(fileName);
workbook.write(out);
out.close();
workbook.close();







share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55358293%2fsetting-some-part-of-cell-content-to-bold-italic-using-apache-poi-4-0-1-library%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    import java.io.FileOutputStream;
    import java.io.OutputStream;

    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.ss.usermodel.Sheet;
    import org.apache.poi.xssf.usermodel.XSSFFont;
    import org.apache.poi.xssf.usermodel.XSSFRichTextString;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;

    public class TextBoldItalic

    public static void main(String[] args) throws Exception

    XSSFWorkbook wb = new XSSFWorkbook();
    Sheet sheet = wb.createSheet();
    Row row = sheet.createRow(0);
    Cell cell = row.createCell(0);

    XSSFFont fontBold = wb.createFont();
    fontBold.setBold(true);
    XSSFFont fontItalic = wb.createFont();
    fontItalic.setItalic(true);
    XSSFFont fontBoldItalic = wb.createFont();
    fontBoldItalic.setBold(true);
    fontBoldItalic.setItalic(true);

    XSSFRichTextString cellValue = new XSSFRichTextString();
    cellValue.append("This is ", fontBold);
    cellValue.append("sample ", fontItalic);
    cellValue.append("content", fontBoldItalic);
    cell.setCellValue(cellValue);

    OutputStream fileOut = new FileOutputStream("TextBoldItalic.xlsx");
    wb.write(fileOut);
    wb.close();




    This code works for me and gives me this result in LibreOffice. OpenOffice is fine, too. No MS Excel here for a test, sorry. Of course tools like this Online-Excel-Viewer will not do it right. So, please try my code and make a report.






    share|improve this answer























    • Thanks, It works with Libra office, but not with WPS spreadsheet. There might be some issue with WPS spreadsheet to pick both styles. Again thanks for your help. :)

      – Bharti Gulati
      Mar 28 at 6:34















    2














    import java.io.FileOutputStream;
    import java.io.OutputStream;

    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.ss.usermodel.Sheet;
    import org.apache.poi.xssf.usermodel.XSSFFont;
    import org.apache.poi.xssf.usermodel.XSSFRichTextString;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;

    public class TextBoldItalic

    public static void main(String[] args) throws Exception

    XSSFWorkbook wb = new XSSFWorkbook();
    Sheet sheet = wb.createSheet();
    Row row = sheet.createRow(0);
    Cell cell = row.createCell(0);

    XSSFFont fontBold = wb.createFont();
    fontBold.setBold(true);
    XSSFFont fontItalic = wb.createFont();
    fontItalic.setItalic(true);
    XSSFFont fontBoldItalic = wb.createFont();
    fontBoldItalic.setBold(true);
    fontBoldItalic.setItalic(true);

    XSSFRichTextString cellValue = new XSSFRichTextString();
    cellValue.append("This is ", fontBold);
    cellValue.append("sample ", fontItalic);
    cellValue.append("content", fontBoldItalic);
    cell.setCellValue(cellValue);

    OutputStream fileOut = new FileOutputStream("TextBoldItalic.xlsx");
    wb.write(fileOut);
    wb.close();




    This code works for me and gives me this result in LibreOffice. OpenOffice is fine, too. No MS Excel here for a test, sorry. Of course tools like this Online-Excel-Viewer will not do it right. So, please try my code and make a report.






    share|improve this answer























    • Thanks, It works with Libra office, but not with WPS spreadsheet. There might be some issue with WPS spreadsheet to pick both styles. Again thanks for your help. :)

      – Bharti Gulati
      Mar 28 at 6:34













    2












    2








    2







    import java.io.FileOutputStream;
    import java.io.OutputStream;

    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.ss.usermodel.Sheet;
    import org.apache.poi.xssf.usermodel.XSSFFont;
    import org.apache.poi.xssf.usermodel.XSSFRichTextString;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;

    public class TextBoldItalic

    public static void main(String[] args) throws Exception

    XSSFWorkbook wb = new XSSFWorkbook();
    Sheet sheet = wb.createSheet();
    Row row = sheet.createRow(0);
    Cell cell = row.createCell(0);

    XSSFFont fontBold = wb.createFont();
    fontBold.setBold(true);
    XSSFFont fontItalic = wb.createFont();
    fontItalic.setItalic(true);
    XSSFFont fontBoldItalic = wb.createFont();
    fontBoldItalic.setBold(true);
    fontBoldItalic.setItalic(true);

    XSSFRichTextString cellValue = new XSSFRichTextString();
    cellValue.append("This is ", fontBold);
    cellValue.append("sample ", fontItalic);
    cellValue.append("content", fontBoldItalic);
    cell.setCellValue(cellValue);

    OutputStream fileOut = new FileOutputStream("TextBoldItalic.xlsx");
    wb.write(fileOut);
    wb.close();




    This code works for me and gives me this result in LibreOffice. OpenOffice is fine, too. No MS Excel here for a test, sorry. Of course tools like this Online-Excel-Viewer will not do it right. So, please try my code and make a report.






    share|improve this answer













    import java.io.FileOutputStream;
    import java.io.OutputStream;

    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.ss.usermodel.Sheet;
    import org.apache.poi.xssf.usermodel.XSSFFont;
    import org.apache.poi.xssf.usermodel.XSSFRichTextString;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;

    public class TextBoldItalic

    public static void main(String[] args) throws Exception

    XSSFWorkbook wb = new XSSFWorkbook();
    Sheet sheet = wb.createSheet();
    Row row = sheet.createRow(0);
    Cell cell = row.createCell(0);

    XSSFFont fontBold = wb.createFont();
    fontBold.setBold(true);
    XSSFFont fontItalic = wb.createFont();
    fontItalic.setItalic(true);
    XSSFFont fontBoldItalic = wb.createFont();
    fontBoldItalic.setBold(true);
    fontBoldItalic.setItalic(true);

    XSSFRichTextString cellValue = new XSSFRichTextString();
    cellValue.append("This is ", fontBold);
    cellValue.append("sample ", fontItalic);
    cellValue.append("content", fontBoldItalic);
    cell.setCellValue(cellValue);

    OutputStream fileOut = new FileOutputStream("TextBoldItalic.xlsx");
    wb.write(fileOut);
    wb.close();




    This code works for me and gives me this result in LibreOffice. OpenOffice is fine, too. No MS Excel here for a test, sorry. Of course tools like this Online-Excel-Viewer will not do it right. So, please try my code and make a report.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Mar 27 at 17:40









    MartinMartin

    1039 bronze badges




    1039 bronze badges












    • Thanks, It works with Libra office, but not with WPS spreadsheet. There might be some issue with WPS spreadsheet to pick both styles. Again thanks for your help. :)

      – Bharti Gulati
      Mar 28 at 6:34

















    • Thanks, It works with Libra office, but not with WPS spreadsheet. There might be some issue with WPS spreadsheet to pick both styles. Again thanks for your help. :)

      – Bharti Gulati
      Mar 28 at 6:34
















    Thanks, It works with Libra office, but not with WPS spreadsheet. There might be some issue with WPS spreadsheet to pick both styles. Again thanks for your help. :)

    – Bharti Gulati
    Mar 28 at 6:34





    Thanks, It works with Libra office, but not with WPS spreadsheet. There might be some issue with WPS spreadsheet to pick both styles. Again thanks for your help. :)

    – Bharti Gulati
    Mar 28 at 6:34













    0














    As the code looks plausible, just doing the complete run:



    The following I have tested with



    • org.apache.poi / poi / 3.16

    • org.apache.poi / poi-ooxml / 3.16

    It worked.



    try (XSSFWorkbook wb = new XSSFWorkbook()) 
    XSSFSheet sheet = wb.createSheet("With Rich Text");
    Row row = sheet.createRow(0);
    Cell cell = row.createCell(0);

    XSSFFont fontPlain = wb.createFont();

    XSSFFont fontBoldItalic = wb.createFont();
    fontBoldItalic.setBoldItalic(true);
    fontBoldItalic.setItalic(true);

    XSSFFont fontItalic = wb.createFont();
    fontItalic.setItalic(true);

    XSSFRichTextString cell1Value= new XSSFRichTextString("This is ");
    cell1Value.applyFont(fontPlain);
    cell1Value.append("sample ", fontBoldItalic);
    cell1Value.append("content", fontItalic);

    cell.setCellValue(cell1Value);
    wb.write(new FileOutputStream(xlsxFile));
    catch (IOException e)
    e.printStackTrace();



    My guess is a variable mix-up or something trivial. Maybe the font.






    share|improve this answer

























    • What apache poiversion provides method XSSFFont.setBoldItalic?

      – Axel Richter
      Mar 27 at 4:58











    • @Joop Egeen, It didn't work. The cell content is not getting bold and italic. :(

      – Bharti Gulati
      Mar 27 at 5:37











    • I have tested and now added the entire code. It probably has nothing to do with the library. Maybe special font handling in your code?

      – Joop Eggen
      Mar 27 at 9:37






    • 1





      @JoopEggen The problem was with WPS spreadsheet. I have checked the same code is working with Libra office now, but now with WPS spreadsheet. Thanks for your kind help. :)

      – Bharti Gulati
      Mar 28 at 6:55















    0














    As the code looks plausible, just doing the complete run:



    The following I have tested with



    • org.apache.poi / poi / 3.16

    • org.apache.poi / poi-ooxml / 3.16

    It worked.



    try (XSSFWorkbook wb = new XSSFWorkbook()) 
    XSSFSheet sheet = wb.createSheet("With Rich Text");
    Row row = sheet.createRow(0);
    Cell cell = row.createCell(0);

    XSSFFont fontPlain = wb.createFont();

    XSSFFont fontBoldItalic = wb.createFont();
    fontBoldItalic.setBoldItalic(true);
    fontBoldItalic.setItalic(true);

    XSSFFont fontItalic = wb.createFont();
    fontItalic.setItalic(true);

    XSSFRichTextString cell1Value= new XSSFRichTextString("This is ");
    cell1Value.applyFont(fontPlain);
    cell1Value.append("sample ", fontBoldItalic);
    cell1Value.append("content", fontItalic);

    cell.setCellValue(cell1Value);
    wb.write(new FileOutputStream(xlsxFile));
    catch (IOException e)
    e.printStackTrace();



    My guess is a variable mix-up or something trivial. Maybe the font.






    share|improve this answer

























    • What apache poiversion provides method XSSFFont.setBoldItalic?

      – Axel Richter
      Mar 27 at 4:58











    • @Joop Egeen, It didn't work. The cell content is not getting bold and italic. :(

      – Bharti Gulati
      Mar 27 at 5:37











    • I have tested and now added the entire code. It probably has nothing to do with the library. Maybe special font handling in your code?

      – Joop Eggen
      Mar 27 at 9:37






    • 1





      @JoopEggen The problem was with WPS spreadsheet. I have checked the same code is working with Libra office now, but now with WPS spreadsheet. Thanks for your kind help. :)

      – Bharti Gulati
      Mar 28 at 6:55













    0












    0








    0







    As the code looks plausible, just doing the complete run:



    The following I have tested with



    • org.apache.poi / poi / 3.16

    • org.apache.poi / poi-ooxml / 3.16

    It worked.



    try (XSSFWorkbook wb = new XSSFWorkbook()) 
    XSSFSheet sheet = wb.createSheet("With Rich Text");
    Row row = sheet.createRow(0);
    Cell cell = row.createCell(0);

    XSSFFont fontPlain = wb.createFont();

    XSSFFont fontBoldItalic = wb.createFont();
    fontBoldItalic.setBoldItalic(true);
    fontBoldItalic.setItalic(true);

    XSSFFont fontItalic = wb.createFont();
    fontItalic.setItalic(true);

    XSSFRichTextString cell1Value= new XSSFRichTextString("This is ");
    cell1Value.applyFont(fontPlain);
    cell1Value.append("sample ", fontBoldItalic);
    cell1Value.append("content", fontItalic);

    cell.setCellValue(cell1Value);
    wb.write(new FileOutputStream(xlsxFile));
    catch (IOException e)
    e.printStackTrace();



    My guess is a variable mix-up or something trivial. Maybe the font.






    share|improve this answer















    As the code looks plausible, just doing the complete run:



    The following I have tested with



    • org.apache.poi / poi / 3.16

    • org.apache.poi / poi-ooxml / 3.16

    It worked.



    try (XSSFWorkbook wb = new XSSFWorkbook()) 
    XSSFSheet sheet = wb.createSheet("With Rich Text");
    Row row = sheet.createRow(0);
    Cell cell = row.createCell(0);

    XSSFFont fontPlain = wb.createFont();

    XSSFFont fontBoldItalic = wb.createFont();
    fontBoldItalic.setBoldItalic(true);
    fontBoldItalic.setItalic(true);

    XSSFFont fontItalic = wb.createFont();
    fontItalic.setItalic(true);

    XSSFRichTextString cell1Value= new XSSFRichTextString("This is ");
    cell1Value.applyFont(fontPlain);
    cell1Value.append("sample ", fontBoldItalic);
    cell1Value.append("content", fontItalic);

    cell.setCellValue(cell1Value);
    wb.write(new FileOutputStream(xlsxFile));
    catch (IOException e)
    e.printStackTrace();



    My guess is a variable mix-up or something trivial. Maybe the font.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Mar 27 at 9:35

























    answered Mar 26 at 13:55









    Joop EggenJoop Eggen

    81.8k7 gold badges59 silver badges110 bronze badges




    81.8k7 gold badges59 silver badges110 bronze badges












    • What apache poiversion provides method XSSFFont.setBoldItalic?

      – Axel Richter
      Mar 27 at 4:58











    • @Joop Egeen, It didn't work. The cell content is not getting bold and italic. :(

      – Bharti Gulati
      Mar 27 at 5:37











    • I have tested and now added the entire code. It probably has nothing to do with the library. Maybe special font handling in your code?

      – Joop Eggen
      Mar 27 at 9:37






    • 1





      @JoopEggen The problem was with WPS spreadsheet. I have checked the same code is working with Libra office now, but now with WPS spreadsheet. Thanks for your kind help. :)

      – Bharti Gulati
      Mar 28 at 6:55

















    • What apache poiversion provides method XSSFFont.setBoldItalic?

      – Axel Richter
      Mar 27 at 4:58











    • @Joop Egeen, It didn't work. The cell content is not getting bold and italic. :(

      – Bharti Gulati
      Mar 27 at 5:37











    • I have tested and now added the entire code. It probably has nothing to do with the library. Maybe special font handling in your code?

      – Joop Eggen
      Mar 27 at 9:37






    • 1





      @JoopEggen The problem was with WPS spreadsheet. I have checked the same code is working with Libra office now, but now with WPS spreadsheet. Thanks for your kind help. :)

      – Bharti Gulati
      Mar 28 at 6:55
















    What apache poiversion provides method XSSFFont.setBoldItalic?

    – Axel Richter
    Mar 27 at 4:58





    What apache poiversion provides method XSSFFont.setBoldItalic?

    – Axel Richter
    Mar 27 at 4:58













    @Joop Egeen, It didn't work. The cell content is not getting bold and italic. :(

    – Bharti Gulati
    Mar 27 at 5:37





    @Joop Egeen, It didn't work. The cell content is not getting bold and italic. :(

    – Bharti Gulati
    Mar 27 at 5:37













    I have tested and now added the entire code. It probably has nothing to do with the library. Maybe special font handling in your code?

    – Joop Eggen
    Mar 27 at 9:37





    I have tested and now added the entire code. It probably has nothing to do with the library. Maybe special font handling in your code?

    – Joop Eggen
    Mar 27 at 9:37




    1




    1





    @JoopEggen The problem was with WPS spreadsheet. I have checked the same code is working with Libra office now, but now with WPS spreadsheet. Thanks for your kind help. :)

    – Bharti Gulati
    Mar 28 at 6:55





    @JoopEggen The problem was with WPS spreadsheet. I have checked the same code is working with Libra office now, but now with WPS spreadsheet. Thanks for your kind help. :)

    – Bharti Gulati
    Mar 28 at 6:55











    0














    The problem using WPS Spreadsheets is that they claim to be most compatible to Excel but sometimes they totally fail. This time they misinterpret all Boolean font settings (bold, italic, strike) if they explicitly are set true.



    Office Open XML provides Boolean elements having val attribute. Example: <b val="true"/> or <b val="false"/> or also <b val="1"/> or <b val="0"/>. But for set bold font having <b/> would be enough. And for not set bold font simply not having the b element at all would be enough.



    Apache poi always sets <b val="true"/> for bold and <b val="false"/> for not bold. But WPS Spreadsheets now seems to misinterpret <b val="true"/>. It expects <b/> only.



    The following code is the most compatible code for creating rich text strings for Excel. It supports Office Open XML (*.xlsx) as well as BIFF (*.xls) and it corrects the <Boolean val="true"/> to <Boolean/> only.



    import java.io.FileOutputStream;
    import org.apache.poi.ss.usermodel.*;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    import org.apache.poi.xssf.usermodel.XSSFRichTextString;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;

    import org.apache.xmlbeans.XmlObject;
    import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBooleanProperty;

    class CreateExcelRichText

    static RichTextString createRichTextString(Workbook workbook, String[] textParts, Font[] fonts)
    CreationHelper creationHelper = workbook.getCreationHelper();
    RichTextString richTextString = creationHelper.createRichTextString(String.join("", textParts));
    int start = 0;
    int end = 0;
    for (int tp = 0; tp < textParts.length; tp ++)
    Font font = null;
    if (tp < fonts.length) font = fonts[tp];
    end += textParts[tp].length();
    if (font != null) richTextString.applyFont(start, end, font);
    start += textParts[tp].length();

    if (richTextString instanceof XSSFRichTextString)
    //unset val="true" for boolean objects
    XSSFRichTextString xSSFRichTextString = (XSSFRichTextString)richTextString;
    String[] boolenanObjectsToUnset = new String[]"b", "i", "strike";
    for (String boolenanObjectToUnset : boolenanObjectsToUnset)
    XmlObject[] xmlObjects = xSSFRichTextString.getCTRst().selectPath(
    "declare namespace main='http://schemas.openxmlformats.org/spreadsheetml/2006/main' " +
    ".//main:" + boolenanObjectToUnset);
    for (XmlObject xmlObject : xmlObjects)
    CTBooleanProperty booleanProperty = (CTBooleanProperty)xmlObject;
    if (booleanProperty.getVal()) booleanProperty.unsetVal();



    return richTextString;


    public static void main(String[] args) throws Exception

    Workbook workbook = new XSSFWorkbook();
    //Workbook workbook = new HSSFWorkbook();

    Font font = workbook.createFont();

    Font fontBoldItalic = workbook.createFont();
    fontBoldItalic.setBold(true);
    fontBoldItalic.setItalic(true);

    Font fontItalic = workbook.createFont();
    fontItalic.setItalic(true);

    Font fontStrikeout = workbook.createFont();
    fontStrikeout.setStrikeout(true);

    String[] textParts = new String[]"This is ", "Sample ", "content. ", "This is crossed out.";
    Font[] fonts = new Font[]font, fontBoldItalic, fontItalic, fontStrikeout;

    RichTextString richTextString = createRichTextString(workbook, textParts, fonts);

    Sheet sheet = workbook.createSheet();
    sheet.createRow(0).createCell(0).setCellValue(richTextString);

    String fileName = (workbook instanceof XSSFWorkbook)?"Excel.xlsx":"Excel.xls";
    FileOutputStream out = new FileOutputStream(fileName);
    workbook.write(out);
    out.close();
    workbook.close();







    share|improve this answer



























      0














      The problem using WPS Spreadsheets is that they claim to be most compatible to Excel but sometimes they totally fail. This time they misinterpret all Boolean font settings (bold, italic, strike) if they explicitly are set true.



      Office Open XML provides Boolean elements having val attribute. Example: <b val="true"/> or <b val="false"/> or also <b val="1"/> or <b val="0"/>. But for set bold font having <b/> would be enough. And for not set bold font simply not having the b element at all would be enough.



      Apache poi always sets <b val="true"/> for bold and <b val="false"/> for not bold. But WPS Spreadsheets now seems to misinterpret <b val="true"/>. It expects <b/> only.



      The following code is the most compatible code for creating rich text strings for Excel. It supports Office Open XML (*.xlsx) as well as BIFF (*.xls) and it corrects the <Boolean val="true"/> to <Boolean/> only.



      import java.io.FileOutputStream;
      import org.apache.poi.ss.usermodel.*;
      import org.apache.poi.xssf.usermodel.XSSFWorkbook;
      import org.apache.poi.xssf.usermodel.XSSFRichTextString;
      import org.apache.poi.hssf.usermodel.HSSFWorkbook;

      import org.apache.xmlbeans.XmlObject;
      import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBooleanProperty;

      class CreateExcelRichText

      static RichTextString createRichTextString(Workbook workbook, String[] textParts, Font[] fonts)
      CreationHelper creationHelper = workbook.getCreationHelper();
      RichTextString richTextString = creationHelper.createRichTextString(String.join("", textParts));
      int start = 0;
      int end = 0;
      for (int tp = 0; tp < textParts.length; tp ++)
      Font font = null;
      if (tp < fonts.length) font = fonts[tp];
      end += textParts[tp].length();
      if (font != null) richTextString.applyFont(start, end, font);
      start += textParts[tp].length();

      if (richTextString instanceof XSSFRichTextString)
      //unset val="true" for boolean objects
      XSSFRichTextString xSSFRichTextString = (XSSFRichTextString)richTextString;
      String[] boolenanObjectsToUnset = new String[]"b", "i", "strike";
      for (String boolenanObjectToUnset : boolenanObjectsToUnset)
      XmlObject[] xmlObjects = xSSFRichTextString.getCTRst().selectPath(
      "declare namespace main='http://schemas.openxmlformats.org/spreadsheetml/2006/main' " +
      ".//main:" + boolenanObjectToUnset);
      for (XmlObject xmlObject : xmlObjects)
      CTBooleanProperty booleanProperty = (CTBooleanProperty)xmlObject;
      if (booleanProperty.getVal()) booleanProperty.unsetVal();



      return richTextString;


      public static void main(String[] args) throws Exception

      Workbook workbook = new XSSFWorkbook();
      //Workbook workbook = new HSSFWorkbook();

      Font font = workbook.createFont();

      Font fontBoldItalic = workbook.createFont();
      fontBoldItalic.setBold(true);
      fontBoldItalic.setItalic(true);

      Font fontItalic = workbook.createFont();
      fontItalic.setItalic(true);

      Font fontStrikeout = workbook.createFont();
      fontStrikeout.setStrikeout(true);

      String[] textParts = new String[]"This is ", "Sample ", "content. ", "This is crossed out.";
      Font[] fonts = new Font[]font, fontBoldItalic, fontItalic, fontStrikeout;

      RichTextString richTextString = createRichTextString(workbook, textParts, fonts);

      Sheet sheet = workbook.createSheet();
      sheet.createRow(0).createCell(0).setCellValue(richTextString);

      String fileName = (workbook instanceof XSSFWorkbook)?"Excel.xlsx":"Excel.xls";
      FileOutputStream out = new FileOutputStream(fileName);
      workbook.write(out);
      out.close();
      workbook.close();







      share|improve this answer

























        0












        0








        0







        The problem using WPS Spreadsheets is that they claim to be most compatible to Excel but sometimes they totally fail. This time they misinterpret all Boolean font settings (bold, italic, strike) if they explicitly are set true.



        Office Open XML provides Boolean elements having val attribute. Example: <b val="true"/> or <b val="false"/> or also <b val="1"/> or <b val="0"/>. But for set bold font having <b/> would be enough. And for not set bold font simply not having the b element at all would be enough.



        Apache poi always sets <b val="true"/> for bold and <b val="false"/> for not bold. But WPS Spreadsheets now seems to misinterpret <b val="true"/>. It expects <b/> only.



        The following code is the most compatible code for creating rich text strings for Excel. It supports Office Open XML (*.xlsx) as well as BIFF (*.xls) and it corrects the <Boolean val="true"/> to <Boolean/> only.



        import java.io.FileOutputStream;
        import org.apache.poi.ss.usermodel.*;
        import org.apache.poi.xssf.usermodel.XSSFWorkbook;
        import org.apache.poi.xssf.usermodel.XSSFRichTextString;
        import org.apache.poi.hssf.usermodel.HSSFWorkbook;

        import org.apache.xmlbeans.XmlObject;
        import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBooleanProperty;

        class CreateExcelRichText

        static RichTextString createRichTextString(Workbook workbook, String[] textParts, Font[] fonts)
        CreationHelper creationHelper = workbook.getCreationHelper();
        RichTextString richTextString = creationHelper.createRichTextString(String.join("", textParts));
        int start = 0;
        int end = 0;
        for (int tp = 0; tp < textParts.length; tp ++)
        Font font = null;
        if (tp < fonts.length) font = fonts[tp];
        end += textParts[tp].length();
        if (font != null) richTextString.applyFont(start, end, font);
        start += textParts[tp].length();

        if (richTextString instanceof XSSFRichTextString)
        //unset val="true" for boolean objects
        XSSFRichTextString xSSFRichTextString = (XSSFRichTextString)richTextString;
        String[] boolenanObjectsToUnset = new String[]"b", "i", "strike";
        for (String boolenanObjectToUnset : boolenanObjectsToUnset)
        XmlObject[] xmlObjects = xSSFRichTextString.getCTRst().selectPath(
        "declare namespace main='http://schemas.openxmlformats.org/spreadsheetml/2006/main' " +
        ".//main:" + boolenanObjectToUnset);
        for (XmlObject xmlObject : xmlObjects)
        CTBooleanProperty booleanProperty = (CTBooleanProperty)xmlObject;
        if (booleanProperty.getVal()) booleanProperty.unsetVal();



        return richTextString;


        public static void main(String[] args) throws Exception

        Workbook workbook = new XSSFWorkbook();
        //Workbook workbook = new HSSFWorkbook();

        Font font = workbook.createFont();

        Font fontBoldItalic = workbook.createFont();
        fontBoldItalic.setBold(true);
        fontBoldItalic.setItalic(true);

        Font fontItalic = workbook.createFont();
        fontItalic.setItalic(true);

        Font fontStrikeout = workbook.createFont();
        fontStrikeout.setStrikeout(true);

        String[] textParts = new String[]"This is ", "Sample ", "content. ", "This is crossed out.";
        Font[] fonts = new Font[]font, fontBoldItalic, fontItalic, fontStrikeout;

        RichTextString richTextString = createRichTextString(workbook, textParts, fonts);

        Sheet sheet = workbook.createSheet();
        sheet.createRow(0).createCell(0).setCellValue(richTextString);

        String fileName = (workbook instanceof XSSFWorkbook)?"Excel.xlsx":"Excel.xls";
        FileOutputStream out = new FileOutputStream(fileName);
        workbook.write(out);
        out.close();
        workbook.close();







        share|improve this answer













        The problem using WPS Spreadsheets is that they claim to be most compatible to Excel but sometimes they totally fail. This time they misinterpret all Boolean font settings (bold, italic, strike) if they explicitly are set true.



        Office Open XML provides Boolean elements having val attribute. Example: <b val="true"/> or <b val="false"/> or also <b val="1"/> or <b val="0"/>. But for set bold font having <b/> would be enough. And for not set bold font simply not having the b element at all would be enough.



        Apache poi always sets <b val="true"/> for bold and <b val="false"/> for not bold. But WPS Spreadsheets now seems to misinterpret <b val="true"/>. It expects <b/> only.



        The following code is the most compatible code for creating rich text strings for Excel. It supports Office Open XML (*.xlsx) as well as BIFF (*.xls) and it corrects the <Boolean val="true"/> to <Boolean/> only.



        import java.io.FileOutputStream;
        import org.apache.poi.ss.usermodel.*;
        import org.apache.poi.xssf.usermodel.XSSFWorkbook;
        import org.apache.poi.xssf.usermodel.XSSFRichTextString;
        import org.apache.poi.hssf.usermodel.HSSFWorkbook;

        import org.apache.xmlbeans.XmlObject;
        import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBooleanProperty;

        class CreateExcelRichText

        static RichTextString createRichTextString(Workbook workbook, String[] textParts, Font[] fonts)
        CreationHelper creationHelper = workbook.getCreationHelper();
        RichTextString richTextString = creationHelper.createRichTextString(String.join("", textParts));
        int start = 0;
        int end = 0;
        for (int tp = 0; tp < textParts.length; tp ++)
        Font font = null;
        if (tp < fonts.length) font = fonts[tp];
        end += textParts[tp].length();
        if (font != null) richTextString.applyFont(start, end, font);
        start += textParts[tp].length();

        if (richTextString instanceof XSSFRichTextString)
        //unset val="true" for boolean objects
        XSSFRichTextString xSSFRichTextString = (XSSFRichTextString)richTextString;
        String[] boolenanObjectsToUnset = new String[]"b", "i", "strike";
        for (String boolenanObjectToUnset : boolenanObjectsToUnset)
        XmlObject[] xmlObjects = xSSFRichTextString.getCTRst().selectPath(
        "declare namespace main='http://schemas.openxmlformats.org/spreadsheetml/2006/main' " +
        ".//main:" + boolenanObjectToUnset);
        for (XmlObject xmlObject : xmlObjects)
        CTBooleanProperty booleanProperty = (CTBooleanProperty)xmlObject;
        if (booleanProperty.getVal()) booleanProperty.unsetVal();



        return richTextString;


        public static void main(String[] args) throws Exception

        Workbook workbook = new XSSFWorkbook();
        //Workbook workbook = new HSSFWorkbook();

        Font font = workbook.createFont();

        Font fontBoldItalic = workbook.createFont();
        fontBoldItalic.setBold(true);
        fontBoldItalic.setItalic(true);

        Font fontItalic = workbook.createFont();
        fontItalic.setItalic(true);

        Font fontStrikeout = workbook.createFont();
        fontStrikeout.setStrikeout(true);

        String[] textParts = new String[]"This is ", "Sample ", "content. ", "This is crossed out.";
        Font[] fonts = new Font[]font, fontBoldItalic, fontItalic, fontStrikeout;

        RichTextString richTextString = createRichTextString(workbook, textParts, fonts);

        Sheet sheet = workbook.createSheet();
        sheet.createRow(0).createCell(0).setCellValue(richTextString);

        String fileName = (workbook instanceof XSSFWorkbook)?"Excel.xlsx":"Excel.xls";
        FileOutputStream out = new FileOutputStream(fileName);
        workbook.write(out);
        out.close();
        workbook.close();








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 30 at 7:23









        Axel RichterAxel Richter

        28.5k3 gold badges21 silver badges41 bronze badges




        28.5k3 gold badges21 silver badges41 bronze badges



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55358293%2fsetting-some-part-of-cell-content-to-bold-italic-using-apache-poi-4-0-1-library%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현