Convert range of workbook cells to image using Aspose.Cells for .NET in C#

Hi,

the size of my image file is 0. I don’t get what I make wrong.

Here is my code:
xlsLic.SetLicense(“Aspose.Total.lic”);

        string fileName = @"image.xlsx";    
        Workbook workbook = new Workbook(fileName);

        Worksheet worksheet = workbook.Worksheets[0];

        ImageOrPrintOptions options = new ImageOrPrintOptions();
        //options.AllColumnsInOnePagePerSheet = true;
        options.ImageFormat = ImageFormat.Png;
        //options.OnePagePerSheet = true;
        //options.Quality = 100;
        
        Aspose.Cells.PageSetup pageSetup = worksheet.PageSetup;
        //pageSetup.IsPercentScale = false;
        //pageSetup.SetFitToPages(1, 1);
        pageSetup.PrintArea = "A1:B2"; // “A1:H161”;

        SheetRender sr = new SheetRender(worksheet, options);
        sr.ToImage(1, "output.png");

image.zip (223.0 KB)

Kind regards,

@christoph_schroeder01_sap_com,

Thanks for the template file and sample code.

I have evaluated your scenario/ case using your template file and sample code. Well, there is an error in your code. There is a hidden sheet in your template file and you are actually referring to that sheet (at zero indexed position). Please change the line of code from your code segment:
i.e.,

Worksheet worksheet = workbook.Worksheets[0];

to:

Worksheet worksheet = workbook.Worksheets[“share”];

it will work fine for your desired printable areas now.

Let us know if you still find the issue.

Hi,

you are right. But after the change the issue still exists. Image file is empty.

Regards,

@christoph_schroeder01_sap_com,

Paste your final/updated code segment here, we will check it soon.

I tested using the following sample code and it works fine. The output image is fine tuned:
e.g
Sample code:

string fileName = "e:\\test2\\image.xlsx";    
            Workbook workbook = new Workbook(fileName);

            Worksheet worksheet = workbook.Worksheets["share"];

            ImageOrPrintOptions options = new ImageOrPrintOptions();
            //options.AllColumnsInOnePagePerSheet = true;
            options.ImageFormat = ImageFormat.Png;
         
            Aspose.Cells.PageSetup pageSetup = worksheet.PageSetup;
            pageSetup.PrintArea = "A1:H161"; // “A1:H161”;

            SheetRender sr = new SheetRender(worksheet, options);
            sr.ToImage(0, "e:\\test2\\out1.png");

string fileName = @“image.xlsx”;
Workbook workbook = new Workbook(fileName);

        Worksheet worksheet = workbook.Worksheets["share"];

        ImageOrPrintOptions options = new ImageOrPrintOptions();
        //options.AllColumnsInOnePagePerSheet = true;
        options.ImageFormat = ImageFormat.Png;
        //options.OnePagePerSheet = true;
        //options.Quality = 100;

        Aspose.Cells.PageSetup pageSetup = worksheet.PageSetup;
        //pageSetup.IsPercentScale = false;
        //pageSetup.SetFitToPages(1, 1);
        pageSetup.PrintArea = "A1:B2"; // “A1:H161”;

        SheetRender sr = new SheetRender(worksheet, options);
        sr.ToImage(1, "output.png");

Aspose.Cells.Version 17.11.0.0

sorry, I got it. sr.ToImage(0, “output.png”);

@christoph_schroeder01_sap_com,

I am using latest version/fix: Aspose.Cells for .NET v18.6.1:

Please try it and it should work fine on your end.

Let us know if you still find the issue with v18.6.x.

Good to know that you have sorted it out. And, yes, you should correct your code as you have done now.
Feel free to contact us any time if you need further help or have some other issue or queries, we will be happy to assist you soon.

Do you know what how to get the best quality image from cells ?

Kind regards,

@christoph_schroeder01_sap_com,

If you are using EMF image file format type, then it will render image with enhanced quality because EMF is vector graphic type. If you need to use PNG as image format, you may try to set a larger horizontal/vertical resolutions in ImageOrPrintOptions. See the following sample code to get PNG image with better quality:
e.g
Sample code:

 string fileName = "e:\\test2\\image.xlsx";    
            Workbook workbook = new Workbook(fileName);

            Worksheet worksheet = workbook.Worksheets["share"];

            ImageOrPrintOptions options = new ImageOrPrintOptions();
            options.ImageType = ImageType.Png;
            options.HorizontalResolution = 1200;
            options.VerticalResolution = 1000;
           
            Aspose.Cells.PageSetup pageSetup = worksheet.PageSetup;
            pageSetup.PrintArea = "A1:H161"; // “A1:H161”;

            SheetRender sr = new SheetRender(worksheet, options);
            sr.ToImage(0, "e:\\test2\\out1.png");

Hope, this helps a bit.