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);
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!
Does anyone know how to change the color of the particular text of a cell in excel.
I am using apache poi and I could find out to change the text color of entire cell. But I want only a particular text.
Eg: Cell A1 has Hello World
I want "Hello" to be in blue and "World" to be in green.
How do I do this?
The key is using the HSSFRichTextString object to set the value of the cell. This object has an applyFont method which accepts a startingIndex, endingIndex and a Font. Thus, you can create fonts having the colors you want, then apply them to parts of the cell value using applyFont().
Here is some example code I cobbled together (completely untested):
// Set up a rudimentary worksheet with a cell in it
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet(“sheet1”);
HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell(0);
// Set up fonts
HSSFFont blueFont = workbook.createFont();
blueFont.setColor(HSSFColor.BLUE.index);
HSSFFont greenFont = workbook.createFont();
greenFont.setColor(HSSFColor.GREEN.index);
// create a cell style and assign the first font to it
HSSFCellStyle style = workbook.createCellStyle();
style.setFont(blueFont);
// assign the style to the cell
cell.setCellStyle(style);
// override the parts of the text that you want to
// color differently by applying a different font.
HSSFRichTextString richString = new HSSFRichTextString("Hello, World!");
richString.applyFont(6, 13, greenFont);
cell.setCellValue(richString);
At first create a style
//////////////////////Excel Header Style/////////////////////////
HSSFCellStyle headerlabelcs = wb.createCellStyle();
headerlabelcs.setFillForegroundColor(HSSFColor.PALE_BLUE.index);
headerlabelcs.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
headerlabelcs.setBorderLeft((short)1);
headerlabelcs.setBorderRight((short)1);
HSSFFont headerlabelfont = wb.createFont();
headerlabelfont.setFontHeightInPoints((short)12);
headerlabelfont.setFontName("Calibri");
headerlabelfont.setColor(HSSFColor.BLACK.index);
headerlabelfont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
headerlabelcs.setFont(headerlabelfont);
//////////////////////Excel Header Style/////////////////////////
add then this line will be added in your code
sheet.getRow(rowIndex).getCell(0).setCellStyle(headerlabelcs);
I want to create a Excel in which only a specific column is locked(Read-only), and the rest are editable,
I am using the following approach, but that doesn't seem to work.
Create two CellStyles, one with setLocked(true) and other with setLocked(false).
Then apply the locked style for all the cells in the column which needs to be locked, and the unlocked style for all the other cells.
Protect the sheet using sheet.protectSheet("");
But when I open the created Excel in open office, I notice that all the cells are locked!
None of them are editable.
How can I achieve the above requirement?
P.S : I cant use the data validation approach.
If you do the opposite it works. Protect the whole sheet and call setLocked(false) for the cells which should be editable.
String file = "c:\\poitest.xlsx";
FileOutputStream outputStream = new FileOutputStream(file);
Workbook wb = new XSSFWorkbook();
CellStyle unlockedCellStyle = wb.createCellStyle();
unlockedCellStyle.setLocked(false);
Sheet sheet = wb.createSheet();
sheet.protectSheet("password");
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("TEST");
cell.setCellStyle(unlockedCellStyle);
wb.write(outputStream);
outputStream.close();
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