Excel images

Following example shows how to add images in different formats to ExcelWorksheet using various types of positioning.

Excel images added with GemBox.Spreadsheet for Java
Screenshot of Excel images added with GemBox.Spreadsheet for Java
import com.gembox.spreadsheet.*;

class Program {

    public static void main(String[] args) throws java.io.IOException {
        // If using the Professional version, put your serial key below.
        SpreadsheetInfo.setLicense("FREE-LIMITED-KEY");

        ExcelFile workbook = new ExcelFile();
        ExcelWorksheet worksheet = workbook.addWorksheet("Images");

        worksheet.getCell(0, 0).setValue("Image examples:");

        // Small BMP added by using rectangle.
        worksheet.getPictures().add("%#SmallImage.bmp%", 50, 50, 48, 48, LengthUnit.PIXEL);

        // Large JPG added by using one anchor.
        worksheet.getPictures().add("%#FragonardReader.jpg%", "B9");

        // PNG added by using two anchors.
        worksheet.getPictures().add("%#Dices.png%", "J16", "K20");

        // GIF added by using anchors. Notice that animation is lost in MS Excel.
        worksheet.getPictures().add("%#Zahnrad.gif%",
                new AnchorCell(worksheet.getColumn(9), worksheet.getRow(21), 100000, 100000),
                new AnchorCell(worksheet.getColumn(10), worksheet.getRow(23), 50000, 50000)).getPosition().setMode(PositioningMode.MOVE);

        // WMF added by using one anchor and size.
        worksheet.getPictures().add("%#Graphics1.wmf%", "J9", 250, 100, LengthUnit.PIXEL).getPosition().setMode(PositioningMode.FREE_FLOATING);

        workbook.save("Images.%OutputFileType%");
    }
}

See also


Next steps

GemBox.Spreadsheet for Java is a component that enables you to read, write, edit and convert spreadsheet files from your applications using one simple API.

Download Buy