I need to color some Excel cells with my C# application.
The next "must have" is: I have to do this with the infragistics reference.
I was able to paint some cells:
worksheet.Rows[row].Cells[col].CellFormat.FillPatternBackgroundColor = Color.DarkSeaGreen;
but there is a grey pattern which makes the reading of the cells really hard. Is there any solution how I can take those grey patterns away?
I couldn't find any backgroundcolor attribute except this FillPatternBackgroundColor...
Greez Arion
Assuming you're using a version prior to 12.1, set the fill pattern to solid:
worksheet.Rows[row].Cells[col].CellFormat.FillPattern = FillPatternStyle.Solid;
If you're on 12.1, those properties of been marked obsolete in favor of the Fill property:
worksheet.Rows[row].Cells[col].CellFormat.Fill = new CellFillPattern(new WorkbookColorInfo(Color.DarkSeaGreen), null, FillPatternStyle.Solid);
Try this:
worksheet.Rows[row].Cells[col].interior.color = rgb(0,255,0)
or this:
worksheet.Rows[row].Cells[col].interior.colorindex = 3
I know everyone who views this will probably know this, but to add to this answer; to set the color if you only have the RGB using the Infragistics Excel you can do this as well:
worksheet.Rows[row].Cells[col].CellFormat.Fill = CellFill.CreateSolidFill(Color.FromArgb(255, 0, 255, 0))
(Alpha, Red, Green, Blue) I believe the alpha(opacity) it always ignored by excel so don't let that throw you off.
I am using 2015.2.
Related
Using EPPlus, I understand that you can set the background color of individual cells, or a range of cells as follows:
ws.Cells["A1:B1"].Style.Fill.PatternType = ExcelFillStyle.Solid;
ws.Cells["A1:B1"].Style.Fill.BackgroundColor.SetColor(Color.White);
Is there any way to set the background color of the entire worksheet? Or is this just a case of setting a very wide range of cells?
So for example I could do:
ws.Cells["A1:AZ10000"].Style.Fill.PatternType = ExcelFillStyle.Solid;
ws.Cells["A1:AZ10000"].Style.Fill.BackgroundColor.SetColor(Color.White);
I'm not sure if there is a performance concern doing this? I tried it with "A1:ZZ100000", it just hung.
Just use cells directly without specifying address range:
ws.Cells.Style.Fill.PatternType = ExcelFillStyle.Solid;
ws.Cells.Style.Fill.BackgroundColor.SetColor(Color.White);
Tested it, it takes no time.
Simpler yet!
ws.SetBackgroundColor(Color.HotPink);
How can one access the last-used colour in PowerPoint from within a C# VSTO Add-In?
In the example below, I am referring to the orange colour indicated by the bucket fill tool.
For Posterity
Here is the C# version of the accepted answer:
var shape = Globals.ThisAddIn.Application.ActivePresentation.Slides[1]
.Shapes.AddShape(MsoAutoShapeType.msoShapeRectangle, 0, 0, 100, 100);
shape.Select();
Globals.ThisAddIn.Application.CommandBars.ExecuteMso("ShapeFillColorPicker");
var color = shape.Fill.ForeColor.RGB;
shape.Delete();
Other than prowling the XML, which may or may not disgorge anything useful, I don't know of any way to do this via the object model, but you could draw a rectangle, invoke the button's action to color it, grab the color then delete the rectangle. Here's how you could do it in VBA:
Function GetColorBucketColor() As Long
Dim oSh As Shape
Set oSh = ActivePresentation.Slides(1).Shapes.AddShape(msoShapeRectangle, 0, 0, 100, 100)
oSh.Select
Application.CommandBars.ExecuteMso ("ShapeFillColorPicker")
GetColorBucketColor = oSh.Fill.ForeColor.RGB
oSh.Delete
End Function
In real life it's a bit more complicated than that; the color in the ShapeFill button might be a theme color or it might be a standard RGB color; this will return the RGB value. Setting other shapes to this color would produce the same appearance, but the shapes would no longer follow the theme. Sometimes that matters, sometimes not.
Another potential drawback is that for this to work, something must be selected. That means that you won't be able to do it in an invisible window, and that there'll be a bit of a blip when PPT selects the newly drawn shape.
My sample code to change the cell color is this. range refers to a cell range.
range.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow);
If i try to change the cell color back to white. Im using this.
range.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.White);
But this erases my cell range grid lines(default color of the lines are grey) and everything becomes white. i.e.only 'range' grid lines becomes white and all other cells have the default excel grid color.
Thanks
System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow);The problem is occuring because the interior colour of the range of cells is not actually white (it just appears white). Each cell by default is actually transparent (-4142).
So do this when you want to set it back to "normal":
range.Interior.Color = -4142
Edit: Best to use Constants and -4142 is equal to xlNone.
Anonymous Type
I don't understand why this doesn't work for you? Can you please refine your question? I tried the following and it worked just fine..
const Int32 transparent_Color = -4142;
//Set Yellow
var Rng = ActiveSheet.Range["D5:E7"];
Rng.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow);
//Set transparent (gridlines come back, so long as you haven't got other code that is interacting with your cells)
var Rng = ActiveSheet.Range["D5:E7"];
Rng.Interior.Color = transparent_Color;
Trick: Open Excel > Developer Toolbar > Record Macro > select/highlight range > change backcolor to yellow > change backcolor to white. Now set the Style to Normal.
Stop the Macro from recording.
Then press Alt + F11 to go into VBA Editor and look at the code in Module 1.
Simply convert that code to C# - it is almost the exact same Object Model in C# as it is with VB.
How does one set the alignment of text within a chart legend object? I've tried using:
myChartName.Legends["mySeriesName"].Alignment = stringAlignment.Near
With no effect. I've also tried to create custom legend items, again resulting in no effect. Text is ALWAYS centered (along with the series marker) in the legend "box". The only text I have been able to align is the title, but I don't need titles in my application.
My research to date says the legend object is basically a table with (by default) two cells. If that is the case there should be a way to access those cells and manipulate them as table cells. So, what gives? Why can't I access the text alignment properties of the legend object? Clearly, there is something I'm missing, but for the life of me I can't seem to figure this out. Quite frustrating.
Problem solved. The CustomItem approach wasn't working either, so I tried using the LegendCellColumn Class.
I changed the LegendStyle from Column to Row, then added two CellColumns, one for the series symbol and one for the legend text. Set the alignment, margins, and column widths (that turned out to be the trick), and voila; a legend that looks like I want. Here's the code for anyone with a similar issue.
chartSel.Legends[ySeries.Name].CellColumns.Add(new LegendCellColumn("", LegendCellColumnType.SeriesSymbol, ""));
chartSel.Legends[ySeries.Name].CellColumns[0].Alignment = ContentAlignment.TopLeft;
chartSel.Legends[ySeries.Name].CellColumns[0].Margins = new System.Windows.Forms.DataVisualization.Charting.Margins(0, 0, 1, 1);
chartSel.Legends[ySeries.Name].CellColumns[0].MinimumWidth = 250;
chartSel.Legends[ySeries.Name].CellColumns[0].MaximumWidth = 250;
chartSel.Legends[ySeries.Name].CellColumns.Add(new LegendCellColumn("", LegendCellColumnType.Text, ySeries.Name));
chartSel.Legends[ySeries.Name].CellColumns[1].Alignment = ContentAlignment.MiddleLeft;
chartSel.Legends[ySeries.Name].CellColumns[1].Margins = new System.Windows.Forms.DataVisualization.Charting.Margins(0, 0, 1, 1);
chartSel.Legends[ySeries.Name].CellColumns[1].MinimumWidth = 1500;
chartSel.Legends[ySeries.Name].CellColumns[1].MaximumWidth = 1500;
It's probably not the most efficient way to do it, but it works. Technically, the legend symbol and text are still centered in the object, but because I'm forcing the widths of the two columns it has the appearance of being left-justified.
Hopefully, this may help another newbie like me avoid days of consternation.
My understanding is that Legend alignment relates to the Docking property, not really how the text is aligned within the legend. So setting Alignment to Near means positioning the legend box near the Docking direction.
It is quite hard to explain this textually. The MS Chart Samples have a subsection named Chart features -> Legends -> Style and Auto Positioning which will help you play with those two properties and understand how they are meant to work.
For inner legend alignment, you may need to use Legend.CustomItems and define LegendCell individually.
chart.Legends["Default"].CustomItems.Clear();
chart.Legends["Default"].CustomItems.Add(new LegendItem("LegendItem", Color.Red, ""));
chart.Legends["Default"].CustomItems[0].Cells.Add(new LegendCell(LegendCellType.Text, "My text", ContentAlignment.MiddleLeft));
This is described inside the Chart features -> Legends -> Legend Cells section of the samples.
While continuing to try to figure this out I stumbled on this tidbit of info from the following MSDN library page:
http://msdn.microsoft.com/en-us/library/dd456711(v=vs.100).aspx
"NOTE: You cannot adjust the individual legend items and cells in the Chart.Legends collection. To adjust them, use custom legend items."
So, back to the CustomItem route. I've got this code gleaned from several sources (including you, Dominique, thanks):
chartSel.Legends.Add(ySeries.Name);
chartSel.Series[ySeries.Name].IsVisibleInLegend = false;
chartSel.Legends[ySeries.Name].CustomItems.Clear();
LegendItem li = new LegendItem();
li.ImageStyle = LegendImageStyle.Line;
li.Cells.Add(LegendCellType.SeriesSymbol, "", ContentAlignment.TopLeft);
li.Cells[0].BackColor = Color.CornflowerBlue; //color is only to see the cell bounds
li.Cells.Add(LegendCellType.Text, ySeries.Name, ContentAlignment.TopLeft);
li.Cells[1].BackColor = Color.Aquamarine; //color is only to see the cell bounds
chartSel.Legends[ySeries.Name].CustomItems.Add(li);
From what I can find it should work, but it doesn't. It results in a legend object with two cells; the first is empty and the second has left-justified text. I tried to post an image of it, but the system says I'm not yet worthy.
Why there is no line for the series symbol is also a mystery, but that's another problem to solve. The text justification issue is my main concern at the moment. It looks like the text within the cells is left-justified as desired, but the cells themselves are centered in the legend object; not what I wanted. I want those cells to be left-justified in the object too, so the first cell is against the left edge of the legend object.
Ideas?
the situation:
.net 3.5
c# or vb.net(also tested)
word 2007 add-in
I'm trying to set the Background Color of the text to a custom rgb color.
The code is the following:
Range r = this.Application.ActiveDocument.Range();
r.Text = "blabla";
r.Font.Shading.BackgroundPatternColor =(WdColor) Color.FromArgb(0, 214, 227,188).ToArgb();
At first it seems to work, except that the color is not the right one. It seems that whenever I set a custom color, it changes it to an existing WdColor constant. Having a look at the doc, it says :
Returns or sets the 24-bit color that's applied to the background of the Shading object. Can be any valid WdColor constant or a value returned by Visual Basic's RGB function.
So, my question is: does anybody has an idea of how it's supposed to work?
Thanx
Use ColorTranslator
Range r = this.Application.ActiveDocument.Range();
r.Text = "blabla";
r.Font.Shading.BackgroundPatternColor =(WdColor)ColorTranslator.ToOle(0, 214, 227,188);