How to set this given image sample same border style when excel print?

image.png (79.5 KB)

@amjad.sahi, @Professionalize.Discourse, @John.He, @simon.zhao

How to set a small square dot border in cell

@imparthgalani

To set the same border style when printing an Excel document using Aspose.Cells, you can utilize the Style.SetBorder method to define the border properties for the cells you want to format. Here’s a concise example of how to apply a border style to a range of cells:

using Aspose.Cells;
using System.Drawing;

class Program
{
    static void Main()
    {
        // Create a new workbook
        Workbook workbook = new Workbook();
        Worksheet worksheet = workbook.Worksheets[0];

        // Define a range of cells
        var range = worksheet.Cells.CreateRange("A1:D4");

        // Create a style and set the border properties
        Style style = workbook.CreateStyle();
        style.SetBorder(BorderType.LeftBorder, CellBorderType.Thin, Color.Black);
        style.SetBorder(BorderType.RightBorder, CellBorderType.Thin, Color.Black);
        style.SetBorder(BorderType.TopBorder, CellBorderType.Thin, Color.Black);
        style.SetBorder(BorderType.BottomBorder, CellBorderType.Thin, Color.Black);

        // Apply the style to the range
        StyleFlag flag = new StyleFlag { Borders = true };
        range.ApplyStyle(style, flag);

        // Save the workbook
        workbook.Save("StyledBordersExample.xlsx");
    }
}

In this example, we create a new workbook, define a range of cells, and set the border style to thin black for all sides. Finally, we apply the style to the specified range and save the workbook. This will ensure that the borders are consistent when printed.

If you have any further questions or need additional assistance, please let me know!

Not in CellBorderType.Thin border, apply the given image, please correct the answer.

@imparthgalani,

Please try using CellBorderType.Hair for your requirements. See the following sample code for your reference.

// Create a new workbook
Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];

// Define a range of cells
var range = worksheet.Cells.CreateRange("A1:D4");

// Create a style and set the border properties
Style style = workbook.CreateStyle();
style.SetBorder(BorderType.LeftBorder, CellBorderType.Hair, System.Drawing.Color.Black);
style.SetBorder(BorderType.RightBorder, CellBorderType.Hair, System.Drawing.Color.Black);
style.SetBorder(BorderType.TopBorder, CellBorderType.Hair, System.Drawing.Color.Black);
style.SetBorder(BorderType.BottomBorder, CellBorderType.Hair, System.Drawing.Color.Black);

// Apply the style to the range
StyleFlag flag = new StyleFlag { Borders = true };
range.ApplyStyle(style, flag);

// Save the workbook
workbook.Save("e:\\test2\\out1.xlsx");

Hope, this helps a bit.

1 Like

@amjad.sahi, Thank you.

@imparthgalani,

We are glad to hear that the suggested code snippet meets your requirements. Please don’t hesitate to reach out to us at any time if you have additional questions or feedback.