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.
Related
Im trying to move a sheet in my excel between other sheets with Apache POI.
to accomplish that Im calling
workbook.setSheetOrder("sheetToMove", 1);
problem is in another sheet i have formulas like "=sheetToMove!A2"
if I do above call, the formula changes to "=unrelatedSheet!A2"
my current workaround involves going through all cells of the referencing sheet, storing away all the formulas + row/column information in a datastructure before changing sheet orders. after I changed the order, I go thorugh the stored formulas and put them in the correct cell. Is there a more efficient way of changing sheet order without changing the formulas?
I have written a code to convert a Word document into an excel sheet.
The problem is that whenever there is a point starting with bullets in Word document line after a colon, after getting converted into excel sheet it is appearing into different cells or say column.
I want everything into the same cell but it is creating extra cells.
I want a code to handle this problem.
suppose if the line is:
I have three cars:
all belong to the same brand
.car1
.car2
.car3
in excel sheet the data is comming like
i have three cars,all belongs to same brand,car1,car2,car3
take commas as cells, but I want each data to be in the same cell.
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);
}
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.
I am developing a desktop application related to Excel sheets. I have some problems inserting rows between two rows. Is there any possibility to do this in Java using Apache POI?
Workbook wb3=WorkbookFactory.create(new FileInputStream("Book1.xls"));
Sheet sh=wb3.getSheet("sheet1");
//Reading the available rows using (sh.getRow(1))
//Here i need to insert second row (????)
//I have third row here which already exists (sh.getRow(3))
I have a solution which is working very well:
Workbook wb3=WorkbookFactory.create(new FileInputStream("Book1.xls"));
Sheet sh=wb3.getSheet("sheet1");
int rows=sh.getLastRowNum();
Shift the number of rows down the sheet.
sh.shiftRows(2,rows,1);
Here
2 -- Position at which we need to insert row
rows -- Total rows
1 -- How many rows we are going to insert
The reason why we are doing the above process is to make an empty row; only then can we create a new row.
Now we shifted the rows, then we can do our stuff
Coding :
sh.createRow(1);
The above code is used to insert a row at the 1st position, as we defined.