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

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.

Related

How can I set gradiant background of Excel sheet row using NPOI C#?

Here I create one style but it doesn't work its have only some option like solid or dotted etc. background so how can I set gradient background?
Here just one stylesheet how can I set gradient background?
ICellStyle tablestyle = sheet.Workbook.CreateCellStyle();
tablestyle.FillForegroundColor = IndexedColors.Blue.Index;
tablestyle.FillPattern = FillPattern.AltBars;

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

Colour applying to all cells

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

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.

VSTO diagonal line in an Excel cell

I am currently trying to find a way using VSTO in C# for Excel, to draw a diagonal line in a cell using C# code. But i can't find anyone on the web who even tried to do this.
Does anyone know how to achieve this ?
Thank you
(Excuse me for my bad english but it's not my language)
You can manipulate borders as follows:
Excel.Range range = ... the cell(s) you want ...;
var border = range.Borders[Excel.XlBordersIndex.xlDiagonalDown];
border.Weight = Excel.XlBorderWeight.xlThin;
border.LineStyle = Excel.XlLineStyle.xlContinuous;
The XlBordersIndex enumeration specifies which border you want to update:
xlDiagonalDown
xlDiagonalUp
xlEdgeBottom
xlEdgeLeft
xlEdgeRight
xlEdgeTop
...
I do not how in VSTO, but using COM you can do something like this:
ActiveSheet.Shapes.AddLine(BeginX, BeginY, EndX, EndY);

Categories