Java-POI-读取-Execl-图片
目录
Java POI 读取 Execl 图片
Java POI 解析Execl 中图片
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.NumberFormat;
import java.util.Iterator;
import java.util.List;
import org.apache.poi.POIXMLDocumentPart;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
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.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.PictureData;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFPicture;
import org.apache.poi.xssf.usermodel.XSSFShape;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker;
/**
* *
* * poi版本必须3.9及以上
* *
* * @author haijunli.lhj
* *
*
*/
public class ExcelImageHandler {
private static final NumberFormat numberFormat = NumberFormat.getNumberInstance();
/**
*
*
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
String name = "C:\\Users\\haijunli.lhj\\Desktop\\a.xlsx";
process(new FileInputStream(name));
}
/**
*
*
* @param is
* @throws Exception
*/
public static void process(InputStream is) throws Exception {
Workbook wb = WorkbookFactory.create(is);
if (wb instanceof HSSFWorkbook) {
HSSFWorkbook hssfworkbook = (HSSFWorkbook)wb;
HSSFSheet sheet = hssfworkbook.getSheetAt(0);
process(sheet, hssfworkbook);
} else {
XSSFWorkbook hssfworkbook = (XSSFWorkbook)wb;
XSSFSheet sheet = hssfworkbook.getSheetAt(0);
process(sheet);
Iterator<Row> rit = sheet.iterator();
while (rit.hasNext()) {
Row row = rit.next();
System.out.print(row.getRowNum() + "\t");
Iterator<Cell> cit = row.iterator();
while (cit.hasNext()) {
System.out.print(getString(cit.next()) + "\t");
}
System.out.print("\n");
}
}
}
/**
*
*
* @param sheet
* @param workbook
* @throws IOException
*/
public static void process(HSSFSheet sheet, HSSFWorkbook workbook) throws Exception {
List<HSSFPictureData> pictures = workbook.getAllPictures();
if (CollectionUtil.isNotEmpty(pictures)) {
for (HSSFShape shape : sheet.getDrawingPatriarch().getChildren()) {
HSSFClientAnchor anchor = (HSSFClientAnchor)shape.getAnchor();
if (shape instanceof HSSFPicture) {
HSSFPicture pic = (HSSFPicture)shape;
int pictureIndex = pic.getPictureIndex() - 1;
handle(anchor.getRow1(), anchor.getCol1(), pictures.get(pictureIndex));
}
}
}
}
/**
*
*
* @param sheet
* @throws IOException
*/
public static void process(XSSFSheet sheet) throws Exception {
for (POIXMLDocumentPart dr : sheet.getRelations()) {
if (dr instanceof XSSFDrawing) {
XSSFDrawing drawing = (XSSFDrawing)dr;
List<XSSFShape> shapes = drawing.getShapes();
for (XSSFShape shape : shapes) {
XSSFPicture pic = (XSSFPicture)shape;
XSSFClientAnchor anchor = pic.getPreferredSize();
CTMarker ctMarker = anchor.getFrom();
handle(ctMarker.getRow(), ctMarker.getCol(), pic.getPictureData());
}
}
}
}
/**
*
*
* @param rowIndex
* @param colIndex
* @param picData
* @throws IOException
*/
public static void handle(int rowIndex, int colIndex, PictureData picData) throws Exception {
String fileName = picData.suggestFileExtension();
fileName = "C:\\Users\\haijunli.lhj\\Desktop\\images\\" + rowIndex + "_" + colIndex + "." + fileName;
byte[] data = picData.getData();
FileOutputStream out = new FileOutputStream(fileName);
out.write(data);
out.close();
}
/**
* @see Cell#CELL_TYPE_BLANK
* @see Cell#CELL_TYPE_NUMERIC
* @see Cell#CELL_TYPE_STRING
* @see Cell#CELL_TYPE_FORMULA
* @see Cell#CELL_TYPE_BOOLEAN
* @see Cell#CELL_TYPE_ERROR
* @param cell
* @return
*/
public static String getString(Cell cell) {
String str = null;
if (cell == null)
return str;
CellType t = cell.getCellTypeEnum();
if (CellType.STRING == t) {
str = cell.getStringCellValue();
} else if (CellType.NUMERIC == t) {
str = numberFormat.format(cell.getNumericCellValue());
str = str.replaceAll(",", "");
} else if (CellType.BOOLEAN == t) {
str = String.valueOf(cell.getBooleanCellValue());
} else if (CellType.FORMULA == t) {
str = String.valueOf(cell.getCellFormula());
} else if (CellType.ERROR == t) {
str = String.valueOf(cell.getErrorCellValue());
}
if (str != null)
str = str.trim();
return str;
}
}