This question already has answers here:
Multiline text in Excel cells
(3 answers)
Closed 7 years ago.
I have got a problem to create new lines into a cell. When I create a file, the new lines aren't shown in correct way. I hope someone can help me to solve this issue.
cell.setCellValue(text);
if(text.indexOf(System.getProperty("line.separator")) != -1)
{
int countRows = text.split(System.getProperty("line.separator")).length;
style.setWrapText(true);
cell.setCellStyle(style);
row.setHeightInPoints((countRows * sheet.getDefaultRowHeightInPoints()));
sheet.autoSizeColumn((short) countRows);
}
This is my Excel-File
When I make a new Excel-File, all the cells are looking like cell A1. There isn't a new line. When I click on the cell A1 shows the description over the cell the content with the new lines.
I made a double click in cell A2 and the cell is showing the content in the correct way.
The new line tag seems to work. My issue is, that the new line isn't shown at the cell in the excel file. But it is shown at the cell description. Is there a way to show all of the cells like the cell A2 without a double click?
Thanks
\n should be \r\n for excel files.
This worked for me ,
Workbook wb = new HSSFWorkbook();
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
CreationHelper createHelper = wb.getCreationHelper();
Sheet sheet = wb.createSheet("new sheet");
Row row = sheet.createRow((short) 0);
row.setHeight((short) (2*sheet.getDefaultRowHeight()));
CellStyle cs = wb.createCellStyle();
cs.setWrapText(true);
Cell cell = row.createCell(0);
cell.setCellStyle(cs);
cell.setCellValue(
createHelper.createRichTextString("This is \n a string"));
wb.write(fileOut);
fileOut.close();
Related
I am trying to auto size a column that is rotated 90 degree has a new line in the value, but seems autosize is not expanding the width of the cell if it contains a new line (only the part before the new line appears and the other part is hidden) as in the code below, is there any workaroudn for this
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet();
String value ="This is long text with new line ... \n this is the second line";
XSSFCellStyle style = workbook.createCellStyle();
style.setRotation((short)90);
style.setWrapText(true);
Row row = sheet.createRow(1);
Cell cell = row.createCell(0);
cell.setCellStyle(style);
cell.setCellValue(value);
workbook.getSheetAt(0).autoSizeColumn(0);
This kinda makes sense to me because of the setWrapText(true). According to apache-poi docs for autoSizeColumn(), this method
Adjusts the column width to fit the contents
But before you call autoSizeColumn(), you are calling setWrapText(true), which what it does is to fit all your text content inside the cell, so since the text is fit inside the cell, autoSizeColumn() has no work to do there because the text already fit and there is no need for the column to be widen.
I am working to generate excel using java apache poi
i just need to beautify it (with border)
below is the excel that i have successfuly create
and here is the excel that i wanted (see those border and currency and background color)
heres some of my code to generate the excel
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("sheet1");
Row row = sheet.createRow(rowIndex);
row.createCell(0).setCellValue("Product Name");
row.createCell(1).setCellValue("name");
FileOutputStream fileOut = new FileOutputStream("excel.xlsx");
workbook.write(fileOut);
fileOut.flush();
fileOut.close();
I assume you'd need to break down the creation of your cell in this format first before applying any style onto it:
Cell cell1 = row.createCell(0);
cell1.setCellValue("Product Name");
Later,
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setBorderTop((short) 1); // single line border
cellStyle.setBorderBottom((short) 1); // single line border
...//add many others here
cell1.setCellStyle(cellStyle); //apply that style to the cell
Simple way is to create a cellStyle at first and then just go ahead with numerous cell creations as per the application requirement! Next, just loop into each cell to apply the cellStyle if it is a common behavior that you need for all.
Hope that helps!
When I set the alignment on a cell that's a number it aligns, when the cell is text it won't.
Workbook workbook = new XSSFWorkbook();
CreationHelper creationHelper = workbook.getCreationHelper();
Sheet sheet = workbook.createSheet("Sheet1");
Row row = sheet.createRow(0);
// doesn't
Cell richTextCell = row.createCell(0);
RichTextString richTextString = creationHelper.createRichTextString("So rich!");
richTextCell.setCellValue(richTextString);
CellStyle richTextCellStyle = richTextCell.getCellStyle();
richTextCellStyle.setAlignment(CellStyle.ALIGN_RIGHT);
// works
Cell numberCell = row.createCell(1);
numberCell.setCellValue(12.34);
CellStyle numberCellStyle = numberCell.getCellStyle();
numberCellStyle.setAlignment(CellStyle.ALIGN_RIGHT);
FileOutputStream fileOutputStream = new FileOutputStream("workbook.xlsx");
try {
workbook.write(fileOutputStream);
} finally {
fileOutputStream.close();
}
I duplicated this behavior when creating an .xlsx workbook with an XSSFWorkbook. Interestingly, when I changed to an HSSFWorkbook, the behavior changed and both cells were aligned right.
I took a closer look at the .xlsx when I opened it in Excel. The Excel toolbar button that indicates "aligned right" was not highlighted for either cell, even though the number looked like it was aligned to the right. For reference, the toolbar button is in "Home", "Alignment" section, and looks like this (big closeup):
________
______
________
______
________
______
However, by default, all numbers in Excel are already right-aligned. It looks like for .xlsx workbooks, you can't just get the current cell style if it's the default cell style and modify it. You must create a new CellStyle, set its properties, and set the new cell style for the cell.
The following code works in .xls and .xlsx workbooks. Additionally, it also creates only one CellStyle object, to be used on all applicable cells, demonstrating the appropriate reuse of CellStyle objects.
CellStyle rightAligned = workbook.createCellStyle();
rightAligned.setAlignment(CellStyle.ALIGN_RIGHT);
Cell richTextCell = row.createCell(0);
RichTextString richTextString = creationHelper.createRichTextString("So rich!");
richTextCell.setCellValue(richTextString);
richTextCell.setCellStyle(rightAligned);
Cell numberCell = row.createCell(1);
numberCell.setCellValue(12.34);
numberCell.setCellStyle(rightAligned);
I am currently doing a program in Java that will run several tests and generate a report right after in excel. I was able to read and write through excel and the results of Passed or Failed are displayed in Results column. I was able to write these in excel but by supplying a default value on the cell (e.g. default) so the code will just overwrite it. I would like to write a comment on the Comment column, but I do not know how to write in a null cell. Here is a screenshot of the report I am generating (the link of the image available) and the code for reading and writing in excel as well.
FileInputStream fileInputStream = new FileInputStream("ReportExcel.xls");
HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
HSSFSheet worksheet = workbook.getSheetAt(0);
fileInput csvInputFile = new fileInput();
String[] sReturnValue = csvInputFile.arrayReturnSingleValue("fileInput.csv");
if (prodnameresult.equals(prodname) ){
Pass++;
totalResult++;
System.out.println ("Testcase1: Branding-Customised Product Name is PASSED");
//Harold's Input
HSSFRow row = worksheet.getRow(1);
HSSFCell cell = row.getCell((short) 4);
cell.setCellValue("Passed");
}
else{
Fail++;
totalResult++;
System.out.println("Testcase1: Branding-Customised Product Name is FAILED");
//assertEquals(prodnameresult.equals(prodname), true);
//Harold's Input
HSSFRow row = worksheet.getRow(1);
HSSFCell cell = row.getCell((short) 4);
cell.setCellValue("Failed");
}
//Harold's Input
FileOutputStream os = new FileOutputStream("ReportExcel.xls");
workbook.write(os);
os.close();
}
Report:
http://i47.tinypic.com/2s1lsfc.png
I don't have access to the report, but to answer to your question, if your cell is null you should create it and not get it as following:
HSSFCell cell = row.getCell((short) 4);
if(cell == null)
cell = row.createCell((short) 4);
cell.setCellValue("Passed");
You should maybe re-apply the Style to the Cell if it had a different one from the row.
I have a situation where I will be reading multiple lines and after some logic I need to write the lines in an Excel Sheet. I am using Apache POI for this purpose. However, the problem that I am facing is that, only the last line (from the loop) is being written to the Excel
Can someone please help me on this or provide some code snippet?
Thanks
Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
Sheet sheet = wb.createSheet();
Row row = sheet.createRow(2);
Cell cell = row.createCell(2);
cell.setCellValue("Use \n with word wrap on to create a new line");
//to enable newlines you need set a cell styles with wrap=true
CellStyle cs = wb.createCellStyle();
cs.setWrapText(true);
cell.setCellStyle(cs);
//increase row height to accomodate two lines of text
row.setHeightInPoints((2*sheet.getDefaultRowHeightInPoints()));
//adjust column width to fit the content
sheet.autoSizeColumn((short)2);
FileOutputStream fileOut = new FileOutputStream("ooxml-newlines.xlsx");
wb.write(fileOut);
fileOut.close();
Using newlines in cells