我的代码中 Drawing drawing = sheet.createDrawingPatriarch();没有drawing.getImageDimension()和drawing.draw(image.createGraphics());方法
时间: 2023-12-10 16:38:07 浏览: 67
如果你想在Excel中插入图片并获取其尺寸,你可以使用以下代码:
```java
// 创建绘图对象
Drawing drawing = sheet.createDrawingPatriarch();
// 读取图片文件
InputStream inputStream = new FileInputStream("image.jpg");
byte[] bytes = IOUtils.toByteArray(inputStream);
// 向绘图对象中添加图片
int pictureIndex = workbook.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);
ClientAnchor anchor = new XSSFClientAnchor();
anchor.setCol1(0);
anchor.setRow1(0);
Picture picture = drawing.createPicture(anchor, pictureIndex);
// 获取图片尺寸
int imageWidth = picture.getImageDimension().width;
int imageHeight = picture.getImageDimension().height;
```
如果你只需要在Excel中插入图片,而不需要获取其尺寸,你可以使用以下代码:
```java
// 创建绘图对象
Drawing drawing = sheet.createDrawingPatriarch();
// 读取图片文件
InputStream inputStream = new FileInputStream("image.jpg");
byte[] bytes = IOUtils.toByteArray(inputStream);
// 向绘图对象中添加图片
int pictureIndex = workbook.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);
ClientAnchor anchor = new XSSFClientAnchor();
anchor.setCol1(0);
anchor.setRow1(0);
drawing.createPicture(anchor, pictureIndex);
```
请注意,这里使用的是Apache POI库。如果你使用的是其他库,可能会有些许差异。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)