forked from javister/excel-streaming-reader
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
656 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/main/java/com/github/pjfanning/xlsx/impl/ooxml/OoxmlStrictHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package com.github.pjfanning.xlsx.impl.ooxml; | ||
|
||
import org.apache.poi.openxml4j.exceptions.InvalidFormatException; | ||
import org.apache.poi.openxml4j.opc.OPCPackage; | ||
import org.apache.poi.openxml4j.opc.PackagePart; | ||
import org.apache.poi.openxml4j.opc.internal.MemoryPackagePart; | ||
import org.apache.poi.util.TempFile; | ||
import org.apache.poi.xssf.model.StylesTable; | ||
import org.apache.poi.xssf.model.ThemesTable; | ||
import org.apache.poi.xssf.usermodel.XSSFRelation; | ||
|
||
import javax.xml.stream.XMLStreamException; | ||
import java.io.*; | ||
import java.util.List; | ||
|
||
public class OoxmlStrictHelper { | ||
public static ThemesTable getThemesTable(OPCPackage pkg) throws IOException, XMLStreamException, InvalidFormatException { | ||
List<PackagePart> parts = pkg.getPartsByContentType(XSSFRelation.THEME.getContentType()); | ||
if (parts.isEmpty()) { | ||
return null; | ||
} else { | ||
PackagePart part = parts.get(0); | ||
File tempFile = TempFile.createTempFile("ooxml-strict-themes", ".xml"); | ||
try { | ||
try( | ||
InputStream is = part.getInputStream(); | ||
OutputStream os = new FileOutputStream(tempFile); | ||
OoXmlStrictConverter converter = new OoXmlStrictConverter(is, os) | ||
) { | ||
while (converter.convertNextElement()) { | ||
//continue | ||
} | ||
} | ||
MemoryPackagePart newPart = new MemoryPackagePart(pkg, part.getPartName(), part.getContentType()); | ||
try(InputStream is = new FileInputStream(tempFile)) { | ||
newPart.load(is); | ||
} | ||
return new ThemesTable(newPart); | ||
} finally { | ||
tempFile.delete(); | ||
} | ||
} | ||
} | ||
|
||
public static StylesTable getStylesTable(OPCPackage pkg) throws IOException, XMLStreamException, InvalidFormatException { | ||
List<PackagePart> parts = pkg.getPartsByContentType(XSSFRelation.STYLES.getContentType()); | ||
if (parts.isEmpty()) { | ||
return null; | ||
} else { | ||
PackagePart part = parts.get(0); | ||
File tempFile = TempFile.createTempFile("ooxml-strict-styles", ".xml"); | ||
try { | ||
try( | ||
InputStream is = part.getInputStream(); | ||
OutputStream os = new FileOutputStream(tempFile); | ||
OoXmlStrictConverter converter = new OoXmlStrictConverter(is, os) | ||
) { | ||
while (converter.convertNextElement()) { | ||
//continue | ||
} | ||
} | ||
MemoryPackagePart newPart = new MemoryPackagePart(pkg, part.getPartName(), part.getContentType()); | ||
try(InputStream is = new FileInputStream(tempFile)) { | ||
newPart.load(is); | ||
} | ||
return new StylesTable(newPart); | ||
} finally { | ||
tempFile.delete(); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.