i am using apache poi library to create a workbook, in the sheet i want to add border to my whole content table , there is option available to create border for a single cell, is there any way so that i can apply the border to whole sheet content
Use this default method, just create a HSSFCellStyle and save that object, then in the loop whenever you are writing the excel, just use this style object directly passed to the method
setDefaultColumnStyle(),, method doc tells you..
public void setDefaultColumnStyle(int column,CellStyle style)
Sets the default column style for a given column. POI will only apply this style to new cells added to the sheet.
Specified by:
setDefaultColumnStyle in interface Sheet
Parameters:
column - the column index
style - the style to set
Just let me know if its working for you?
Related
I have a problem with HSSFWorkbook object. I have a XLS template, where first row is dark gray, and next rows are light gray. During generation of the report, I want to change the background color of few cells (works perfectly) for red/blue. But after calling:
HSSFCellStyle style = workbook.createCellStyle();
style.setFillBackgroundColor(...)
or:
HSSFCellStyle style = workbook.getCellStyleAt(0) [default style of the workbook]
style.setFillBackgroundColor(...)
the styles of whole sheet is lost. The header becomes violet, and the rest cells becomes dark-dark-gray. I love violets, but I need my colours from the XLS not being changed.
In short - styles from my XLS template are not being preserved after I change style of one cell in the way described above.
I'm out of ideas. Please help.
Mateusz
I think the problem will be in your .xls spreadsheet itself. Styles that you chose on your template are incompatible with the current file format (and spreadsheet could be converted to .xlsx, but then you'd probably have to change your implementation to XSSFWorkbook).
You can check if styles are compatible by opening your template, making a minor change and save it - in your case warning should pop up that styles will be converted to the closest equivalents.
Solution: Edit your spreadsheet and pick compatible styles with your .xls format or upgrade to .xlsx format.
I am using Apache POI to write data to an Excel template. The template only contains headers on the first row, but I also applied specific styles to the entire column (e.g. Accounting and Percentage).
It would be nice to use these formats when I write data to the new cells. But if I use the createRow and createCell methods, the cell format is overwritten and I get General for all the cells. If I try to use getRow and getCell instead, I run into NullPointerExceptions retrieving the blank cell.
Is there a way to use the pre-existing cell formatting saved in the template? Or am I stuck setting the data format myself through the API?
Thanks for your help.
If you have applied specific styles to an entire column, then you can retrieve that CellStyle with Sheet's getColumnStyle method, passing it the 0-based column index. It retrieves a normal CellStyle object that can be used anywhere else CellStyles are accepted, such as in Cell's setCellStyle method.
For avoiding the NullPointerException, both getRow and getCell may return null if the row or cell doesn't exist, respectively. You will need to call createRow and/or createCell to create the Cell, on which you can always call setCellStyle.
cell.setCellStyle(sheet.getColumnStyle(index) works well.
I'm working with the Apache POI API to generate .xls files, and I want to set the printArea of my file.
I know there is a funcion called setPrintArea() but this one receive as parameters start and end row and column of my print area.
I want to know, if there is a way to set my print area for each sheet with the size of an A4 paper (210mmx290mm)
Thanks.
For each Sheet in the workbook, get its PrintSetup object and call setPaperSize on it, passing the appropriate PrintSetup constant: A4_PAPERSIZE.
mySheet.getPrintSetup().setPaperSize(PrintSetup.A4_PAPERSIZE);
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