本文整理汇总了Java中org.apache.poi.openxml4j.opc.OPCPackage.open方法的典型用法代码示例。如果您正苦于以下问题:Java OPCPackage.open方法的具体用法?Java OPCPackage.open怎么用?Java OPCPackage.open使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.poi.openxml4j.opc.OPCPackage
的用法示例。
在下文中一共展示了OPCPackage.open方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: process
import org.apache.poi.openxml4j.opc.OPCPackage; //导入方法依赖的package包/类
/**
* 遍历工作簿中所有的电子表格
*
* @param filename
* @throws Exception
*/
public void process(InputStream stream) throws Exception {
OPCPackage pkg = OPCPackage.open(stream);
XSSFReader r = new XSSFReader(pkg);
SharedStringsTable sst = r.getSharedStringsTable();
XMLReader parser = fetchSheetParser(sst);
Iterator<InputStream> sheets = r.getSheetsData();
while (sheets.hasNext()) {
curRow = 0;
sheetIndex++;
InputStream sheet = sheets.next();
InputSource sheetSource = new InputSource(sheet);
parser.parse(sheetSource);
sheet.close();
}
}
示例2: docxFileContentParser
import org.apache.poi.openxml4j.opc.OPCPackage; //导入方法依赖的package包/类
public String docxFileContentParser(String fileName) {
try {
FileInputStream fs = new FileInputStream(new File(fileName));
OPCPackage d = OPCPackage.open(fs);
if (fileName.endsWith(".docx")) {
XWPFWordExtractor xw = new XWPFWordExtractor(d);
return xw.getText();
} else if (fileName.endsWith(".pptx")) {
XSLFPowerPointExtractor xp = new XSLFPowerPointExtractor(d);
return xp.getText();
} else if (fileName.endsWith(".xlsx")) {
XSSFExcelExtractor xe = new XSSFExcelExtractor(d);
xe.setFormulasNotResults(true);
xe.setIncludeSheetNames(true);
return xe.getText();
}
} catch (Exception e) {
System.out.println("# DocxFileParser Error :" + e.getMessage());
}
return "";
}
示例3: testNonEmptyDoc
import org.apache.poi.openxml4j.opc.OPCPackage; //导入方法依赖的package包/类
@Test
public void testNonEmptyDoc() throws InvalidFormatException, IOException {
try (FileInputStream is = new FileInputStream("resources/document/notEmpty/notEmpty-template.docx");
OPCPackage oPackage = OPCPackage.open(is);
XWPFDocument document = new XWPFDocument(oPackage);) {
TokenProvider iterator = new TokenProvider(document);
XWPFRun run = iterator.next().getRun();
assertEquals("P1Run1 ", run.getText(run.getTextPosition()));
run = iterator.next().getRun();
assertEquals("P1Run2", run.getText(run.getTextPosition()));
run = iterator.next().getRun();
assertEquals(" P1Run3", run.getText(run.getTextPosition()));
run = iterator.next().getRun();
assertEquals("P2Run1 ", run.getText(run.getTextPosition()));
run = iterator.next().getRun();
assertEquals("P2Run2", run.getText(run.getTextPosition()));
run = iterator.next().getRun();
assertEquals(" ", run.getText(run.getTextPosition()));
run = iterator.next().getRun();
assertEquals("P2Run3", run.getText(run.getTextPosition()));
run = iterator.next().getRun();
assertEquals("", run.getText(run.getTextPosition()));
assertTrue(!iterator.hasNext());
}
}
示例4: testLookaheadEmptyIterator
import org.apache.poi.openxml4j.opc.OPCPackage; //导入方法依赖的package包/类
@Test
public void testLookaheadEmptyIterator() throws InvalidFormatException, IOException {
try (FileInputStream is = new FileInputStream("resources/document/notEmpty/notEmpty-template.docx");
OPCPackage oPackage = OPCPackage.open(is);
XWPFDocument document = new XWPFDocument(oPackage);) {
TokenProvider iterator = new TokenProvider(document);
iterator.next().getRun();
iterator.next().getRun();
iterator.next().getRun();
iterator.next().getRun();
iterator.next().getRun();
iterator.next().getRun();
iterator.next().getRun();
iterator.next().getRun();
assertNull(iterator.lookAhead(1));
}
}
示例5: testNextWitLookAhead
import org.apache.poi.openxml4j.opc.OPCPackage; //导入方法依赖的package包/类
@Test
public void testNextWitLookAhead() throws InvalidFormatException, IOException {
try (FileInputStream is = new FileInputStream("resources/document/notEmpty/notEmpty-template.docx");
OPCPackage oPackage = OPCPackage.open(is);
XWPFDocument document = new XWPFDocument(oPackage);) {
TokenProvider iterator = new TokenProvider(document);
// CHECKSTYLE:OFF
assertTrue(iterator.hasElements(7));
// CHECKSTYLE:ON
XWPFRun run;
run = iterator.lookAhead(1).getRun();
assertEquals("P1Run1 ", run.getText(run.getTextPosition()));
run = iterator.next().getRun();
assertEquals("P1Run1 ", run.getText(run.getTextPosition()));
run = iterator.lookAhead(1).getRun();
assertEquals("P1Run2", run.getText(run.getTextPosition()));
run = iterator.lookAhead(2).getRun();
assertEquals(" P1Run3", run.getText(run.getTextPosition()));
run = iterator.next().getRun();
assertEquals("P1Run2", run.getText(run.getTextPosition()));
run = iterator.next().getRun();
assertEquals(" P1Run3", run.getText(run.getTextPosition()));
assertTrue(iterator.hasElements(4));
}
}
示例6: testAccessEmptyIterator
import org.apache.poi.openxml4j.opc.OPCPackage; //导入方法依赖的package包/类
@Test
public void testAccessEmptyIterator() throws InvalidFormatException, IOException {
try (FileInputStream is = new FileInputStream("resources/document/notEmpty/notEmpty-template.docx");
OPCPackage oPackage = OPCPackage.open(is);
XWPFDocument document = new XWPFDocument(oPackage);) {
TokenIterator iterator = new TokenIterator(document);
assertNotNull(iterator.next());
assertNotNull(iterator.next());
assertNotNull(iterator.next());
assertNotNull(iterator.next());
assertNotNull(iterator.next());
assertNotNull(iterator.next());
assertNotNull(iterator.next());
assertNotNull(iterator.next());
assertFalse(iterator.hasNext());
boolean hasException = false;
try {
iterator.next();
} catch (NoSuchElementException e) {
assertTrue(e instanceof NoSuchElementException);
hasException = true;
}
assertTrue(hasException);
}
}
示例7: getTextContent
import org.apache.poi.openxml4j.opc.OPCPackage; //导入方法依赖的package包/类
/**
* Gets the textual element of the .docx at the given {@link URI}.
*
* @param uriConverter
* the {@link URIConverter}
* @param uri
* the .docx {@link URI}
* @return the textual element of the .docx at the given {@link URI}
*/
public static String getTextContent(URIConverter uriConverter, URI uri) {
String result = "";
try (InputStream is = uriConverter.createInputStream(uri);
OPCPackage oPackage = OPCPackage.open(is);
XWPFDocument document = new XWPFDocument(oPackage);
XWPFWordExtractor ex = new XWPFWordExtractor(document);) {
result += "===== Document Text ====\n";
result += ex.getText();
// CHECKSTYLE:OFF
} catch (Throwable e) {
// CHECKSTYLE:ON
/*
* if for some reason we can't use POI to get the text content then move along, we'll still get the XML and hashs
*/
}
return result;
}
示例8: unmarshal
import org.apache.poi.openxml4j.opc.OPCPackage; //导入方法依赖的package包/类
@Override
@SuppressWarnings("unchecked")
public <T> List<T> unmarshal(Class<T> type) {
try (OPCPackage open = OPCPackage.open(poijiFile.file())) {
ReadOnlySharedStringsTable readOnlySharedStringsTable = new ReadOnlySharedStringsTable(open);
XSSFReader xssfReader = new XSSFReader(open);
StylesTable styles = xssfReader.getStylesTable();
SheetIterator iter = (SheetIterator) xssfReader.getSheetsData();
int index = 0;
while (iter.hasNext()) {
try (InputStream stream = iter.next()) {
if (index == options.sheetIndex()) {
return processSheet(styles, readOnlySharedStringsTable, type, stream);
}
}
++index;
}
return new ArrayList<>();
} catch (SAXException | IOException | OpenXML4JException e) {
throw new PoijiException("Problem occurred while reading data", e);
}
}
示例9: process
import org.apache.poi.openxml4j.opc.OPCPackage; //导入方法依赖的package包/类
/**
* 遍历工作簿中所有的电子表格
*
* @param filename string
* @throws Exception if an error occurred
*/
public void process(String filename) throws Exception {
OPCPackage pkg = OPCPackage.open(filename);
XSSFReader r = new XSSFReader(pkg);
SharedStringsTable sst = r.getSharedStringsTable();
XMLReader parser = fetchSheetParser(sst);
Iterator<InputStream> sheets = r.getSheetsData();
while (sheets.hasNext()) {
curRow = 0;
sheetIndex++;
InputStream sheet = sheets.next();
InputSource sheetSource = new InputSource(sheet);
parser.parse(sheetSource);
sheet.close();
}
}
示例10: process
import org.apache.poi.openxml4j.opc.OPCPackage; //导入方法依赖的package包/类
/**
* 遍历工作簿中所有的电子表格
*
* @param filename
* @throws Exception
*/
public void process(String filename) throws Exception {
OPCPackage pkg = OPCPackage.open(filename);
XSSFReader r = new XSSFReader(pkg);
SharedStringsTable sst = r.getSharedStringsTable();
XMLReader parser = fetchSheetParser(sst);
Iterator<InputStream> sheets = r.getSheetsData();
while (sheets.hasNext()) {
curRow = 0;
sheetIndex++;
InputStream sheet = sheets.next();
InputSource sheetSource = new InputSource(sheet);
parser.parse(sheetSource);
sheet.close();
}
}
示例11: importFromXLSX
import org.apache.poi.openxml4j.opc.OPCPackage; //导入方法依赖的package包/类
/**
* ImportFromXLSX
* @param inputStream
* @param sheetNumber
* @return
* @throws InvalidFormatException
* @throws IOException
*/
public static DenseObjectMatrix2D importFromXLSX(final InputStream inputStream, final int sheetNumber)
throws InvalidFormatException, IOException
{
final OPCPackage pkg = OPCPackage.open(inputStream);
final XSSFWorkbook workbook = new XSSFWorkbook(pkg);
final DenseObjectMatrix2D matrix = importFromWorkbook(workbook, sheetNumber);
pkg.close();
return matrix;
}
示例12: execute
import org.apache.poi.openxml4j.opc.OPCPackage; //导入方法依赖的package包/类
public void execute(InputStream inputStream, IParseExcelRowMapper parseExcelRowMapper) throws Exception {
OPCPackage pkg = OPCPackage.open(inputStream);
XSSFReader xssfReader = new XSSFReader(pkg);
sst = xssfReader.getSharedStringsTable();
stylesTable = xssfReader.getStylesTable();
SAXParserFactory saxFactory = SAXParserFactory.newInstance();
SAXParser saxParser = saxFactory.newSAXParser();
XMLReader parser = saxParser.getXMLReader();
parser.setContentHandler(new XSSFParserHandler());
this.parseExcelRowMapper = parseExcelRowMapper;
XSSFReader.SheetIterator iter = (XSSFReader.SheetIterator) xssfReader.getSheetsData();
while (iter.hasNext()) {
curRow = 0;
sheetIndex++;
InputStream sheet = iter.next();
sheetName = iter.getSheetName();
InputSource sheetSource = new InputSource(sheet);
parser.parse(sheetSource);
sheet.close();
}
}
示例13: testOPC
import org.apache.poi.openxml4j.opc.OPCPackage; //导入方法依赖的package包/类
@Test
public void testOPC() throws Exception {
// setup
InputStream inputStream = OOXMLSignatureVerifierTest.class.getResourceAsStream("/hello-world-signed.docx");
// operate
OPCPackage opcPackage = OPCPackage.open(inputStream);
ArrayList<PackagePart> parts = opcPackage.getParts();
for (PackagePart part : parts) {
LOG.debug("part name: " + part.getPartName().getName());
LOG.debug("part content type: " + part.getContentType());
}
ArrayList<PackagePart> signatureParts = opcPackage
.getPartsByContentType("application/vnd.openxmlformats-package.digital-signature-xmlsignature+xml");
assertFalse(signatureParts.isEmpty());
PackagePart signaturePart = signatureParts.get(0);
LOG.debug("signature part class type: " + signaturePart.getClass().getName());
PackageDigitalSignatureManager packageDigitalSignatureManager = new PackageDigitalSignatureManager();
// yeah... POI implementation still missing
}
示例14: DefaultXSSFSaxHandler
import org.apache.poi.openxml4j.opc.OPCPackage; //导入方法依赖的package包/类
public DefaultXSSFSaxHandler(File file, ExcelReadListener excelReader,
boolean ignoreNumFormat) throws InvalidFormatException {
try {
this.pkg = OPCPackage.open(file, PackageAccess.READ);
this.excelReader = excelReader;
this.ignoreNumFormat = ignoreNumFormat;
this.handler = new DefaultSheetContentsHandler(
excelReader);
} catch (IllegalArgumentException e) {
//异常是open方法抛出的。确保io关闭
pkg.revert();
throw e;
}
}
示例15: create
import org.apache.poi.openxml4j.opc.OPCPackage; //导入方法依赖的package包/类
/**
* @param inp
* @param excelReader
* @param ignoreNumFormat 是否忽略数据格式 (default=false,按照格式读取)
* @return jie
* @throws InvalidFormatException
* @throws IOException
* @throws SQLException
*/
public static ReadHandler create(InputStream inp,
ExcelReadListener excelReader, boolean ignoreNumFormat)
throws InvalidFormatException, IOException, SQLException {
// If clearly doesn't do mark/reset, wrap up
if (! inp.markSupported()) {
inp = new PushbackInputStream(inp, 8);
}
// Ensure that there is at least some data there
byte[] header8 = IOUtils.peekFirst8Bytes(inp);
// Try to create
if (POIFSFileSystem.hasPOIFSHeader(header8)) {
POIFSFileSystem fs = new POIFSFileSystem(inp);
return create(fs, excelReader, ignoreNumFormat);
}
if (POIXMLDocument.hasOOXMLHeader(inp)) {
OPCPackage pkg = OPCPackage.open(inp);
return create(pkg, excelReader, ignoreNumFormat);
}
throw new InvalidFormatException("Your InputStream was neither an OLE2 stream, nor an OOXML stream");
}