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.
Related
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.
: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.
Requirement
I need a new XSSFCellStyle because I have to change some stylings.
Situation
I only have a XSSFCellStyle - I don't have the XSSFCell it belongs to. Thus I also don't have access to the related XSSFSheet or XSSFWorkbook.
What I already tried
I don't have the XSSFWorkbook therefore I can't call workbook.createCellStyle().
The XSSFCellStyle constructor needs at least a StylesTable which I also don't have (because I couldn't find a way to get it from the old XSSFCellStyle).
The cellStyle.cloneStyleFrom(XSSFCellStyle Source) doesn't really clone the style (it's more or less just a copy with the same pointers, so if I change something on one cellStyle the "cloned" cellStyle has the same changes).
Question
How can I get a new XSSFCellStyle?
Regards, winklerrr
Because of the way Excel stores the styles in the xls/xlsx, you need to have the Workbook/Sheet available in order to create a new Style. In fact Styles are not stored as part of Cells, but as a separate list in the Workbook. Because of this styles also should be re-used across Cells if possible.
Then you would do something like
XSSFCellStyle clone = wb.createCellStyle();
clone.cloneStyleFrom(origStyle);
to create a new Style and clone the settings from the original one.
There is a XSSFCellStyle.clone(), but I am not sure if it will do what you expect as the links to the Workbook will not be updated, so you will have two Style object which point at the same style-index in the list of styles in the Workbook...
There is a clone() method available on the XSSFCellStyle.
I don't know why but I didn't see it in the first place. My bad.
I have an application I'm writing which reads a docx file. It appears that I may need to read the formatting of the text, and not just the content. I have googled the matter but finding a search term that finds me what I'm looking for, most of it points me to using formatted text inputs and the like.
Does anyone know what class I should be using?
Apache poi should give you (at least some) access to Excel styles - such as colors, fonts etc. I'm not sure about exotic cases, but it's certainly possible to obtain cell font color for example. The following code works for me:
XSSFCell cell = ...
if (IndexedColors.WHITE.getIndex() == cell.getCellStyle().getFont().getColor()) {
...
}
I need to display a value in an excel cell formatted like a percentage, e.g. like 12.3%.
By default the value is displayed as Text, but I need to display it as a number.
What is the appropriate method to achieve this?
You need to:
Set your data as number (floating-point), not as text.
Specify cell format as percentage.
Something like:
cell.setCellValue(0.123); // set value as number
CellStyle style = workbook.createCellStyle();
style.setDataFormat(workbook.createDataFormat().getFormat("0.000%"));
cell.setCellStyle(style);
Take a look at user defined formats section of POI quick guide for more details. You may also want to go through the examples which show how to use different POI capabilities.
POI has built-in formats check this link first
and check this link for example
For percentage, it will be something like this
dataCell.setCellValue(.12)
CellStyle stylePercentage = workbook.createCellStyle();
stylePercentage.setDataFormat(workbook.createDataFormat()
.getFormat(BuiltinFormats.getBuiltinFormat( 10 )));
dataCell.setCellStyle(stylePercentage);