Rendering worksheet to image

Hello Aspose,

I would like to create a preview image from an excel file using aspose.cells.Rendering.SheetRender, meaning I would render only a part of the file into the image.

It seems the SheetRrender uses the sheet’s printing page breaks (Workseet.GetPrintingPageBreaks()) to render the images, which, unfortunalty i was unable to modify as desired.


Regards.
C.


Hi,


Well, if you need to partially (only a part of it) render your worksheet to generate image, you may first specify the PageSetup’s printable area or the worksheet and then render image(s) of the sheet accordingly. See the sample code below:
e.g
Sample code:

Aspose.Cells.Workbook wb = new Aspose.Cells.Workbook(@“e:\test2\Book1.xlsx”);

Aspose.Cells.Rendering.ImageOrPrintOptions imgOptions = new Aspose.Cells.Rendering.ImageOrPrintOptions();
imgOptions.ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg;
Aspose.Cells.Worksheet sheet = wb.Worksheets[0];

sheet.PageSetup.PrintArea = “D10:J25”;
Aspose.Cells.Rendering.SheetRender sr = new Aspose.Cells.Rendering.SheetRender(sheet, imgOptions);

System.Drawing.Bitmap bitmap = sr.ToImage(0);
bitmap.Save(@“e:\test2\out1Sample1_new1.png”, System.Drawing.Imaging.ImageFormat.Png);

Hope, this helps a bit.

Thank you.

Hi,

I have tried what you suggested.
I’m attaching my code, the excel file and the image output.

My Code:

Worksheet sheet = document.Worksheets[(page - 1)];

ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
imgOptions.ImageFormat = System.Drawing.Imaging.ImageFormat.Png;
// sheet.PageSetup.PrintArea = GetSheetPrintArea(sheet.Cells.MaxColumn, sheet.Cells.MaxRow);
sheet.PageSetup.PrintArea = “A1:U151”;
SheetRender renderer = new SheetRender(sheet, imgOptions);
if (renderer.PageCount != 0)
{
Bitmap outputImage = renderer.ToImage(0);
outputImage.Save(output + “test”);
}

Hi,

Thanks for your posting and using Aspose.Cells.

You are actually taking the image of entire worksheet by setting the print area. You can achieve the same thing by setting the ImageOrPrintOptions.OnePagePerSheet property to true. This way, you will get a single image which will cover entire worksheet.

Please see the following code which is same as yours except it is using ImageOrPrintOptions.OnePagePerSheet property now. Now you don’t need to set print area to get the desired results. I have attached the output image for your reference.

C#


Workbook document = new Workbook(“exceldummy.xlsx”);


Worksheet sheet = document.Worksheets[“Blad4”];


ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();

imgOptions.ImageFormat = System.Drawing.Imaging.ImageFormat.Png;

imgOptions.OnePagePerSheet = true;


SheetRender renderer = new SheetRender(sheet, imgOptions);

if (renderer.PageCount != 0)

{

Size s = renderer.GetPageSize(0);

renderer.ToImage(0, “test.png”);

}

Hi,


Thanks for the template file.

What’s wrong with it.?For your information, when you set the printable area i.e., “A1:U151” for any worksheet, it will render approx. 12 or more pages in total, you may confirm this by opening your template file into MS Excel 2007/2010 and specify the printable area (click Sheet tab in Page Setup dialog box and set the Print area in the field there and then click OK) manually, now take the print preview of the worksheet and you will see that there are 12 pages or more. Aspose.Cells does render pages based on what is shown in the print preview for the sheets. Checking your sample code, you are rendering only the first page (using e.g Bitmap outputImage = renderer.ToImage(0); --> zero means first page (0 indexed position)) out of 12 more pages, you may match the output image with the first page by using print preview of the sheet in Ms Excel manually. If want to take single image for the whole 12 pages, I am afraid, this would not work (although you may try to add a line of code: imgOptions.OnePagePerSheet = true; to your code segment) as there is long list of data in rows/cols to be rendered for the range “A1:U151”, so if you are even successful in MS Excel (using Fit to Pages Tall and Wide to 1) taking one whole page for the area, the font used by the data is minimized to that extent and you cannot read it at all when printing or taking the preview of it. And, if you need to render multiple pages, you may simply update your code segment a bit accordingly:
e.g
Sample code:

SheetRender sr = new SheetRender(sheet, imgOptions);

for (int j = 0; j < sr.PageCount; j++)
{
sr.ToImage(j, “e:\test2\outImage_” + j + “.png”);
}

Hope, this helps a bit.

Thank you.

Thanks for both replies.
the OnePagePerSheet solved my issue.

Hi,

Thanks for your feedback and using Aspose.Cells.

It is good to know that your issue is resolved with one page per sheet option. Let us know if you encounter any other issue, we will be glad to look into it and help you further.