Generate a Thumbnail Image of a Worksheet

Skip to end of metadata
Go to start of metadata
It can be useful to generate thumbnails from worksheets. A thumbnail is a small image that can be pasted into a Word document or a PowerPoint presentation to give a preview of what's on the worksheet. It can be added to a webpage with a link to download the original document and has a host of other uses. Aspose.Cells' APIs allow you to output worksheets to image files so making a thumbnail is easy.

The sample code below show you how, step by step.

Example:
[Java]
//........
import com.aspose.cells.*;
import java.io.*;
import java.awt.image.*;
import java.awt.*;
import javax.imageio.*;


public class Thumbnail1
{
    public static void main(String[] args) throws Exception
    {
       
//Instantiate and open an Excel file
Workbook book = new Workbook("e:\\test\\book1.xls");
 
//Define ImageOrPrintOptions
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
//Set the vertical and horizontal resolution
imgOptions.setVerticalResolution(200);
imgOptions.setHorizontalResolution(200);
//Set the image's format
imgOptions.setImageFormat(ImageFormat.getJpeg());
//One page per sheet is enabled
imgOptions.setOnePagePerSheet(true);
 
//Get the first worksheet
Worksheet sheet = book.getWorksheets().get(0);
//Render the sheet with respect to specified image/print options
SheetRender sr = new SheetRender(sheet, imgOptions);
//Render the image for the sheet
sr.toImage(0, "mythumb.jpg");

//Creating Thumbnail
Image img = ImageIO.read(new File("mythumb.jpg")).getScaledInstance(100, 100, BufferedImage.SCALE_SMOOTH);
BufferedImage img1 = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
img1.createGraphics().drawImage(ImageIO.read(new File("mythumb.jpg")).getScaledInstance(100, 100, Image.SCALE_SMOOTH),0,0,null);
ImageIO.write(img1, "jpg", new File("thumbnail_out.jpg"));

    }
}
 
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.