跪求java实现对有内容的word文档添加页眉,jacob、poi、itext都没尝试成功,跪求指导!

跪求java实现对有内容的word文档添加页眉,jacob、poi、itext都没尝试成功,跪求指导!,第1张

package com.ray.poi.util

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 {

// 创建输入流读取DOC文件

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)

可以试试,满意请采纳


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11192644.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-14
下一篇 2023-05-14

发表评论

登录后才能评论

评论列表(0条)

保存