android下解析PPT、word使用POI出现错误

Ronys 2012-07-18 06:09:54
如题:
出现如下错误:
java.lang.NoClassDefFoundError: org.apache.poi.hslf.HSLFSlideShow
at org.apache.poi.hslf.usermodel.SlideShow.<init>(SlideShow.java:123)

为什么啊?难道android不支持POI吗?
我确定那个包我已经导入了!我一层一层包找下去都有这个类啊!纳闷。。。怎么回事啊!!!
谁给指导下 啊
...全文
423 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
AMinfo 2012-07-18
  • 打赏
  • 举报
回复
第二部份:

/**
* Extracts the main PowerPoint document stream from the
* POI file, ready to be passed
*
* @throws IOException
*/
private void readPowerPointStream() throws IOException
{
// Get the main document stream
DocumentEntry docProps =
(DocumentEntry)directory.getEntry("PowerPoint Document");

// Grab the document stream
_docstream = new byte[docProps.getSize()];
directory.createDocumentInputStream("PowerPoint Document").read(_docstream);
}

/**
* Builds the list of records, based on the contents
* of the PowerPoint stream
*/
private void buildRecords()
{
// The format of records in a powerpoint file are:
// <little endian 2 byte "info">
// <little endian 2 byte "type">
// <little endian 4 byte "length">
// If it has a zero length, following it will be another record
// <xx xx yy yy 00 00 00 00> <xx xx yy yy zz zz zz zz>
// If it has a length, depending on its type it may have children or data
// If it has children, these will follow straight away
// <xx xx yy yy zz zz zz zz <xx xx yy yy zz zz zz zz>>
// If it has data, this will come straigh after, and run for the length
// <xx xx yy yy zz zz zz zz dd dd dd dd dd dd dd>
// All lengths given exclude the 8 byte record header
// (Data records are known as Atoms)

// Document should start with:
// 0F 00 E8 03 ## ## ## ##
// (type 1000 = document, info 00 0f is normal, rest is document length)
// 01 00 E9 03 28 00 00 00
// (type 1001 = document atom, info 00 01 normal, 28 bytes long)
// 80 16 00 00 E0 10 00 00 xx xx xx xx xx xx xx xx
// 05 00 00 00 0A 00 00 00 xx xx xx
// (the contents of the document atom, not sure what it means yet)
// (records then follow)

// When parsing a document, look to see if you know about that type
// of the current record. If you know it's a type that has children,
// process the record's data area looking for more records
// If you know about the type and it doesn't have children, either do
// something with the data (eg TextRun) or skip over it
// If you don't know about the type, play safe and skip over it (using
// its length to know where the next record will start)
//

_records = read(_docstream, (int)currentUser.getCurrentEditOffset());
}

private Record[] read(byte[] docstream, int usrOffset){
ArrayList<Integer> lst = new ArrayList<Integer>();
HashMap<Integer,Integer> offset2id = new HashMap<Integer,Integer>();
while (usrOffset != 0){
UserEditAtom usr = (UserEditAtom) Record.buildRecordAtOffset(docstream, usrOffset);
lst.add(Integer.valueOf(usrOffset));
int psrOffset = usr.getPersistPointersOffset();

PersistPtrHolder ptr = (PersistPtrHolder)Record.buildRecordAtOffset(docstream, psrOffset);
lst.add(Integer.valueOf(psrOffset));
Hashtable<Integer,Integer> entries = ptr.getSlideLocationsLookup();
for(Integer id : entries.keySet()) {
Integer offset = entries.get(id);
lst.add(offset);
offset2id.put(offset, id);
}

usrOffset = usr.getLastUserEditAtomOffset();
}
//sort found records by offset.
//(it is not necessary but SlideShow.findMostRecentCoreRecords() expects them sorted)
Integer a[] = lst.toArray(new Integer[lst.size()]);
Arrays.sort(a);
Record[] rec = new Record[lst.size()];
for (int i = 0; i < a.length; i++) {
Integer offset = a[i];
rec[i] = Record.buildRecordAtOffset(docstream, offset.intValue());
if(rec[i] instanceof PersistRecord) {
PersistRecord psr = (PersistRecord)rec[i];
Integer id = offset2id.get(offset);
psr.setPersistId(id.intValue());
}
}

return rec;
}

/**
* Find the "Current User" stream, and load it
*/
private void readCurrentUserStream() {
try {
currentUser = new CurrentUserAtom(directory);
} catch(IOException ie) {
logger.log(POILogger.ERROR, "Error finding Current User Atom:\n" + ie);
currentUser = new CurrentUserAtom();
}
}

/**
* Find any other streams from the filesystem, and load them
*/
private void readOtherStreams() {
// Currently, there aren't any
}

/**
* Find and read in pictures contained in this presentation.
* This is lazily called as and when we want to touch pictures.
*/
private void readPictures() throws IOException {
_pictures = new ArrayList<PictureData>();

byte[] pictstream;

try {
DocumentEntry entry = (DocumentEntry)directory.getEntry("Pictures");
pictstream = new byte[entry.getSize()];
DocumentInputStream is = directory.createDocumentInputStream("Pictures");
is.read(pictstream);
} catch (FileNotFoundException e){
// Silently catch exceptions if the presentation doesn't
// contain pictures - will use a null set instead
return;
}

int pos = 0;
// An empty picture record (length 0) will take up 8 bytes
while (pos <= (pictstream.length-8)) {
int offset = pos;

// Image signature
int signature = LittleEndian.getUShort(pictstream, pos);
pos += LittleEndian.SHORT_SIZE;
// Image type + 0xF018
int type = LittleEndian.getUShort(pictstream, pos);
pos += LittleEndian.SHORT_SIZE;
// Image size (excluding the 8 byte header)
int imgsize = LittleEndian.getInt(pictstream, pos);
pos += LittleEndian.INT_SIZE;

// When parsing the BStoreDelay stream, [MS-ODRAW] says that we
// should terminate if the type isn't 0xf007 or 0xf018->0xf117
if (!((type == 0xf007) || (type >= 0xf018 && type <= 0xf117)))
break;

// The image size must be 0 or greater
// (0 is allowed, but odd, since we do wind on by the header each
// time, so we won't get stuck)
if(imgsize < 0) {
throw new CorruptPowerPointFileException("The file contains a picture, at position " + _pictures.size() + ", which has a negatively sized data length, so we can't trust any of the picture data");
}

// If they type (including the bonus 0xF018) is 0, skip it
if(type == 0) {
logger.log(POILogger.ERROR, "Problem reading picture: Invalid image type 0, on picture with length " + imgsize + ".\nYou document will probably become corrupted if you save it!");
logger.log(POILogger.ERROR, "" + pos);
} else {
// Build the PictureData object from the data
try {
PictureData pict = PictureData.create(type - 0xF018);

// Copy the data, ready to pass to PictureData
byte[] imgdata = new byte[imgsize];
System.arraycopy(pictstream, pos, imgdata, 0, imgdata.length);
pict.setRawData(imgdata);

pict.setOffset(offset);
_pictures.add(pict);
} catch(IllegalArgumentException e) {
logger.log(POILogger.ERROR, "Problem reading picture: " + e + "\nYou document will probably become corrupted if you save it!");
}
}

pos += imgsize;
}
}


/**
* Writes out the slideshow file the is represented by an instance
* of this class.
* It will write out the common OLE2 streams. If you require all
* streams to be written out, pass in preserveNodes
* @param out The OutputStream to write to.
* @throws IOException If there is an unexpected IOException from
* the passed in OutputStream
*/
public void write(OutputStream out) throws IOException {
// Write out, but only the common streams
write(out,false);
}
AMinfo 2012-07-18
  • 打赏
  • 举报
回复
HSLFSlideShow.java文件如下,放到org\apache\poi\hslf\目录下

不过,少了这个文件就有可能其他文件也缺少了。

代码太长,分两部份,第一部份:


/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */

package org.zkoss.poi.hslf;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;

import org.zkoss.poi.POIDocument;
import org.zkoss.poi.hslf.exceptions.CorruptPowerPointFileException;
import org.zkoss.poi.hslf.exceptions.EncryptedPowerPointFileException;
import org.zkoss.poi.hslf.exceptions.HSLFException;
import org.zkoss.poi.hslf.record.CurrentUserAtom;
import org.zkoss.poi.hslf.record.ExOleObjStg;
import org.zkoss.poi.hslf.record.PersistPtrHolder;
import org.zkoss.poi.hslf.record.PersistRecord;
import org.zkoss.poi.hslf.record.PositionDependentRecord;
import org.zkoss.poi.hslf.record.Record;
import org.zkoss.poi.hslf.record.UserEditAtom;
import org.zkoss.poi.hslf.usermodel.ObjectData;
import org.zkoss.poi.hslf.usermodel.PictureData;
import org.zkoss.poi.poifs.filesystem.DirectoryNode;
import org.zkoss.poi.poifs.filesystem.DocumentEntry;
import org.zkoss.poi.poifs.filesystem.DocumentInputStream;
import org.zkoss.poi.poifs.filesystem.NPOIFSFileSystem;
import org.zkoss.poi.poifs.filesystem.POIFSFileSystem;
import org.zkoss.poi.util.LittleEndian;
import org.zkoss.poi.util.POILogFactory;
import org.zkoss.poi.util.POILogger;

/**
* This class contains the main functionality for the Powerpoint file
* "reader". It is only a very basic class for now
*
* @author Nick Burch
*/
public final class HSLFSlideShow extends POIDocument {
// For logging
private POILogger logger = POILogFactory.getLogger(this.getClass());

// Holds metadata on where things are in our document
private CurrentUserAtom currentUser;

// Low level contents of the file
private byte[] _docstream;

// Low level contents
private Record[] _records;

// Raw Pictures contained in the pictures stream
private List<PictureData> _pictures;

// Embedded objects stored in storage records in the document stream, lazily populated.
private ObjectData[] _objects;

/**
* Returns the underlying POIFSFileSystem for the document
* that is open.
*/
protected POIFSFileSystem getPOIFSFileSystem() {
return directory.getFileSystem();
}

/**
* Returns the directory in the underlying POIFSFileSystem for the
* document that is open.
*/
protected DirectoryNode getPOIFSDirectory() {
return directory;
}

/**
* Constructs a Powerpoint document from fileName. Parses the document
* and places all the important stuff into data structures.
*
* @param fileName The name of the file to read.
* @throws IOException if there is a problem while parsing the document.
*/
public HSLFSlideShow(String fileName) throws IOException
{
this(new FileInputStream(fileName));
}

/**
* Constructs a Powerpoint document from an input stream. Parses the
* document and places all the important stuff into data structures.
*
* @param inputStream the source of the data
* @throws IOException if there is a problem while parsing the document.
*/
public HSLFSlideShow(InputStream inputStream) throws IOException {
//do Ole stuff
this(new POIFSFileSystem(inputStream));
}

/**
* Constructs a Powerpoint document from a POIFS Filesystem. Parses the
* document and places all the important stuff into data structures.
*
* @param filesystem the POIFS FileSystem to read from
* @throws IOException if there is a problem while parsing the document.
*/
public HSLFSlideShow(POIFSFileSystem filesystem) throws IOException
{
this(filesystem.getRoot());
}

/**
* Constructs a Powerpoint document from a POIFS Filesystem. Parses the
* document and places all the important stuff into data structures.
*
* @param filesystem the POIFS FileSystem to read from
* @throws IOException if there is a problem while parsing the document.
*/
public HSLFSlideShow(NPOIFSFileSystem filesystem) throws IOException
{
this(filesystem.getRoot());
}

/**
* Constructs a Powerpoint document from a specific point in a
* POIFS Filesystem. Parses the document and places all the
* important stuff into data structures.
*
* @deprecated Use {@link #HSLFSlideShow(DirectoryNode)} instead
* @param dir the POIFS directory to read from
* @param filesystem the POIFS FileSystem to read from
* @throws IOException if there is a problem while parsing the document.
*/
@Deprecated
public HSLFSlideShow(DirectoryNode dir, POIFSFileSystem filesystem) throws IOException
{
this(dir);
}

/**
* Constructs a Powerpoint document from a specific point in a
* POIFS Filesystem. Parses the document and places all the
* important stuff into data structures.
*
* @param dir the POIFS directory to read from
* @throws IOException if there is a problem while parsing the document.
*/
public HSLFSlideShow(DirectoryNode dir) throws IOException
{
super(dir);

// First up, grab the "Current User" stream
// We need this before we can detect Encrypted Documents
readCurrentUserStream();

// Next up, grab the data that makes up the
// PowerPoint stream
readPowerPointStream();

// Check to see if we have an encrypted document,
// bailing out if we do
boolean encrypted = EncryptedSlideShow.checkIfEncrypted(this);
if(encrypted) {
throw new EncryptedPowerPointFileException("Encrypted PowerPoint files are not supported");
}

// Now, build records based on the PowerPoint stream
buildRecords();

// Look for any other streams
readOtherStreams();
}
/**
* Constructs a new, empty, Powerpoint document.
*/
public static final HSLFSlideShow create() {
InputStream is = HSLFSlideShow.class.getResourceAsStream("data/empty.ppt");
if (is == null) {
throw new RuntimeException("Missing resource 'empty.ppt'");
}
try {
return new HSLFSlideShow(is);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
AMinfo 2012-07-18
  • 打赏
  • 举报
回复
首先,不是不支持POI,否则就不是这个提示了,

其次,这个出错是org.apache.poi.hslf.usermodel.SlideShow.java这个文件出错,里面有调用到另外一个类org.apache.poi.hslf.HSLFSlideShow,这个类不存在,所以出错。
Ronys 2012-07-18
  • 打赏
  • 举报
回复
自己先顶下!!!!偶是新手!!!多多指教啊!求大神啊

80,349

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧