我已经编写了一个java基本程序,它使用Apache POI将3种文件(ppt,doc,txt)嵌入到Excel工作表中.现在我要以原始格式导出此文件.这该怎么做?
参考链接是Embed files into Excel using Apache POI.
我从这个链接制作了节目.
总之,我想要嵌入式文件的导出功能.
我使用下面的代码尝试了上面的问题,但是它不能用于在Excel工作表中导出嵌入式文件:
这是试图解决的代码:
public static void main(String[] args) throws IOException {
String fileName = "ole_ppt_in_xls.xls";
ReadExcel(fileName);
}
public static void ReadExcel(String fileName) throws IOException {
FileInputStream inputFileStream = new FileInputStream(fileName);
POIFSFileSystem fs = new POIFSFileSystem(inputFileStream);
HSSFWorkbook workbook = new HSSFWorkbook(fs);
for (HSSFObjectData obj : workbook.getAllEmbeddedObjects()) {
// the OLE2 Class Name of the object
String oleName = obj.getOLE2ClassName();
System.out.println(oleName);
if (oleName.equals("Worksheet")) {
System.out.println("Worksheet");
DirectoryNode dn = (DirectoryNode) obj.getDirectory();
HSSFWorkbook embeddedWorkbook = new HSSFWorkbook(dn, fs, false);
} else if (oleName.equals("Document")) {
System.out.println("Document");
DirectoryNode dn = (DirectoryNode) obj.getDirectory();
HWPFDocument embeddedWordDocument = new HWPFDocument(dn, fs);
} else if (oleName.equals("Presentation")) {
System.out.println("Presentation");
DirectoryNode dn = (DirectoryNode) obj.getDirectory();
SlideShow embeddedPowerPointDocument = new SlideShow(
new HSLFSlideShow(dn, fs));
} else if (oleName.equals("Presentation")) {
System.out.println("Presentation");
DirectoryNode dn = (DirectoryNode) obj.getDirectory();
SlideShow embeddedPowerPointDocument = new SlideShow(
new HSLFSlideShow(dn, fs));
}else {
System.out.println("Else part ");
if (obj.hasDirectoryEntry()) {
System.out.println("obj.hasDirectoryEntry()"+obj.hasDirectoryEntry());
// The DirectoryEntry is a DocumentNode. Examine its entries
DirectoryNode dn = (DirectoryNode) obj.getDirectory();
for (Iterator entries = dn.getEntries(); entries.hasNext();) {
Entry entry = (Entry) entries.next();
System.out.println(oleName + "." + entry.getName());
}
} else {
System.out.println("Else part 22");
byte[] objectData = obj.getObjectData();
}
}
}
}
那么,如何导出功能实现呢?
解决方法:
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
/**
* Demonstrates how you can extract embedded data from a .xlsx file
*/
public class GetEmbedded {
public static void main(String[] args) throws Exception {
String path = "SomeExcelFile.xlsx"
XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(new File(path)));
for (PackagePart pPart : workbook.getAllEmbedds()) {
String contentType = pPart.getContentType();
if (contentType.equals("application/vnd.ms-excel")) { //This is to read xls workbook embedded to xlsx file
HSSFWorkbook embeddedWorkbook = new HSSFWorkbook(pPart.getInputStream());
int countOfSheetXls=embeddedWorkbook.getNumberOfSheets();
}
else if (contentType.equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")) { //This is to read xlsx workbook embedded to xlsx file
if(pPart.getPartName().getName().equals("/xl/embeddings/Microsoft_Excel_Worksheet12.xlsx")){
//"/xl/embeddings/Microsoft_Excel_Worksheet12.xlsx" - Can read an Excel from a particular sheet
// This is the worksheet from the Parent Excel-sheet-12
XSSFWorkbook embeddedWorkbook = new XSSFWorkbook(pPart.getInputStream());
int countOfSheetXlsx=embeddedWorkbook.getNumberOfSheets();
ArrayList<String> sheetNames= new ArrayList<String>();
for(int i=0;i<countOfSheetXlsx;i++){
String name=workbook.getSheetName(i);
sheetNames.add(name);
}
}
}
}
}
}