1、在Excel中绘制图片
public static void main(String[] args) throws Exception { // 定义一个工作簿 Workbook workbook = new XSSFWorkbook(); // 创建一个sheet页 Sheet sheet = workbook.createSheet("第一个sheet页"); FileInputStream fis = new FileInputStream("E:\\桌面壁纸\\yoona头像.jpg"); // 向Excel中添加一张图片,并返回改图片在Excel中的图片集中的索引 int pictureIndex = ((XSSFWorkbook) workbook).addPicture(fis, Workbook.PICTURE_TYPE_JPEG); // 获取工作簿的绘图工具类 CreationHelper creationHelper = workbook.getCreationHelper(); // 创建一个绘图对象 Drawing<?> drawingPatriarch = sheet.createDrawingPatriarch(); // 创建锚点,设置图片坐标 ClientAnchor clientAnchor = creationHelper.createClientAnchor(); clientAnchor.setRow1(0); clientAnchor.setCol1(0); // 创建图片 Picture picture = drawingPatriarch.createPicture(clientAnchor, pictureIndex); picture.resize(); FileOutputStream fileOutputStream = new FileOutputStream("D:\\测试Excel中绘制图片.xlsx"); workbook.write(fileOutputStream); fileOutputStream.close(); }
结果如下:
原文地址:https://www.cnblogs.com/lkc9/p/12262776.html
时间: 2024-10-05 02:59:55