Script on the button. I'd recommend breaking this out into a project script or two for use in production.
from java.awt.image import BufferedImage
from org.apache.poi.util import IOUtils
from javax.imageio import ImageIO
from org.apache.poi.ss.usermodel import Cell
from org.apache.poi.ss.usermodel import Row
from org.apache.poi.ss.usermodel import Sheet
from org.apache.poi.ss.usermodel import Workbook
from org.apache.poi.xssf.usermodel import XSSFWorkbook
from org.apache.poi.xssf.usermodel import XSSFDataFormat
from org.apache.poi.xssf.usermodel import XSSFClientAnchor
from org.apache.poi.ss.usermodel.ClientAnchor import AnchorType
from java.io import FileOutputStream, ByteArrayOutputStream
component = event.source.parent.getComponent('Easy Chart')
# Create a captrue of the component and get its byte array
image = BufferedImage(component.getWidth(),
component.getHeight(),
BufferedImage.TYPE_INT_RGB
)
component.paint(image.getGraphics())
baos = ByteArrayOutputStream()
ImageIO.write(image, 'png', baos)
imageBytes = baos.toByteArray()
# Create workbook
wb = XSSFWorkbook()
# Create Sheet
sheet = wb.createSheet('Easy Chart')
# Add the image to the workbook
imageId = wb.addPicture(imageBytes, Workbook.PICTURE_TYPE_PNG)
# Create the drawing patriarch. We need this to draw the image on the sheet.
drawing = sheet.createDrawingPatriarch()
# Create an anchor. The anchor is where the image will go.
anchor = XSSFClientAnchor()
anchor.setAnchorType(AnchorType.DONT_MOVE_AND_RESIZE)
# set anchor location to B2
anchor.setCol1(1)
anchor.setRow1(1)
# Draw the image and return the picture object
picture = drawing.createPicture(anchor, imageId)
# Put the picture back to it's orignal size
picture.resize()
# Write the excel file
output = FileOutputStream('c:/test/easyChart.xlsx')
wb.write(output)
output.close()