Home > Mobile >  Java Copy Excel to Word without losing font style
Java Copy Excel to Word without losing font style

Time:06-25

I have an excel file with a lot of sheets. I need to transfer some of that information to a word file. If this were all, there wouldn't be a problem. Poi api offers me everything what I need for that task. The problem comes when I need to copy that information from an excel cell to a word file without losing the font style since every cell has specific or multiples font color and I also need to preserve that info.

I know that Poi provides you Cell.getCellStyle() method so you can save your cell style but this is useful only if you want to copy an excel file to another one, but not for my case.

Do you know how to make what I need or if is an impossible task? May be I am using the wrong API.

CodePudding user response:

POI can do it for sure. You need set the font in your new word object. Like:

Font font = wb.createFont(); 
font.setFontName("xxx");
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setFont(font);

And if you like to apply multi font in one cell, here are the core code

//Set one cell with different font style  
HSSFRichTextString textString = new HSSFRichTextString(fileHead);
textString.applyFont(0,fileHead.indexOf("("), font);
textString.applyFont(fileHead.indexOf("("),fileHead.length(), font3);
cell.setCellValue(textString);

Hope helpful.

  • Related