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(); }
結果如下: