Skip to content

Commit

Permalink
修改图片
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuangjiaju committed Apr 11, 2024
1 parent 7929e36 commit af7a906
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public XlsxSaxAnalyser(XlsxReadContext xlsxReadContext, InputStream decryptedStr
// Reading images
if (xlsxReadContext.readWorkbookHolder().getExtraReadSet().contains(CellExtraTypeEnum.IMAGE)) {
List<PackagePart> drawingsPackagePartList = pkg.getPartsByName(
Pattern.compile("xl/drawings/drawing[0-9]+.xml"));
Pattern.compile("/xl/drawings/drawing[0-9]+.xml"));
if (CollectionUtils.isNotEmpty(drawingsPackagePartList)) {
for (PackagePart drawingPackagePart : drawingsPackagePartList) {
log.info("xx{}", drawingPackagePart);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
import com.alibaba.easyexcel.test.demo.write.DemoData;
import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.enums.CellExtraTypeEnum;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.util.PositionUtils;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
Expand Down Expand Up @@ -52,9 +54,9 @@ public void test() throws Exception {
File file = new File("/Users/zhuangjiaju/IdeaProjects/easyexcel/src/test/resources/converter/converter07.xlsx");

List<Object> list = EasyExcel.read(
"/Users/zhuangjiaju/IdeaProjects/easyexcel/easyexcel-test/target/test-classes"
+ "/simpleWrite1674051907397.xlsx")
"/Users/zhuangjiaju/测试数据/imagetest.xlsx")
//.useDefaultListener(false)
.extraRead(CellExtraTypeEnum.IMAGE)
.sheet(0)
.headRowNumber(0).doReadSync();
LOGGER.info("数据:{}", list.size());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.alibaba.easyexcel.test.temp.poi;

import java.io.FileInputStream;

import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFPicture;
import org.apache.poi.hssf.usermodel.HSSFPictureData;
import org.apache.poi.hssf.usermodel.HSSFShape;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFPicture;
import org.apache.poi.xssf.usermodel.XSSFPictureData;
import org.apache.poi.xssf.usermodel.XSSFShape;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.jupiter.api.Test;

/**
* 测试poi
*
* @author Jiaju Zhuang
**/
@Slf4j
public class PoiImageTest {

@Test
public void xls() throws Exception {

FileInputStream fis = new FileInputStream("/Users/zhuangjiaju/测试数据/imagetest.xls");
HSSFWorkbook workbook = new HSSFWorkbook(fis);
HSSFSheet sheet = workbook.getSheetAt(0);
HSSFPatriarch patriarch = sheet.getDrawingPatriarch();

for (HSSFShape shape : patriarch.getChildren()) {
if (shape instanceof HSSFPicture) {
HSSFPicture picture = (HSSFPicture)shape;
HSSFPictureData pictureData = picture.getPictureData();
byte[] data = pictureData.getData();

log.info("图片:{}", data.length);
}
}

workbook.close();
fis.close();
}

@Test
public void xlsx() throws Exception {
FileInputStream fis = new FileInputStream("/Users/zhuangjiaju/测试数据/imagetest.xlsx");
XSSFWorkbook workbook = new XSSFWorkbook(fis);
XSSFSheet sheet = workbook.getSheetAt(0);
XSSFDrawing drawing = sheet.getDrawingPatriarch();

for (XSSFShape shape : drawing.getShapes()) {
if (shape instanceof XSSFPicture) {
XSSFPicture picture = (XSSFPicture)shape;
XSSFPictureData pictureData = picture.getPictureData();
byte[] data = pictureData.getData();
log.info("图片:{}", data.length);
log.info("图片:{}", pictureData.getPictureType());
log.info("图片:{}", pictureData.getMimeType());
log.info("图片:{}", pictureData.suggestFileExtension());
//log.info("图片:{}", pictureData.suggestFileExtension());

}
}

workbook.close();
fis.close();
}

}

0 comments on commit af7a906

Please sign in to comment.