Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

新增了读图片的特性 #1316

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add new feature:read image
  • Loading branch information
dota17 committed May 15, 2020
commit d0e1b4a62dc23e127d6e2243447e125758eb41b7
1 change: 1 addition & 0 deletions easyexcel
Submodule easyexcel added at 4076b8
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.3</version>
<version>2.2.2</version>
<packaging>jar</packaging>
<name>easyexcel</name>

Expand Down
369 changes: 366 additions & 3 deletions src/main/java/com/alibaba/excel/EasyExcelFactory.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class XlsxSaxAnalyser implements ExcelReadExecutor {
* excel comments key: sheetNo value: CommentsTable
*/
private Map<Integer, CommentsTable> commentsTableMap;
//解析excel的核心部分

public XlsxSaxAnalyser(XlsxReadContext xlsxReadContext, InputStream decryptedStream) throws Exception {
this.xlsxReadContext = xlsxReadContext;
// Initialize cache
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.alibaba.excel.analysis.v07.handlers;

import org.apache.poi.openxml4j.opc.PackagePart;
import org.xml.sax.Attributes;
import com.alibaba.excel.read.listener.ImageDataReadListener;

/**
* Abstract class for handling image related tags in a XML file
*
*/
public abstract class AbstractXlsxImageTagHandler implements XlsxImageTagHandler {
@Override
public void startElement(ImageDataReadListener imageDataListener, PackagePart packagePart, String name,
Attributes attributes) {

}

@Override
public void endElement(ImageDataReadListener imageDataListener, String name) {

}

@Override
public void characters(ImageDataReadListener imageDataListener, char[] ch, int start, int length) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.alibaba.excel.analysis.v07.handlers;

import com.alibaba.excel.constant.ExcelXmlConstants;
import com.alibaba.excel.read.listener.ImageDataReadListener;

import org.apache.poi.openxml4j.opc.PackagePart;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

/**
* Handle drawingX.xml file. The character 'X' represents a number. e.g. drawing1.xml
*/
public class DrawingXMLHandler extends DefaultHandler {
private ImageDataReadListener imageDataListener;
private static final Map<String, XlsxImageTagHandler> XLSX_IMAGE_HANDLER_MAP =
new HashMap<String, XlsxImageTagHandler>(32);
private LinkedList<String> tagDeque = new LinkedList<String>();
private PackagePart packagePart;

static {
ImageAnchorFromTagHandler imageAnchorFromTagHandler = new ImageAnchorFromTagHandler();
XLSX_IMAGE_HANDLER_MAP.put(ExcelXmlConstants.IMAGE_ANCHOR_FROM, imageAnchorFromTagHandler);

ImageAnchorToTagHandler imageAnchorToTagHandler = new ImageAnchorToTagHandler();
XLSX_IMAGE_HANDLER_MAP.put(ExcelXmlConstants.IMAGE_ANCHOR_TO, imageAnchorToTagHandler);

ImageAnchorColTagHandler imageAnchorColTagHandler = new ImageAnchorColTagHandler();
XLSX_IMAGE_HANDLER_MAP.put(ExcelXmlConstants.IMAGE_ANCHOR_COL, imageAnchorColTagHandler);

ImageAnchorColOffTagHandler imageAnchorColoffTagHandler = new ImageAnchorColOffTagHandler();
XLSX_IMAGE_HANDLER_MAP.put(ExcelXmlConstants.IMAGE_ANCHOR_COLOFF, imageAnchorColoffTagHandler);

ImageAnchorRowTagHandler imageAnchorRowTagHandler = new ImageAnchorRowTagHandler();
XLSX_IMAGE_HANDLER_MAP.put(ExcelXmlConstants.IMAGE_ANCHOR_ROW, imageAnchorRowTagHandler);

ImageAnchorRowOffHandler imageAnchorRowOffHandler = new ImageAnchorRowOffHandler();
XLSX_IMAGE_HANDLER_MAP.put(ExcelXmlConstants.IMAGE_ANCHOR_ROWOFF, imageAnchorRowOffHandler);

ImageRefIdTagHandler imageRefIdTagHandler = new ImageRefIdTagHandler();
XLSX_IMAGE_HANDLER_MAP.put(ExcelXmlConstants.IMAGE_REF_ID, imageRefIdTagHandler);

}

public DrawingXMLHandler(PackagePart packagePart, ImageDataReadListener imageDataListener) {
this.packagePart = packagePart;
this.imageDataListener = imageDataListener;
}

@Override
public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException {
XlsxImageTagHandler handler = XLSX_IMAGE_HANDLER_MAP.get(name);
if (handler == null) {
return;
}
handler.startElement(imageDataListener, packagePart, name, attributes);
tagDeque.push(name);
}

@Override
public void characters(char[] ch, int start, int length) throws SAXException {
String currentTag = tagDeque.peek();
if (currentTag == null) {
return;
}
XlsxImageTagHandler handler = XLSX_IMAGE_HANDLER_MAP.get(currentTag);
if (handler == null) {
return;
}
handler.characters(imageDataListener, ch, start, length);
}

@Override
public void endElement(String uri, String localName, String name) throws SAXException {
XlsxImageTagHandler handler = XLSX_IMAGE_HANDLER_MAP.get(name);
if (handler == null) {
return;
}
handler.endElement(imageDataListener, name);
tagDeque.pop();
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.alibaba.excel.analysis.v07.handlers;

import java.util.List;

import com.alibaba.excel.metadata.ImageData;
import com.alibaba.excel.read.listener.ImageDataReadListener;

/**
* Handle image anchor "xdr:colOff" tag
*/
public class ImageAnchorColOffTagHandler extends AbstractXlsxImageTagHandler {

@Override
public void characters(ImageDataReadListener imageDataListener, char[] ch, int start, int length) {
List<ImageData> imageDataList = imageDataListener.getImageDataList();
ImageData imageData = imageDataList.get(imageDataList.size() - 1);
StringBuilder colOff = new StringBuilder();
colOff.append(ch, start, length);

if (imageData.isAnchorFromTag()) {
imageData.getImageAnchor().setDx1(Integer.valueOf(colOff.toString()));
return;
}
imageData.getImageAnchor().setDx2(Integer.valueOf(colOff.toString()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.alibaba.excel.analysis.v07.handlers;

import java.util.List;
import com.alibaba.excel.metadata.ImageData;
import com.alibaba.excel.read.listener.ImageDataReadListener;

/**
* Handle image anchor "xdr:col" tag
*/
public class ImageAnchorColTagHandler extends AbstractXlsxImageTagHandler {

@Override
public void characters(ImageDataReadListener imageDataListener, char[] ch, int start, int length) {
List<ImageData> imageDataList = imageDataListener.getImageDataList();
ImageData imageData = imageDataList.get(imageDataList.size() - 1);
StringBuilder col = new StringBuilder();
col.append(ch, start, length);

if (imageData.isAnchorFromTag()) {
imageData.getImageAnchor().setCol1(Integer.valueOf(col.toString()));
return;
}
imageData.getImageAnchor().setCol2(Integer.valueOf(col.toString()));
}
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
package com.alibaba.excel.analysis.v07.handlers;

import com.alibaba.excel.context.xlsx.XlsxReadContext;
import org.xml.sax.Attributes;

public class ImageAnchorFromTagHandler extends XlsxTagHandler {

@Override
public boolean support(XlsxReadContext xlsxReadContext) {
return false;
}
import java.util.List;
import com.alibaba.excel.metadata.ImageData;
import com.alibaba.excel.read.listener.ImageDataReadListener;

@Override
public void startElement(XlsxReadContext xlsxReadContext, String name, Attributes attributes) {
import org.apache.poi.openxml4j.opc.PackagePart;
import org.xml.sax.Attributes;

}
/**
* Handle image anchor "xdr:from" tag
*/
public class ImageAnchorFromTagHandler extends AbstractXlsxImageTagHandler {

@Override
public void endElement(XlsxReadContext xlsxReadContext, String name) {

public void startElement(ImageDataReadListener imageDataListener, PackagePart packagePart, String name,
Attributes attributes) {
ImageData imageData = new ImageData();
imageData.setRelatedSheetName(imageDataListener.getRelatedSheetName());
imageData.setRelatedSheetNo(imageDataListener.getRelatedSheetNo());
imageData.setAnchorFromTag(true);
List<ImageData> imageDataList = imageDataListener.getImageDataList();
imageDataList.add(imageData);
}

@Override
public void characters(XlsxReadContext xlsxReadContext, char[] ch, int start, int length) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.alibaba.excel.analysis.v07.handlers;

import java.util.List;

import com.alibaba.excel.metadata.ImageData;
import com.alibaba.excel.read.listener.ImageDataReadListener;

/**
* Handle image anchor "xdr:rowOff" tag
*/
public class ImageAnchorRowOffHandler extends AbstractXlsxImageTagHandler {

@Override
public void characters(ImageDataReadListener imageDataListener, char[] ch, int start, int length) {
List<ImageData> imageDataList = imageDataListener.getImageDataList();
ImageData imageData = imageDataList.get(imageDataList.size() - 1);
StringBuilder rowOff = new StringBuilder();
rowOff.append(ch, start, length);
if (imageData.isAnchorFromTag()) {
imageData.getImageAnchor().setDy1(Integer.valueOf(rowOff.toString()));
return;
}
imageData.getImageAnchor().setDy2(Integer.valueOf(rowOff.toString()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.alibaba.excel.analysis.v07.handlers;

import java.util.List;

import com.alibaba.excel.metadata.ImageData;
import com.alibaba.excel.read.listener.ImageDataReadListener;

/**
* Handle image anchor "xdr:row" tag
*/
public class ImageAnchorRowTagHandler extends AbstractXlsxImageTagHandler {

@Override
public void characters(ImageDataReadListener imageDataListener, char[] ch, int start, int length) {
List<ImageData> imageDataList = imageDataListener.getImageDataList();
ImageData imageData = imageDataList.get(imageDataList.size() - 1);
StringBuilder row = new StringBuilder();
row.append(ch, start, length);

if (imageData.isAnchorFromTag()) {
imageData.getImageAnchor().setRow1(Integer.valueOf(row.toString()));
return;
}
imageData.getImageAnchor().setRow2(Integer.valueOf(row.toString()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.alibaba.excel.analysis.v07.handlers;

import java.util.List;

import org.apache.poi.openxml4j.opc.PackagePart;
import org.xml.sax.Attributes;

import com.alibaba.excel.metadata.ImageData;
import com.alibaba.excel.read.listener.ImageDataReadListener;

/**
* Handle image anchor "xdr:to" tag
*/
public class ImageAnchorToTagHandler extends AbstractXlsxImageTagHandler {

@Override
public void startElement(ImageDataReadListener imageDataListener, PackagePart packagePart, String name,
Attributes attributes) {
List<ImageData> imageDataList = imageDataListener.getImageDataList();
ImageData imageData = imageDataList.get(imageDataList.size() - 1);
imageData.setAnchorFromTag(false);
}

}
Loading