提问人:Karthick88it 提问时间:11/13/2023 更新时间:11/13/2023 访问量:40
使用 apache poi Java 读取嵌入式 excel
Reading embedded excel using apache poi Java
问:
有没有办法在Java中读取嵌入式excel文件,如下图所示
目前我正在使用下面的代码来读取 excel 文件
File xlsxFile = new File("test.xlsx");
inputStream = new FileInputStream(xlsxFile);
XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
XSSFSheet sheet = workbook.getSheetAt(4);
答:
2赞
Axel Richter
11/13/2023
#1
嵌入的对象在第 5 张图纸的图纸图纸中以形状显示。
若要获取对象数据,可以遍历该图形的所有形状。如果 shape 是 的实例,则它是一个 object-data-shape。如果内容类型为“application/vnd.openxmlformats-officedocument.spreadsheetml.sheet”,则 object-data-shape 将链接到包含 Office Open XML 格式 () 的 Excel 电子表格数据的包部件。如果是这样,那么您可以从该包部件的输入流中获取一个。XSSFObjectData
XSSF
XSSFWorkbook
示例代码:
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import java.io.FileInputStream;
class ExcelGetEmbeddedFromShape {
public static void main(String[] args) throws Exception {
String filename = "./ExcelWithEmbedded.xlsx";
Workbook workbook = WorkbookFactory.create(new FileInputStream(filename));
Sheet sheet = workbook.getSheetAt(4);
Drawing drawing = sheet.getDrawingPatriarch();
if (drawing instanceof XSSFDrawing) {
for (XSSFShape shape : ((XSSFDrawing)drawing).getShapes()) {
if (shape instanceof XSSFObjectData) {
XSSFObjectData objectData = (XSSFObjectData)shape;
if ("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet".equals(objectData.getContentType())) {
XSSFWorkbook embeddedWorkbook = (XSSFWorkbook)WorkbookFactory.create(objectData.getObjectPart().getInputStream());
System.out.println(embeddedWorkbook);
//... do something with embeddedWorkbook ...
}
}
}
}
workbook.close();
}
}
评论