When I'm creating protected sheets using Apache POI, all cells are protected by default. I have to unlock each cell individually. Is it possible to protect a sheet while all cells are unprotected by default (so that I protect only the cells I want).
(code used)
/*for sheet protection*/
sheet.protected("password");
/*creating style to unlock cell */
CellStyle unlockedCellStyle = workbook.createCellStyle();
unlockedCellStyle.setLocked(false);
/*applying unlock style to cell */
cell.setCellStyle(unlockedCellStyle);
It is not possible to have created cells default to unlocked; locked is the default. But you are on the right track by creating a CellStyle with locked set to false. Make sure that you set locked to false on any and all of your new CellStyle objects that you want to be unlocked. Additionally, Excel has a limit on the number of cell styles that can be created in a Workbook, so re-use your CellStyle object(s) with each Cell you create.
It is possible to change default cell style in apache POI.
Instead of creating a new cell style for your cell if you had done getCellStyle() on your cell, this would have returned the default cell style, and you would be able to edit it,
according to this.
getCellStyle never returns null, in case of new cells it returns the default cell style.
So basically to edit default cell style of a cell, either do getCellStyle on a newly created cell or try this workbook.getCellStyleAt(0).
Refer to this for more details.
you can do XSSFCellStyle defaultCellStyle = wb.getCellStyleAt(0); to get default cell style and then defaultCellStyle.setLocked(false); this will change the default cellStyle of your workbook to unlocked.
Related
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 am trying to lock the data within an Excel Worksheet so that it is not possible to edit the data already written to the Worksheet, but leaving the rest of the empty spaces free for editing/ adding more data.
I have tried going through the whole sheet trying to set the cell style, using the code provided below but it is only the relevant code, but it doesn't work, which agrees to this question already asked
Lock single column in Excel using Apache POI
XSSFCellStyle lockCell = getFileWriter().getWorkbook().createCellStyle();
lockCell.setLocked(true);
for(Row row : sheet){
for(Cell mycell : row){
mycell.setCellStyle(lockCell);
}
}
the opposite: by locking the whole sheet and setting the relevant rows' cell style to unlock, I have tried that but the cells without any data don't unlock and so it hasn't worked for me. In any case how far and wide should one unlock the cells as it is not known how much space is needed for the unknown amount of data to be added.
Any help or suggestions would be greatly appreciated.
The iterator for a row will return no cells if the cells have not been explicitly created before. To unlock a cell it must be set explicitly to an unlocked style. Changing the default is not possible to my knowledge.
So the only way i see is to create cells for every row up to a certain column index.
To unlock cells with no data, suposing that your sheet is protected, you must set the default style of the columns you want to be unlocked, using the setDefaultColumnStyle method.
In your case you must do something like:
CellStyle editableStyle = workbook.createCellStyle();
editableStyle.setLocked(false);
for (int i = 0; i < numColumns; i++) {
sheet.setDefaultColumnStyle(i, editableStyle);
}
I am using Apache Poi in an Android application, but I have a problem with the update of xls file.
To change the value of a cell where there is already a value I have no problem, but I have a null pointer exeption when I change the value of empty cells.
I use the method setCellValue.
You may need to create the cell before you change the value of it. If a cell has no value it 'doesn't exist' so to speak and so you need to create it and then set it's value. You could try using the getCell() with mising rowPolicy to try and obtain a cell if their isn't currently one, like so:
myRow.getCell(7, Row.CREATE_NULL_AS_BLANK);//Should create cell if it is currently blank
Once you have the cell, try setting it's value as you have been doing.
Alternatively, try checking beforehand if you have a cell, e.g
if (myCell ==null) {
//Create cell code
Cell cell = row.createCell(0);
}
Good luck!
shouldn't this be working? fac is the uncolored workbook and facsheet is its uncolored sheet
sheet is the colored sheet,i'm trying to get the color from the workbook of one and set them into the other one
for(int i=2;rowIterator.hasNext();i++){
CellStyle style=fac.createCellStyle();
style.setFillForegroundColor(sheet.getRow(i).getRowStyle().getFillForegroundColor());
style.setFillBackgroundColor(sheet.getRow(i).getRowStyle().getFillBackgroundColor());
facsheet.getRow(i).setRowStyle(style);
}
How was the Excel spreadsheet where sheet comes from created? Did you create it in Excel or POI? If POI, did you use setRowStyle?
Usually, individual cells are styled, not entire rows, so getRowStyle() will return null.
Quoting the Javadocs from Row#getRowStyle():
Returns the whole-row cell styles. Most rows won't have one of these,
so will return null. Call isFormatted() to check first.
You will need to loop over all individual cells in each row, copying the CellStyle for each Cell. But be careful, there is a limit to the number of CellStyles that can be created for a spreadsheet. So if you already have created an identical CellStyle, then reuse it.
It seems that the character-encoding methods for the HSSF Excel Classes have been taken out from the 3.8 release. I really need to specify the enconding on at least the cells. The API does not even register the encoding methods any more, not even as deprecated, they have left the static fields of encoding, though.
¿How to set encoding in new POI versions?
Ok, there is a method for doing this from your code for what ever your needs may be:
//Create the workbook, and the font
HSSFWorkbook wb;
HSSFFont wbFont;
wbFont=wb.createFont();
wbFont.setCharSet(HSSFFont.ANSI_CHARSET); //Your Character encoding goes in the parameter
//Establish cell styles
HSSFCellStyle cellStyle =wb.createCellStyle();
cellStyle.setFont(wbFont);
//We create our cells with our data and with our specified format
HSSFCell cell =null;
cell = row.createCell(1);
cell.setCellStyle(cellStyle);
cell.setCellValue("MY DATA");
//Do the rest of work for your workbook and then you create it
Remember, these classes only work with .xls 2003