I'm trying to write multiline text to excel cells.
cell.setCellValue("line1 \n line2");
But when I open the document, I see only one line until I double-click it for editing, then it becomes two-lined. Why is it so? Thanks
You need to set the row height to accomodate two lines of text.
row.setHeightInPoints((2*sheet.getDefaultRowHeightInPoints()));
You need to set the wrap text = true to get the new line.
Try this :
Here wb is the Workbook.
CellStyle cs = wb.createCellStyle();
cs.setWrapText(true);
cell.setCellStyle(cs);
I found that you have to now include a third step after you follow the accepted answer to this question. You have to auto-size the column after you've added all of your data.
Assuming you're concerned about column 2,
sheet.autoSizeColumn(2);
See this example from Apache for context. It works for me with Java 8, poi-3.15.jar, and Excel for Mac.
This worked for me
cell.setCellValue("line1 \n\r line2");
Related
Im editing an xlsx file and Im trying to find some mistakes in numerical cells and make the bold.
the numbers are big for instance: 13882004729568
After finding them and changing the font to bold (or changing foreground color) the cell type is changing to its default state and I get : b1.3882E+13
even after trying to change the type to numerical.
XSSFFont italic = wb.createFont();
italic.setFontName("Arial");
italic.setItalic(true);
CellStyle italicStyle = wb.createCellStyle();
italicStyle.setFont(italic);
cell.setCellStyle(italicStyle);
cell.setCellType(XSSFCell.CELL_TYPE_NUMERIC);
Am I doing something wrong ?
Is it other way to do this ?
#maraca thx for the help
what I did is :
XSSFDataFormat df = wb.createDataFormat();
italicStyle.setDataFormat(df.getFormat("##0"));
so the format change made the difference
Official documentation of setCellType (link):
Set the cells type (numeric, formula or string).
If the cell currently contains a value, the value will be converted to match the new type, if possible. Formatting is generally lost in the process however.
So, just switch the statements and it should work:
cell.setCellType(XSSFCell.CELL_TYPE_NUMERIC);
cell.setCellStyle(italicStyle);
Another possibility is that you run out of styles, because you create a new one each time. Create the style once and reuse it.
You can either get the style before changing the cell type or you have to set the format too:
italicStyle.setDataFormat(wb.createDataFormat().getFormat("0"));
(Just to show the priciple, don't create a new data format each time, reuse.)
I am using Apache POI 3.9 to create cell comments.
I have been using the code for HSSF sheets suggested by Erik Pragt in creating cell comments using HSSFClientAnchor in apache poi for a few years, and it works well.
However now I have a need to add cell comments to XSSF sheets.
I have tried the code suggested by lastnitescurry in the same page, which works nicely, but it creates for me comments with a transparent background.
The code is reproduced below.
protected void setCellComment(Cell cell, String message) {
Drawing drawing = cell.getSheet().createDrawingPatriarch();
CreationHelper factory = cell.getSheet().getWorkbook()
.getCreationHelper();
// When the comment box is visible, have it show in a 1x3 space
ClientAnchor anchor = factory.createClientAnchor();
anchor.setCol1(cell.getColumnIndex());
anchor.setCol2(cell.getColumnIndex() + 1);
anchor.setRow1(cell.getRowIndex());
anchor.setRow2(cell.getRowIndex() + 1);
anchor.setDx1(100);
anchor.setDx2(100);
anchor.setDy1(100);
anchor.setDy2(100);
// Create the comment and set the text+author
Comment comment = drawing.createCellComment(anchor);
RichTextString str = factory.createRichTextString(message);
comment.setString(str);
comment.setAuthor("Apache POI");
// Assign the comment to the cell
cell.setCellComment(comment);
}
How can I change the background to a yellow background?
NOTE: If one edits the apache-poi created comment in Excel, then it will appear with a yellow background temporarily. However if one tries to format this comment to change the background from within Excel, then one can't. (The Color and Lines menu does not appear)
The answer is that my Java was manipulating an Excel .xlsm file that had "Show All Comments" set to true. Once I changed this setting, the java created the comments correctly.
I managed to reproduce the described behavior in a sheet that contained a Button Form Control.
I deleted the button and the comments are showing correctly.
As an alternative to a button, I used this guide to run a Macro from a cell
https://www.extendoffice.com/documents/excel/4354-excel-click-on-cell-to-run-macro.html
I have a generic problem with a not-so-generic requirement.
I have to convert an Excel file into PDF with a catch that all the formatting of each cell in the excel file has to be retained as it is. No change allowed. Like if a cell is formatted as Currency/Accounting then by default negative values are displayed in round braces e.g.(8.5) but when read from Java the value is -8.5. For numeric cell, value would be 0 but is displayed as hyphen (-).
Similarly for rest of the formatting types, the display changes.
As the cell has different cell types and the actual value in the cell is displayed according to the formatting applied on the cell, how to copy it in output PDF file?
Latest I learnt that DataFormatter is much useful in my case. So I wrote below code
DataFormatter df = new DataFormatter();
Cell c = row.getCell(i);
CellStyle style = c.getCellStyle();
cellvalue = df.formatRawCellContents(row.getCell(i).getNumericCellValue(), style.getDataFormat(), style.getDataFormatString());
One of the cell has formatting ($* #,##0.00);($* (#,##0.00);($* "-"??);(#_) (0x2c). If the value in the cell is 0 then ideally it should display "-" but it displays as 0.0 only.
Any help would be appreciated.
First install the pdf software in your local system then run this code in excel vb application. Give the name as you wish... that's it..
Sub expf()
Application.ActivePrinter = "PDF Complete on PDFC"
ExecuteExcel4Macro "PRINT(1,,,1,,,,,,,,2,""PDF Complete on PDFC"",,TRUE,,FALSE)"
End Sub
Using POI jar we can read excel . After reading for each row you can create PDF files using PDFBox. Also based on your requirements apply the styles for the pdf.
I'm using Apache POI to read an existing template excel file and would like to copy the existing styles in some header rows and apply them to new cells.
It seems like the existing formatting is not being applied (IE, Date, Currency, Percentage etc).
The code is pretty basic:
//read existing style
Row existingRow = sheet.getRow(headerRowIndex);
Cell existingCell = existingRow.getCell(0);
CellStyle currentStyle = existingCell.getCellStyle();
//apply date style here
Date date = StringUtil.toDate(map.get(column.getHeaderName()));
cell.setCellValue(date);
//apply previous style
cell.setCellStyle(currentStyle);
It does copy the font and background color etc but it seems like the formatting is lost (all cells have "general" formatting applied).
Also when I do this:
currentStyle.getDataFormat(); // always 0
So it makes me think that I'm not reading the formatting correctly. Any ideas on how to accomplish this?
OK I figured it out, it was my mistake. I was reading the style from the first cell in the row and applying it to all, instead of reading from each cell.
This fixes it
Cell existingCell = existingRow.getCell(i);
Using Apache Poi I'm exporting my table to an excel sheet. I extended ExcelExport class, override the getCellStyle method and customized my cell styles.
Now what i want is when a user edits any cell in the excel document i want that cell to change its color, so anyone later reviewing the document can easily see what has been changed.
Is there way to achieve this?
One solution could be to add conditional formatting to the cells use Apache POI's HSSFSheetConditionalFormatting.
For example, if the cell value written by your program is 5 then your conditional formatting could set the cell background to yellow if the value wasn't equal to 5