I am using POI 3.8 version and trying to read the background color of a cell in an excel sheet (XLS). Ideally I wanted to copy this cell with styles to a different sheet. When I did a cloneFrom(..) method, its not copying the correct colors to the target sheet.
When I tried to read the cell's background color separately, I am not getting the correct RGB value. If I could get the correct RGB value I can create a custom palette and set the same to the target sheet's cell.
Is there a correct way of reading the background color from a cell?
I have tried the following
cell.getCellStyle().getFillBackgroundColor()
cell.getCellStyle().getFillBackgroundColorColor().getTriplet()
The above line should give me RGB value of 159/200/222, but is giving me an incorrect value of 51/204/204.
Could someone please help on the same. I have already checked this forum for all possible answers and still could not get what I want.
The problem is that HSSF uses a color palette (essentially an array of colors) to define its RGB values. So the background color is actually an index into the palette. Here is some code to get the actual rgb values of the cell's background color:
CellStyle style = cell.getCellStyle();
short colorIdx = style.getFillForegroundColor();
HSSFPalette palette = ((HSSFWorkbook) wb).getCustomPalette();
HSSFColor color = palette.getColor(colorIdx);
short [] triplet = color.getTriplet();
// triplet will contain the actual rgb values
In your output spreadsheet, you have to add a custom color to the palette. If there is an empty space in your output's palette, you can use HSSFPalette.addColor. Otherwise, you will have to replace an unused existing color using HSSFPalette.setColorAtIndex. Once you add the custom color, you use CellStyle.setFillBackgroundColor to set the cell's background color, passing in the index of the custom color you just created.
If you can use an XSSF (xlsx) file instead of an HSSF (xls) file, you would not need to fool around with a color palette. You can use actual rgb values.
Related
As the title says, I would like to create a dropdown with no text only color options to fill the cell. I'd like to give only a few specific options to the user, for convenience and simplicity.
It would also facilitate my VBA coding because I'm using cells().Interior.ColorIndex to identify the color and do certain tasks depending on the color chosen. So, I suppose I'm restricted by the 56 colors in the index color pallet of Excel.
Is this possible?
Well, I didn't accomplish what I wanted 100% but with the help of Stackoverflow developers I got to a pretty cool result which works perfectly for what I wanted and it could probably be useful for someone else.
So what I did was a drop down menu with the colour names and a Conditiontal Formatting which converts the text and fill of the cell to the specific colour. I downsized the font size to 1, so I can use them as small rectangles.
I'm using Microsoft Excel 2016 to construct a forecast model with a range of outputs. I have used Excel's built-in Conditional Formatting setting (Graded Color Scale, to be precise) to make the outputs visually informative, setting a green gradient scale to distinguish between low-valued outputs and high-valued outputs.
The minimum-valued cell background is set to 'Green, Accent 6, Lighter 80%' and the maximum-valued cell background is set to 'Green, Accent 6'. This has worked well. The cell values near the maximum, however, can sometimes be difficult to read, since they are 'Black, Text 1' (not a great contrast with the darker Green).
I am hoping to add the same type of conditional formatting to the cell texts in the range of outputs to generate a color contrast: for example, in the lighter end of the gradient, the cell texts will be 'Black, Text', and in the darker end of the gradient, the cells texts will be 'White, Background.' Visually, that would solve my contrast issue.
Unfortunately, after reading through this forum and searching for conditional formatting guides on Google, I haven't found a solution on how to apply the two types of formatting (background and font) to the same range of cells. I am not practiced in VBA, so I am hoping for any pointer that involves Excel's native functionality, if that's possible.
To boil all this down into a one-liner: "How do I add gradient conditional formatting to both font and background to a range of cells?"
Thank you for the help!
I have done this in the past by creating a number of rules that give the appearance of a gradient on both. To do this however you would create as many rules you need to get the resolution you want. For example:
Suppose you want to assign font and background fill for each value in increments of 10% from 0 to 100%.
For this you need to get 10 font colors in rgb format that gradually shift in the gradient that you want here is a site that can help:
http://www.colorizer.org/
Then do the same with for you background colors.
Now pair a font and background color with the range that you want that rule to apply to and write the excel rule.
While this method is a pain to set up it avoids vba at all costs and if you use the sheet as a template for other sheets you won't have to mess with it much.
Creating your own sets of custom rules as per #rahicks answer in the most comprehensive way of doing this. If you have a fairly stable range of values, then a quick and dirty solution is to use Custom Number Formats.
On top of your current conditional formatting, apply a Custom Number Format using a condition. This example sets the colour to white if the value is greater than 50, and then uses the existing colour otherwise.
[White][>50]General;General
You'll need to replace 50 with the mid-point of your data range and you'll also need to replace both instances of General with whatever number format you currently have on the range.
I'm trying to create a live display using data that is coming from my excel sheet.
How can I fill a shape based on cell data i. e. 0=red and 1=green?
Thank you
If you have Visio 2010 Professional or Premium, you can use Data Graphics / Color By Value
If you only have the Standard edition, then you can use the Database Wizard to map values in an Excel column to FillForegnd
Alternatively, you can customize your Master shape to set the FillForegnd cell value when the value of value in another cell changes.
Visio does have a default Color Map, with sensible colors from 0 to 13, and shades of gray between 14 and 24.
However, you probably want to specify the RGB values instead.
So, if you have a Shape Data row called Prop.MyDataRow, which has values say between 0 and 5, then you could do the following.
Set the formula of FillPattern to =GUARD(1) - this prevents anyone accidently overwriting the formula, and thus blasting your hard work. The shape will have Solid fill.
Set the formula in FillForegnd to =GUARD(INDEX(Prop.MyDataRow,"0;1;2;3;4"))
Or
Set the formula in FillForegnd to =GUARD(INDEX(Prop.MyDataRow,"RGB(0,0,0);RGB(255,255,255);RGB(255,0,0);RGB(0,255,0);RGB(0,0,255)"))
This formula works for English. The separator character could be different in other languages.
To overcome this, you could use the alternative formula
=GUARD(INDEX(Prop.MyDataRow,RGB(0,0,0)&"~"&RGB(255,255,255)&"~"&RGB(255,0,0)&"~"&RGB(0,255,0)&"~"&RGB(0,0,255),"~"))
Visio will automatically use the RGB separator in your language.
As you can see, there are many ways of achieving the automatic color changing.
Requirement: I want to read an excel sheet and perform some action on the cells that are highlighted in the sheet i.e the cells which have their background colour as yellow.
I went through all the other similar posts but could not get the required information.
I tried using getFillBackgroundColor() which returns me value 64 each time for every colour. I tried using getFillBackgroundColorColor() which returns a colour object but I could not find a way to resolve it to the color name.
Please If anyone can help me with this
Take a look at the samples at http://poi.apache.org/spreadsheet/quick-guide.html#FillsAndFrills, you might need to use getFillForegroundColor()
Fills feel a bit unnatural to me, but it is just the nature of the beast. The fill itself is the cell background, but the fill itself has a foreground and background. So our typical two layer view of the world (foreground/background) doesn't work. In Excel we have three layers: Text color, Fill Foreground color, and Fill Background color. The Solid Fill happens to use the Fill Foreground color rather than the Fill Background color. Only two color fills have a Fill Background Color.
So if you have a solid "background", that means you have a solid fill which means you need to use getFillForegroundColor().
I want to highlight/fill a specific cell with color in a table generated using jsPDF, which is output to PDF.
Open this JSFiddle. As a base case, it draws a single table cell and sets both the fill colour and text colour to black. Uncomment line 12 and run it, which causes a PDF containing a single table cell to be saved to your download location. Open it and look - the text colour is black, but the cell fill colour is white.
The important line is line 6 - no matter which RGB values I feed in, the cell colour doesn't change.
doc.setFillColor(0,0,0);
How do I set a cell's fill colour? I've had a look through jspdf.plugin.cell.js, and there isn't anything overriding the fill colour. setFillColor is used in the function jsPDFAPI.printHeaderRow, which I followed as an example for my code.
Has anyone encountered this before?
Please let me know if I can provide any more details or explanation.
I haven't debugged it fully, but it works if I set doc.printingHeaderRow to true - http://jsfiddle.net/sp5qr4fd/4/
doc.printingHeaderRow = true;