Cell borders

I create 3 spreadsheets using aspose.excel. In each, I loop over cells setting the properties of each one individually. When I use Cell.Borders.SetStyle(CellBorderType.Thin) to turn on cell borders, I can not edit the cell format (Format\Cells… from menu) in Excel. Also, every cell in one of the sheets is crossed out with an X. I have posted the sheet so you can see what is happening.

Which version are you using? have you tried the latest v2.9.7?

Cell.Borders.SetStyle(CellBorderType.Thin) sets all borders, including the diagonal borders. So you can see the crossed X. Please use

cell.Style.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;
cell.Style.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;


That worked. Thanks very much.

Sorry, spoke too soon. The Xs are gone now. However, when I try to bring up the cell formatting dialog on any cell, nothing happens. If I remove the border code, it comes up fine. Any ideas?

Just want to make sure I get an email this time…

Have you set border color?

cell.Style.Borders[BorderType.LeftBorder].Color = Color.Black;

That was it. Thanks.

@rcarickhoff,
Aspose.Excel is discarded and no more actively developed now. It is replaced by Aspose.Cells that supports all the advanced features available in different versions of MS Excel. You can set the border to an individual cell as well as to a range of cells and can format the border also. Here is a sample code that can be used to set border for a cell and range of cells.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET

// 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("Visit Aspose!");

// 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 left border
style.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thick;

// Setting the color of the left border
style.Borders[BorderType.LeftBorder].Color = Color.Black;

// Setting the line style of the right border
style.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thick;

// Setting the color of the right border
style.Borders[BorderType.RightBorder].Color = Color.Black;

// Apply the border styles to the cell
cell.SetStyle(style);

// Saving the Excel file
workbook.Save("book1.out.xls");

You can set the border to a range of cells as shown in the following sample code:

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// 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
Cell cell = worksheet.Cells["A1"];

// Adding some value to the "A1" cell
cell.PutValue("Hello World From Aspose");

// Creating a range of cells starting from "A1" cell to 3rd column in a row
Range range = worksheet.Cells.CreateRange(0, 0, 1, 3);

// Adding a thick top border with blue line
range.SetOutlineBorder(BorderType.TopBorder, CellBorderType.Thick, Color.Blue);

// Adding a thick bottom border with blue line
range.SetOutlineBorder(BorderType.BottomBorder, CellBorderType.Thick, Color.Blue);

// Adding a thick left border with blue line
range.SetOutlineBorder(BorderType.LeftBorder, CellBorderType.Thick, Color.Blue);

// Adding a thick right border with blue line
range.SetOutlineBorder(BorderType.RightBorder, CellBorderType.Thick, Color.Blue);

// Saving the Excel file
workbook.Save("book2.out.xls");

Here is the program output.

For more information on cells formatting refer to the following article:
Cells Formatting

Download the free trial version of this product here:
Aspose.Cells for .NET (Latest Version)

A ready to run solution is available here that can be used to test different features of this product without writing any code.