Independentsoft
- any library, any programming language
Home
Purchase
Support
Company
Contact
JSpreadsheet
>
Tutorial
> Insert Image
The following example shows you how to insert an image.
import com.independentsoft.office.Unit; import com.independentsoft.office.UnitType; import com.independentsoft.office.drawing.Extents; import com.independentsoft.office.drawing.FillRectangle; import com.independentsoft.office.drawing.Offset; import com.independentsoft.office.drawing.PictureLocking; import com.independentsoft.office.drawing.PresetGeometry; import com.independentsoft.office.drawing.ShapeType; import com.independentsoft.office.drawing.Stretch; import com.independentsoft.office.drawing.Transform2D; import com.independentsoft.office.spreadsheet.Cell; import com.independentsoft.office.spreadsheet.Workbook; import com.independentsoft.office.spreadsheet.Worksheet; import com.independentsoft.office.spreadsheet.drawing.ClientData; import com.independentsoft.office.spreadsheet.drawing.DrawingObjects; import com.independentsoft.office.spreadsheet.drawing.EditAs; import com.independentsoft.office.spreadsheet.drawing.EndAnchorPoint; import com.independentsoft.office.spreadsheet.drawing.Picture; import com.independentsoft.office.spreadsheet.drawing.StartAnchorPoint; import com.independentsoft.office.spreadsheet.drawing.TwoCellAnchor; public class Example { public static void main(String[] args) { try { Workbook book = new Workbook(); Worksheet sheet1 = new Worksheet(); sheet1.set("A1", new Cell("Value1")); sheet1.set("A2", new Cell("Value2")); sheet1.set("A3", new Cell("Value3")); sheet1.set("A4", new Cell("Value4")); sheet1.set("A5", new Cell("Value5")); TwoCellAnchor anchor = new TwoCellAnchor(); anchor.setEditAs(EditAs.ONE_CELL); StartAnchorPoint start = new StartAnchorPoint(); start.setColumn(2); start.setColumnOffset(new Unit(0, UnitType.ENGLISH_METRIC_UNIT)); start.setRow(2); start.setRowOffset(new Unit(0, UnitType.ENGLISH_METRIC_UNIT)); EndAnchorPoint end = new EndAnchorPoint(); end.setColumn(12); end.setColumnOffset(new Unit(0, UnitType.ENGLISH_METRIC_UNIT)); end.setRow(26); end.setRowOffset(new Unit(0, UnitType.ENGLISH_METRIC_UNIT)); anchor.setStart(start); anchor.setEnd(end); Picture picture = new Picture("c:\\test\\image.jpg"); picture.setID("1"); picture.setName("Picture 1"); picture.setDescription("image.jpg"); PictureLocking locking = new PictureLocking(); locking.setDisallowAspectRatioChange(true); picture.setLocking(locking); Stretch stretch = new Stretch(); stretch.setFillRectangle(new FillRectangle()); picture.setStretch(stretch); Transform2D transform2D = new Transform2D(); transform2D.setOffset(new Offset(1219200, 381000)); transform2D.setExtents(new Extents(6096000, 4572000)); picture.getShapeProperties().setTransform2D(transform2D); picture.getShapeProperties().setPresetGeometry(new PresetGeometry(ShapeType.RECTANGLE)); anchor.setElement(picture); anchor.setClientData(new ClientData()); DrawingObjects drawingObjects = new DrawingObjects(); drawingObjects.getAnchors().add(anchor); sheet1.setDrawingObjects(drawingObjects); book.getSheets().add(sheet1); book.save("c:\\test\\output.xlsx", true); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } } }
Need help? Ask our developers:
Name*
Email*
Message*