我的代码中 Drawing drawing = sheet.createDrawingPatriarch();没有drawing.getImageDimension()方法
时间: 2023-12-10 20:38:08 浏览: 53
如果你使用的是 Apache POI 3.17或更早的版本,那么确实没有`getImageDimension()`方法。不过,Apache POI 3.18及以上版本已经添加了这个方法,你可以升级 Apache POI 到最新版本来使用它。
如果你无法升级 Apache POI,也可以使用以下代码获取图像的长和宽:
```java
Picture picture = drawing.createPicture(anchor, pictureIndex);
int width = Units.toPixel(picture.getImageDimension().getWidth());
int height = Units.toPixel(picture.getImageDimension().getHeight());
```
这里的 `Units.toPixel()` 方法将 EMU(English Metric Units)转换为像素值。
相关问题
我的代码中 Drawing drawing = sheet.createDrawingPatriarch();没有drawing.getImageDimension()和drawing.draw(image.createGraphics());方法
如果你想在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库。如果你使用的是其他库,可能会有些许差异。
rivate static void export(HttpServletResponse response, File file, String fileName, if (response != null) { // 前端导出 try { write(response, book, fileName); } catch (IOException e) { e.printStackTrace(); } } else { // 本地导出 FileOutputStream fos; try { fos = new FileOutputStream(file); ByteArrayOutputStream ops = new ByteArrayOutputStream(); book.write(ops); fos.write(ops.toByteArray()); fos.close(); } catch (Exception e) { e.printStackTrace(); } } Map<String, List<List<Object>>> sheetMap, Map<Integer, List<String>> selectMap) { // 整个 Excel 表格 book 对象 SXSSFWorkbook book = new SXSSFWorkbook(); // 每个 Sheet 页 Set<Entry<String, List<List<Object>>>> entries = sheetMap.entrySet(); for (Entry<String, List<List<Object>>> entry : entries) { List<List<Object>> sheetDataList = entry.getValue(); Sheet sheet = book.createSheet(entry.getKey()); Drawing<?> patriarch = sheet.createDrawingPatriarch();
这段代码是一个Excel导出的实现,其中包含了两种导出方式:前端导出和本地导出。具体实现如下:
1. 首先创建一个SXSSFWorkbook对象,表示整个Excel表格。
2. 遍历sheetMap中的每个Sheet页,创建对应的Sheet对象,并将Sheet页的名称设置为entry.getKey()。
3. 对于每个Sheet页,遍历其对应的数据列表sheetDataList,创建对应的行和单元格,并将数据填充到单元格中。
4. 对于每个Sheet页,如果有需要下拉选择的单元格,将其加入到数据验证中。
5. 如果response不为空,则表示前端导出,将book写入response流中。否则,表示本地导出,将book写入文件中。
下面是代码的具体实现:
```java
private static void export(HttpServletResponse response, File file, String fileName,
Map<String, List<List<Object>>> sheetMap, Map<Integer, List<String>> selectMap) {
// 整个 Excel 表格 book 对象
SXSSFWorkbook book = new SXSSFWorkbook();
// 每个 Sheet 页
Set<Entry<String, List<List<Object>>>> entries = sheetMap.entrySet();
for (Entry<String, List<List<Object>>> entry : entries) {
List<List<Object>> sheetDataList = entry.getValue();
Sheet sheet = book.createSheet(entry.getKey());
Drawing<?> patriarch = sheet.createDrawingPatriarch();
// 遍历每一行数据
for (int i = 0; i < sheetDataList.size(); i++) {
Row row = sheet.createRow(i);
List<Object> rowDataList = sheetDataList.get(i);
// 遍历每个单元格
for (int j = 0; j < rowDataList.size(); j++) {
Cell cell = row.createCell(j);
Object cellData = rowDataList.get(j);
// 判断单元格数据类型,并设置单元格格式
if (cellData instanceof String) {
cell.setCellValue((String) cellData);
} else if (cellData instanceof Number) {
cell.setCellValue(((Number) cellData).doubleValue());
} else if (cellData instanceof Date) {
cell.setCellValue((Date) cellData);
CellStyle style = book.createCellStyle();
style.setDataFormat(book.getCreationHelper().createDataFormat().getFormat("yyyy-MM-dd HH:mm:ss"));
cell.setCellStyle(style);
} else if (cellData instanceof Calendar) {
cell.setCellValue((Calendar) cellData);
CellStyle style = book.createCellStyle();
style.setDataFormat(book.getCreationHelper().createDataFormat().getFormat("yyyy-MM-dd HH:mm:ss"));
cell.setCellStyle(style);
} else if (cellData instanceof Boolean) {
cell.setCellValue((Boolean) cellData);
} else if (cellData == null) {
cell.setCellValue("");
}
// 设置下拉选择
if (selectMap.containsKey(j)) {
List<String> selectList = selectMap.get(j);
DataValidationHelper helper = sheet.getDataValidationHelper();
DataValidationConstraint constraint = helper.createExplicitListConstraint(selectList.toArray(new String[0]));
CellRangeAddressList addressList = new CellRangeAddressList(cell.getRowIndex(), cell.getRowIndex(), cell.getColumnIndex(), cell.getColumnIndex());
DataValidation validation = helper.createValidation(constraint, addressList);
sheet.addValidationData(validation);
}
}
}
}
// 导出Excel
if (response != null) {
// 前端导出
try {
write(response, book, fileName);
} catch (IOException e) {
e.printStackTrace();
}
} else {
// 本地导出
FileOutputStream fos;
try {
fos = new FileOutputStream(file);
ByteArrayOutputStream ops = new ByteArrayOutputStream();
book.write(ops);
fos.write(ops.toByteArray());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
其中,write()方法用于将book写入response流中,实现前端导出。具体实现如下:
```java
private static void write(HttpServletResponse response, SXSSFWorkbook book, String fileName) throws IOException {
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("utf-8"), "ISO8859-1"));
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
OutputStream os = response.getOutputStream();
book.write(os);
os.flush();
os.close();
}
```
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)