Sorted I think. All I did was to pick at random a cell from the workbook you
posted - Sheet2 Cell B3 - cloned it's style, modified the clone and applied
that to another cell on the same sheet - Cell B7. It seems that the key is
to set the background and the forground colours explicitly following the
clone. Looking at the foreground and background colour values, on my PC both
were reported as being 64 and both were copied faithfully by the clone
method. This value - 64 - equates with the 'automatic' colour setting and so
it was Excel that 'decided' to choose a black background for the cell for
some reason that I cannot even begin to geuss at.
Anyway, have a look at the code below which I have run against the workbook
you posted.
public void cloneTest(String filename) throws IOException,
FileNotFoundException {
File file = null;
FileInputStream fis = null;
FileOutputStream fos = null;
HSSFWorkbook workbook = null;
HSSFSheet sheet = null;
HSSFCell cell = null;
HSSFCellStyle originalStyle = null;
HSSFCellStyle clonedStyle = null;
try {
// OPen the workbook and get cell B£ from sheet 2.
file = new File(filename);
fis = new FileInputStream(file);
workbook = new HSSFWorkbook(fis);
fis.close();
fis = null;
sheet = workbook.getSheetAt(1);
cell = sheet.getRow(2).getCell(1);
// Grab that cells style and then clone it
originalStyle = cell.getCellStyle();
clonedStyle = workbook.createCellStyle();
clonedStyle.cloneStyleFrom(originalStyle);
// Set the forground colour to red and the background colour to
// white. Should not really need to do this but it does solve
the
// problem.
clonedStyle.setFillForegroundColor(HSSFColor.RED.index);
clonedStyle.setFillBackgroundColor(HSSFColor.WHITE.index);
// Set fill pattern to diagonal bars.
clonedStyle.setFillPattern(HSSFCellStyle.THIN_BACKWARD_DIAG);
// Get another cell on the same sheet - this time B7 - and apply
// the cloned, modified style to it.
cell = sheet.getRow(6).getCell(1);
cell.setCellStyle(clonedStyle);
// Save the workbook away.
fos = new FileOutputStream(file);
workbook.write(fos);
}
finally {
if(fis != null) {
try {
fis.close();
fis = null;
}
catch(IOException ioEx) {
// I G N O R E
}
}
if(fos != null) {
try {
fos.close();
fos = null;
}
catch(IOException ioEx) {
// I G N O R E
}
}
}
}
Yours
Mark B
yehogold wrote:
>
> I am, indeed, cloning a cellStyle from the same workbook.
>
> The point of this portion of the program is to mark the cell of an
> existing workbook that was created using EXCEL, not the POI. Therefore, I
> am opening and reading in the workbook.
>
> What I would like to do is to mark the cell with red diagonal lines in the
> foreground. I would like the rest of the format to remain the same as it
> was before, allowing me to "unmark" it at a later point, returning it to
> its original format. Therefore, if the background is white, I would like
> it to stay white, if it is blue, I would like it to stay blue, if the font
> is bold, I would like it to stay bold, etc.
>
> Thank you again for spending so much time on this,
>
> yehogold
>
>
> MSB wrote:
>>
>> Sorry about BiffViewer. I was assuming that you were cloning a style from
>> one workbook for use in a different workbook. BiffViwere would have
>> allowed you to see what the differences were between the original style
>> and the clone had this been the case.
>>
>> Now, from what you have said, it seems as though you are cloning a style
>> within a workbook, can I ask, is this the case? If so, are you building
>> the workbook entirely using POI or are you opening and then modifying an
>> existing workbook? If the former, then the easy way to get around the
>> problem is to not clone and then modify a style but to build it
>> completely from scratch, even though this does mean a few lines of
>> repeated code.
>>
>> If I have the time tomorrow, I will experiment with the workbook you
>> attached to see if I can replicate the problem. Can I check to make sure
>> that the style you are looking for has a white background to the cell and
>> red diagonal bars in the foreground?
>>
>> Yours
>>
>> Mark B
>>
>>
>> yehogold wrote:
>>>
>>> I used the BiffViewer to look at the file, but I'm not sure how to read
>>> what I am looking at. I can see many different kinds of style objects
>>> and a couple of cell objects, but am not sure how you know what cell has
>>> what style.
>>> Is there anywhere where there are instructions on how to read the output
>>> of the BiffViewer?
>>>
>>> As shown in the code, the new cellStyle was created by cloning the
>>> cellStyle of the cell, modifying it, and setting the cellStyle of the
>>> cell to the newly modified clone.
>>>
>>> I don't remember what the original style of the cells were in the
>>> workbook attached to my first method. I am attaching a second workbook
>>> were I know that the original cellStyle of all the cells was the
>>> default, i.e. I did not modify the style before running the workbook
>>> through the program.
>>>
>>> Please let me know if anyone has any ideas.
>>>
>>> Thank again in advance for your time.
>>>
>>> Regaurds,
>>> yehogold
>>>
>>> http://www.nabble.com/file/p24955769/workbook.xls workbook.xls
>>>
>>>
>>> MSB wrote:
>>>>
>>>> As a first step, I would reccomend that you investigate alittle using
>>>> the BiffViewer utility. That may tell you which attributes of the cell
>>>> style are either not being set correctly or corrunpted by the clone
>>>> process.
>>>>
>>>> Does the example workbook you have posted contain both the corrupted
>>>> cell style and the style that you are cloning to create it in the first
>>>> instance?
>>>>
>>>> Yours
>>>>
>>>> Mark B
>>>>
>>>>
>>>> yehogold wrote:
>>>>>
>>>>> Hi.
>>>>>
>>>>> I have the following code used to modify the pattern of a cell:
>>>>>
>>>>> Workbook wb = cell.getSheet().getWorkbook();
>>>>> CellStyle errorStyle = wb.createCellStyle();
>>>>>
>>>>> errorStyle.cloneStyleFrom(cell.getCellStyle());
>>>>>
>>>>> errorStyle.setFillForegroundColor(Font.COLOR_RED);
>>>>> errorStyle.setFillPattern(CellStyle.THIN_BACKWARD_DIAG);
>>>>>
>>>>> cell.setCellStyle(errorStyle);
>>>>>
>>>>> When I run it, I end up getting these weird looking black cells.
>>>>> Excel 2003 will also not let me directly change the format of the
>>>>> black cells. How would I format the cells withouth getting this
>>>>> problem?
>>>>>
>>>>> I am inclosing one of the workbooks. The messed up black cells are on
>>>>> sheet2.
>>>>>
>>>>> Thank you in advance for your help.
>>>>> http://www.nabble.com/file/p24923092/workbook2.xls workbook2.xls
>>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>
>
--
View this message in context:
http://www.nabble.com/Weird-cells-appearing-after-modifying-the-format-of-a-cell-tp24923092p24969984.html
Sent from the POI - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]