Colour applying to all cells - c#

I am trying to apply colours to certain cells in a spreadsheet generated by Spreadsheet Gear.
My current code
var workbook = Factory.GetWorkbook();
var worksheet = workbook.Worksheets["Sheet1"];
// Set the new worksheet name
worksheet.Name = "Group export";
var cells = worksheet.Cells;
cells[0, 0].Style.Font.Size = 20;
cells[5, 0].Value = "Default Values";
cells["A6"].Style.Interior.Color = System.Drawing.Color.FromArgb(166,166,166); //Line 86
However, when opening the spreadsheet, I find that the font size and cell colour is applied to every single cell in the spreadsheet. The "Default Values" cell is only in the correct cell, however any background or font styling I apply anywhere in sheet applies to all cells.
I set up a watch for cells["A6"].Style.Interior.Color and cells["A5"].Style.Interior.Color, and a breakpoint directly after line 86 to confirm that this is where the styling happens.
Why is the styling applying to all cells in the spreadsheet?

The problem is that you are setting the Interior definition for the style (see IRange.Style.Interior and the IStyle interface) that the cell is using and not directly setting the interior for the cell itself (IRange.Interior). When you set any property at the style level, it will affect all other cells that use that same style.
Think "Normal", "Bad", "Good", etc., available from Excel's Ribbon > Home > Styles). By default, all cells utilize the "Normal" style, so by setting IRange.Style.Interior you are setting the color for all cells that utilize the "Normal" style--namely all cells in the workbook.
To set individual cell colors that don't affect any other cells using that style you would need to set the Interior directly under IRange, such as:
cells[0, 0].Font.Size = 20;
cells["A6"].Interior.Color = System.Drawing.Color.FromArgb(166,166,166);

For some reason, accessing the Style part causes it to apply a style to the entire sheet, instead of just the range. Removing "Style" fixed the issue
Instead of using
cells["A6"].Style.Interior.Color = System.Drawing.Color.FromArgb(166,166,166);
Using
cells["A6"].Interior.Color = System.Drawing.Color.FromArgb(166,166,166);
Formatted the cells correctly

Related

How to create a diagonal line in excel with C# using Aspose

I've worked with aspose for rendering reports but i don't know how to create a diagonal line across multiple cells in excel using aspose for C#?
It can be seen as a diagonal line is laid above the cells but i do not find a solution for that.
See the following sample code on how to apply Diagonal border to a cell in the worksheet using Aspose.Cells for your reference:
e.g.
Sample code:
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Obtaining the reference of the first (default) worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[0];
// Accessing the "A1" cell from the worksheet
Aspose.Cells.Cell cell = worksheet.Cells["A1"];
// Adding some value to the "A1" cell
cell.PutValue("Test!");
// Create a style object
Style style = cell.GetStyle();
// Setting the line style of the top border
style.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thick;
// Setting the color of the top border
style.Borders[BorderType.TopBorder].Color = Color.Black;
// Setting the line style of the bottom border
style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thick;
// Setting the color of the bottom border
style.Borders[BorderType.BottomBorder].Color = Color.Black;
// Setting the line style of the diagonal border
style.Borders[BorderType.DiagonalDown].LineStyle = CellBorderType.Thin;
// Setting the color of the diagonal border
style.Borders[BorderType.DiagonalDown].Color = Color.Black;
// Apply the border styles to the cell
cell.SetStyle(style);
// Saving the Excel file
workbook.Save(dataDir + "book1.out.xls");
You may also post your queries/comments in Aspose forums.
PS. I am working as Support developer/ Evangelist at Aspose.

How to work with style Index in Open xml?

Can Anyone please explain How Style Index in OpenXml works?
I have a business requirement where I need to apply background color to certain cells in an excel sheet. And Some style is already applied to other cells. So I need to decide which style Index I need to apply.
OpenXML styling can be confusing when you take the first look at it. Excel document styling falls under the SpreadsheetML markup language, which is different from Word and PowerPoint..
For typical cells in Excel, the only style information required is the StyleIndex (as you pointed out).
Cell cell16 = new Cell(){ CellReference = "HU1", StyleIndex = (UInt32Value)1U, DataType = CellValues.SharedString };
This corresponds to the s attribute in the XML:
<x:c r="HU1" s="1" t="s">
<x:v>0</x:v>
</x:c>
The StyleIndex is the zero-based index of the CellFormat record in the Styles part.
The styles part (aka the Stylesheet of the workbook) contains the following sections:
Numbering Formats
Fonts
Fills
Borders
Cell Style Formats
Cell Formats <== cell styleindex is referring to one of these
Cell Styles
Differential Formats
Table Styles
Colors
Stylesheet Extention List
Now inside the CellFormat record, there are references that refer back out to each of the following sections in the stylesheet:
Numbering Format (first bullet above)
Font (second bullet above)
Fill (third bullet above)
Border (fourth bullet above)
An example cell format in code looks like:
// this line is important to your question
CellFormat cellFormat5 = new CellFormat(){ NumberFormatId = (UInt32Value)0U, FontId = (UInt32Value)0U, FillId = (UInt32Value)0U, BorderId = (UInt32Value)11U,
//the rest of the CellFormat definition is not so important to your question
FormatId = (UInt32Value)0U, ApplyFill = true, ApplyBorder = true, ApplyAlignment = true };
To answer your question: apply a certain background color to certain cells . Let say we want to update cell B3 of your spreadsheet and B3 already has StyleIndex of 10.
You will need to follow these steps:
Step 1. If this is a new background color to the spreadsheet, add the background (aka Fill) to the Fills section of the Stylesheet (third bullet above) that contains your new color. If the color already exists, you need to find and remember the index of the existing Fill for that color. Either way, for this example lets say the Fill index you requre is 25.
Step 2. Create a new CellFormat that is a copy of the CellFormat at index 10. You will add this new CellFormat to the end of the CellFormat section. Lets say the index of the new CellFormat will be 53.
Step 3. You update the CellFormat at index 53 and make its Fill index property be 25 (from Step 1).
Last Step: Update the Cell in question B3, to have a new StyleIndex of 53
Note: This answer is for non-table cell styling in Excel - if you want styling information for table cells, please reply and Ill try and update or add an answer for it.
This answer comes mainly out of my experience and also interpretation of pages 73-79 of the free e-book: Open XML - The markup explained - by Wouter van Vugt. It is a good reference to use for all OpenXml.

Centering, Merging, and Wrapping Text In Cells - EPPlus

I'm trying to center a header(s) for an excel file like so:
But I am missing a few details, since the code below one writes on one line and does not expand the cell's height. Here is my code so far:
ws.Cells[$"A{row}:F{row}"].Merge = true;
ws.Cells[$"A{row}"].Style.WrapText = true;
ws.SelectedRange[$"A{row}"].Value = purchaseHistory[0].LineText;
To center the merged cell both vertical and horizontally just do this:
//Only need to grab the first cell of the merged range
ws.Cells[$"A{row}"].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
ws.Cells[$"A{row}"].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
If you need to do something with the height of the rows you will want to look at the CustomHeight setting. This should explain it: Autofit rows in EPPlus

How to avoid erasing the grid line color(default light gray) of excel sheet after changing the background of a cell in C#, ASP.NET?

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.

Increase datagridview row size automatically with wrapmode

I have read so many article to do my task. I want to use wrapmode to increase the height of the cell when the content reaches the cell width. The text want to display in a new line in that cell only.
So many articles refer me to set
dataGridView1.DefaultCellStyle.WrapMode =DataGridViewTriState.True;
dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
I have set these two properties to may datagridview.. Still the text is not wrapping in the cell. Am I missing anything??

Categories