Image Gridline color different from the color in the excel sheet

I am using Aspose.Cells to take snapshot of a range, when I get the image it has gridlines which are thicker and of a different color than in the excel sheet. I took one image using Range.getImage() of excel javascript API and I want my aspose image to look as close to that as possible. Also, I would rather not use borders because they overwrite any borders that are manually set on a range or table in excel(look at range called table).
Excel Js API Image -
image.png (9.6 KB)
(Don’t mind the blue background outside the range, not part of the image)

Aspose Image -
image.png (4.1 KB)

Source file-
sourceFile.zip (25.7 KB)

Here is my code -
Aspose.Cells.Worksheet worksheet = workbook.Worksheets[namedRange.SheetIndex - 1];
Aspose.Cells.Range range = namedRange.GetRange();

worksheet.AutoFitRows();
worksheet.PageSetup.PrintArea = namedRange.RefersTo;
worksheet.PageSetup.LeftMargin = 0;
worksheet.PageSetup.RightMargin = 0;
worksheet.PageSetup.TopMargin = 0;
worksheet.PageSetup.BottomMargin = 0;
worksheet.PageSetup.PrintGridlines = true;
worksheet.IsGridlinesVisible = true;

ImageOrPrintOptions imgOptions = new ImageOrPrintOptions
{
OnePagePerSheet = true,
ImageType = ImageType.Jpeg,
HorizontalResolution = 400,
VerticalResolution = 400,
PrintingPage = PrintingPageType.IgnoreBlank,
GridlineType = GridlineType.Hair
};

SheetRender sr = new SheetRender(worksheet, imgOptions);

@Adhirath

To address the issue of gridline color and thickness in the image generated by Aspose.Cells compared to the Excel JavaScript API, you can adjust the GridlineType property in your ImageOrPrintOptions.

In your current implementation, you are using GridlineType.Hair, which may not match the appearance of the gridlines in Excel. You can try using GridlineType.Normal or GridlineType.Thick to see if that better matches the gridline style you are looking for.

Here’s an updated version of your code snippet with the suggested change:

ImageOrPrintOptions imgOptions = new ImageOrPrintOptions
{
    OnePagePerSheet = true,
    ImageType = ImageType.Jpeg,
    HorizontalResolution = 400,
    VerticalResolution = 400,
    PrintingPage = PrintingPageType.IgnoreBlank,
    GridlineType = GridlineType.Normal // Change this to Normal or Thick
};

Additionally, ensure that the PrintGridlines property is set to true, which you have already done. This will ensure that the gridlines are included in the image output.

If the gridlines still do not match after these adjustments, consider checking the Excel file’s gridline settings directly, as they may affect how Aspose renders them.

For further customization, you might also explore applying borders to specific cells if necessary, but as you mentioned, this could overwrite any manually set borders in your Excel sheet.

Feel free to test these changes and see if they help achieve the desired appearance for your image output.

Sources:
[1]: Apply Style on cell

Only two options available on gridlinetype - hair and dotted, no normal.

@Adhirath
By testing with sample files and code on the latest version v25.3, we can reproduce the issue.Image gridline color is different from the color in the excel sheet.

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): CELLSNET-58026

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

is there something I can do about it for now?

@Adhirath
As a temporary solution, you can set borders on the area where grid lines need to be output and modify the borders to the desired style. We need time to fix the issue with the grid lines. Once there is an update, we will notify you promptly.

@Adhirath ,

When you printview the file in Excel or save the file to pdf manually in Excel, you will find the color of gridline is also black.

We can consider to add a new enum in GridlineType(e.g. Gray), but the line width will be same as GridlineType.Hair. Please let us know your feedback.

works for me

@Adhirath ,

OK, we will add a new enum in GridlineType(e.g. Gray).

1 Like

@Adhirath
We will public Worksheet.GridlineColor property in the next version 25.4, then you can set your excepted gridline color.

The issues you have found earlier (filed as CELLSNET-58026) have been fixed in this update. This message was posted using Bugs notification tool by leoluo

Hi Simon how do I use this?

@Adhirath
Please refer to the following example code to set the grid line color.

Workbook workbook = new Workbook(filePath + "sourceFile.xlsx");
Aspose.Cells.Worksheet worksheet = workbook.Worksheets[0];
worksheet.AutoFitRows();
worksheet.PageSetup.PrintArea = "R4:S7";
worksheet.PageSetup.LeftMargin = 0;
worksheet.PageSetup.RightMargin = 0;
worksheet.PageSetup.TopMargin = 0;
worksheet.PageSetup.BottomMargin = 0;
worksheet.PageSetup.PrintGridlines = true;
worksheet.IsGridlinesVisible = true;

//add this line to set grid line color
worksheet.GridlineColor = Color.Red;

ImageOrPrintOptions imgOptions = new ImageOrPrintOptions
{
    OnePagePerSheet = true,
    ImageType = ImageType.Jpeg,
    HorizontalResolution = 400,
    VerticalResolution = 400,
    PrintingPage = PrintingPageType.IgnoreBlank,
    GridlineType = GridlineType.Hair
};

SheetRender sr = new SheetRender(worksheet, imgOptions);
sr.ToImage(0, filePath + "out_net.png");

Hope helps a bit.

Hi, I am getting weird gridlines for some reason using this code-

worksheet.AutoFitRows();
worksheet.PageSetup.PrintArea = printArea;
worksheet.PageSetup.LeftMargin = 0;
worksheet.PageSetup.RightMargin = 0;
worksheet.PageSetup.TopMargin = 0;
worksheet.PageSetup.BottomMargin = 0;
worksheet.PageSetup.PrintGridlines = true;
worksheet.GridlineColor = System.Drawing.Color.Gray;
worksheet.IsGridlinesVisible = true;

ImageOrPrintOptions originalImgOptions = new ImageOrPrintOptions
{
TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias,
OnePagePerSheet = true,
ImageType = ImageType.OfficeCompatibleEmf,
};

the Image of the range from excel is the following-
image.png (7.8 KB)

and with gridlines it should look like this-
image.png (3.5 KB)

but what I end up with is this
image.png (13.8 KB)

gridline is dotted, the borders are also the same color as gridline?

@Adhirath
Please refer to the following example code to change the grid line type.

ImageOrPrintOptions originalImgOptions = new ImageOrPrintOptions
{
    TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias,
    OnePagePerSheet = true,
    ImageType = ImageType.OfficeCompatibleEmf,
    GridlineType = GridlineType.Hair
};

Hope helps a bit.

Same issue with the following code too-

            worksheet.AutoFitRows();
            worksheet.PageSetup.PrintArea = printArea;
            worksheet.PageSetup.LeftMargin = 0;
            worksheet.PageSetup.RightMargin = 0;
            worksheet.PageSetup.TopMargin = 0;
            worksheet.PageSetup.BottomMargin = 0;
            worksheet.PageSetup.PrintGridlines = true;
            worksheet.GridlineColor = System.Drawing.Color.Gray; 
            worksheet.IsGridlinesVisible = true;
            
            ImageOrPrintOptions imgOptions = new ImageOrPrintOptions
            {
               OnePagePerSheet = true,
               ImageType = ImageType.Png,
               HorizontalResolution = 400,
               VerticalResolution = 400,
               PrintingPage = PrintingPageType.IgnoreBlank,
               GridlineType = GridlineType.Hair
             };
             SheetRender sr = new SheetRender(worksheet, imgOptions);

             using (MemoryStream ms = new MemoryStream())
             {
                sr.ToImage(0, ms);
              }

@Adhirath
Would you like to provide your sample file and complete runnable test code? We will check it soon.

I have provided the entire code for generating the image above, the table is present in this file-
tableexcel.zip (12.2 KB), this did not use to happen with 24.7.0 version started happening when i moved to 25.4.0

@Adhirath ,

We can see the color of border which is automatic color is changed to be same color as grildline after setting worksheet.GridlineColor = System.Drawing.Color.Gray; . In Excel, after changing the color of gridline, the color of border set in cell is also changed.

The gridline is solid after setting GridlineType = GridlineType.Hair, which is OK.

Do you want only the color of gridline is changed to the set color, while keeping the color of border set in Cells as it is?

yes I would like that