import java.io.ByteArrayInputStream
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.IOException
import org.apache.poi.poifs.filesystem.DirectoryEntry
import org.apache.poi.poifs.filesystem.DocumentEntry
import org.apache.poi.poifs.filesystem.POIFSFileSystem
import org.textmining.text.extraction.WordExtractor
/**
* 读写doc
* @author wangzonghao
*
*/
public class POIWordUtil {
/**
* 读入doc
* @param doc
* @return
* @throws Exception
*/
public static String readDoc(String doc) throws Exception {
FileInputStream in = new FileInputStream(new File(doc))
WordExtractor extractor = null
String text = null
// 创建WordExtractor
extractor = new WordExtractor()
// 对DOC文件进行提取
text = extractor.extractText(in)
return text
}
/**
* 写出doc
* @param path
* @param content
* @return
*/
public static boolean writeDoc(String path, String content) {
boolean w = false
try {
// byte b[] = content.getBytes("ISO-8859-1")
byte b[] = content.getBytes()
ByteArrayInputStream bais = new ByteArrayInputStream(b)
POIFSFileSystem fs = new POIFSFileSystem()
DirectoryEntry directory = fs.getRoot()
DocumentEntry de = directory.createDocument("WordDocument", bais)
FileOutputStream ostream = new FileOutputStream(path)
fs.writeFilesystem(ostream)
bais.close()
ostream.close()
} catch (IOException e) {
e.printStackTrace()
}
return w
}
}
测试
package com.ray.poi.util
import junit.framework.TestCase
public class POIUtilTest extends TestCase {
public void testReadDoc() {
try{
String text = POIWordUtil.readDoc("E:/work_space/poi/com/ray/poi/util/demo.doc")
System.out.println(text)
}catch(Exception e){
e.printStackTrace()
}
}
public void testWriteDoc() {
String wr
try {
wr = POIWordUtil.readDoc("E:/work_space/poi/com/ray/poi/util/demo.doc")
boolean b = POIWordUtil.writeDoc("c:\\demo.doc",wr)
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
}
}
看看取出来的对象有哪些属性
两个jar包
http://www.apache.org/dyn/closer.cgi/poi/release/
http://www.ibiblio.org/maven2/org/textmining/tm-extractors/0.4/
WritableSheet s = w.createSheet("Report", 0)BufferedImage input = ImageIO.read(new URL("http://example.com/image.jpg"))
ByteArrayOutputStream baos = new ByteArrayOutputStream()
ImageIO.write(input, "PNG", baos)
s.addImage(new WritableImage(0,0,input.getWidth() / CELL_DEFAULT_WIDTH, input.getHeight() / CELL_DEFAULT_HEIGHT,baos.toByteArray()))
s.mergeCells(0,0,0,2)
可以试试,满意请采纳
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)