Java可以使用Apache POI库来把水印图片添加到excel中。首先,你需要使用Apache POI库将excel文件载入,然后创建工作表,读取水印图片,在excel单元格中添加该图片,最后将excel文件保存。代码
时间: 2023-03-15 08:02:21 浏览: 435
可以使用以下代码来实现:InputStream inp = new FileInputStream("workbook.xls");//Get the workbook instance for XLS file
HSSFWorkbook wb = new HSSFWorkbook(inp);//Get first sheet from the workbook
HSSFSheet sheet = wb.getSheetAt(0);//Get the input stream of the watermark image
InputStream imgStream = new FileInputStream("watermark.png");//Create the byte array of the watermark image
byte[] bytes = IOUtils.toByteArray(imgStream);//Create the expression for the watermark image
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 1023, 255,(short) 0, 0, (short) 5, 10);//Create the picture
int picIndex = wb.addPicture(bytes, HSSFWorkbook.PICTURE_TYPE_PNG);//Create the drawing
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();//Create the picture
HSSFPicture picture = patriarch.createPicture(anchor, picIndex);//Write the Excel file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)