Print gridlines in the output image of the Excel worksheet using SheetRender.ToImage method in .NET

Hi team,


We are using Aspose cell to generate image(snapshot) of excel sheet.

below is code which generates image of an excel sheet provide (by default first sheet’s snapshot is taken)

public MemoryStream ExtractActiveCellAsImage(byte[] inputExcelData, int activeWorkSheet)
{
MemoryStream inputStream = new MemoryStream(inputExcelData);
inputStream.Position = 0;
Workbook book = new Workbook(inputStream);
Aspose.Cells.Worksheet sheet = book.Worksheets[0];

//Set left, top, right, bottom margins to 0
sheet.PageSetup.LeftMargin = 0;
sheet.PageSetup.RightMargin = 0;
sheet.PageSetup.TopMargin = 0;
sheet.PageSetup.BottomMargin = 0;

ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
imgOptions.ImageFormat = ImageFormat.Png;
imgOptions.OnePagePerSheet = true;
imgOptions.PrintingPage = PrintingPageType.IgnoreBlank;

MemoryStream imageStream = new MemoryStream();
imageStream.Position = 0;
SheetRender sr = new SheetRender(sheet, imgOptions);
sr.ToImage(activeWorkSheet, imageStream);
return imageStream;
}

But the grid lines of excel sheets are not displayed in snap shot.

See attached sample for the details.
Image.png is generated image by above code

And the file expected.docx contains desire image output. (i.e excel sheet image Generated by MS word)

Please help us in this issue.

Thanks and regards,
Nakul

Hi,


If you need to print gridlines of the cells, you may set PrintGridLines to true, see your code segment with an addition of one line only. i.e…,
public MemoryStream ExtractActiveCellAsImage(byte[] inputExcelData, int activeWorkSheet)
{
MemoryStream inputStream = new MemoryStream(inputExcelData);
inputStream.Position = 0;
Workbook book = new Workbook(inputStream);
Aspose.Cells.Worksheet sheet = book.Worksheets[0];

//Set left, top, right, bottom margins to 0
sheet.PageSetup.LeftMargin = 0;
sheet.PageSetup.RightMargin = 0;
sheet.PageSetup.TopMargin = 0;
sheet.PageSetup.BottomMargin = 0;
sheet.PageSetup.PrintGridlines = true;
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
imgOptions.ImageFormat = ImageFormat.Png;
imgOptions.OnePagePerSheet = true;
imgOptions.PrintingPage = PrintingPageType.IgnoreBlank;

MemoryStream imageStream = new MemoryStream();
imageStream.Position = 0;
SheetRender sr = new SheetRender(sheet, imgOptions);
sr.ToImage(activeWorkSheet, imageStream);
return imageStream;
}


If you could open your template file and take view in MS Excel’s print preview, you will also find that the gridlines are not shown so it does for Sheet-to-Image feature.

Also, you may add borders for your cells, if you don’t on the print gridlines, see the document for reference:


Thank you.