XSSFRichTextString with mixed font ignores newline after bold font only [duplicate] - java

:EDIT START:
Should anyone stumble across this q then here's a small heads up.
After getting newLines to work I wanted to set the row height to match the content. To do this I simply set the row height to the number of newLines in the cellValue.
However when creating a spreadsheet using poi it seems to be matter if you apply the cellStyle before or after the cellValue. In my case it only worked if I apply the cellStyle before adding the cellValue.
Oh and remember that rowHeight is defined as 1/256 of a character(!?!?!) so set rowHeight to
(short)(numberOfNewLines*256)
:EDIT END:
I have this piece of code that generates an Excel spreadsheet. The contents of some of the cells are created using
cell.setCellValue(new XSSFRichTextString(header));
cell.setCellStyle(cs);
However if I set
header = "Estimated\nDuration";
then the new line is not rendered in the resulting spreadsheet. It just comes out as "EstimatedDuration".
I'm using Apache POI v3.9.
Any suggestions?

Let's start with how to put a newline in an Excel spreadsheet using Excel. According to this article, edit the cell contents, placing the cursor at the desired newline location, and do Alt + Enter. The effect of this is to introduce a newline character into the string and turn on "Wrap Text" in the cell.
You have the newline character in the string already, but you'll need to add to your code to modify your CellStyle to turn on wrapped text, with the setWrapText method.
String header = "Estimated\nDuration";
cell.setCellValue(new XSSFRichTextString(header));
// Add this line to turn on wrapped text.
cs.setWrapText(true);
cell.setCellStyle(cs);
This appears to test successfully on .xls and .xlsx files.
It appears that Excel will only wrap text, newline present or not, if the cell style has the "wrap text" property set to true.

Related

Java Copy Excel to Word without losing font style

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.
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.

reading text along with formatting from excel cell using apache poi

I am trying to read ms-excel sheet cell value which contains RichText. Example below.
Welcome to apache-poi world.
Please use latest version of poi.
As you can see the word contains bold and italic word and new paragraph. I want to read this text along with style format, So that i can display in UI the way i have entered.

Conversion of word document into excel sheet

I have written a code to convert a Word document into an excel sheet.
The problem is that whenever there is a point starting with bullets in Word document line after a colon, after getting converted into excel sheet it is appearing into different cells or say column.
I want everything into the same cell but it is creating extra cells.
I want a code to handle this problem.
suppose if the line is:
I have three cars:
all belong to the same brand
.car1
.car2
.car3
in excel sheet the data is comming like
i have three cars,all belongs to same brand,car1,car2,car3
take commas as cells, but I want each data to be in the same cell.

Applying cell style over already styled cell that is two style on single cell

I am using Apache POI to create excel from csv file.
I have one column in this csv which tells what style should be applied on this row.
It contain Multiple value like
Bold|Red|grey
What i want is to - apply style to cell one by one e.g.
First Bold, over Bold i want to make it RED and then highlight row with GREY.
What i am doing right now is - splitting string of this column, running for loop and applying all three one by one.
But problem is only last style I got into excel, so last style override other styles.
Is there any way to append style and not override?
Thanks for help :)

Problem while copying JTable contents into Excel

I have a Standalone application in which I am trying to copy contents of JTable into Excel with line breaks included in my cells of JTable. I have used wrapping using "\"" to my cell contents. It's working fine but I am getting a square box type of symbol in place of line breaks in Excel. How to remove the symbol while copying? Is there any way to do this?
Excel expects DOS/Windows line breaks ("\r\n") instead of Unix/Java ones (just "\n"). To fix this, use this code:
s = s.replace("\r\n", "\n").replace("\n", "\r\n");
The first part makes sure that you don't have any DOS/Win line breaks and then converts everything to DOS.
If you're stuck with Java 1.4, use replaceAll().
[EDIT] The square box means that there is an unprintable character in the string. I suggest to create a file with Excel which contains a line break in a cell and check which characters Excel uses.

Categories

Resources