跪求java实现对有内容的word文档添加页眉,jacob、poi、itext都没尝试成功,跪求指导! 50
2014-10-22
展开全部
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/
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/
追问
pieryon 加下我的秋秋,看下你的私信
博思aippt
2024-07-20 广告
2024-07-20 广告
博思AIPPT是基于ai制作PPT的智能在线工具,它提供了4种AI制作PPT的方式,包括AI生成大纲、AI直接生成PPT、文本生成PPT、AI提炼word文档生成PPT,一站式集成多种AI生成PPT的方式,可满足办公用户的不同需求和使用场景...
点击进入详情页
本回答由博思aippt提供
展开全部
try {
document = new XWPFDocument();
/************* Adding Para *****************/
para = document.createParagraph();
run = para.createRun();
run.setText("Some Text");
run.setBold(true);
run = para.createRun();
run.setText("Goodbye");
/************* End Adding Para *****************/
/************* Adding Footer *****************/
hfPolicy = document.getHeaderFooterPolicy();
if (hfPolicy == null) {
hfPolicy = new XWPFHeaderFooterPolicy(document);
}
hfPolicy.createFooter(org.openxmlformats.schemas.wordprocessingml.x2006.main.STHdrFtr.Enum
.forString("This is a Footer."));
/************* End Adding Footer *****************/
file = new File(documentFolder, documentName);
fos = new FileOutputStream(file);
document.write(fos);
} catch (Exception ex) {
System.out.println("Caught an: " + ex.getClass().getName());
System.out.println("Message: " + ex.getMessage());
System.out.println("Stacktrace follows:.....");
ex.printStackTrace(System.out);
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException ioEx) {
// I G N O R E
}
}
}
}
这里注意一下最新的jar:
1. poi-3.10-FINAL.jar
2. poi-ooxml-3.10-FINAL.jar
?3. poi-ooxml-schemas-3.10-FINAL.jar
document = new XWPFDocument();
/************* Adding Para *****************/
para = document.createParagraph();
run = para.createRun();
run.setText("Some Text");
run.setBold(true);
run = para.createRun();
run.setText("Goodbye");
/************* End Adding Para *****************/
/************* Adding Footer *****************/
hfPolicy = document.getHeaderFooterPolicy();
if (hfPolicy == null) {
hfPolicy = new XWPFHeaderFooterPolicy(document);
}
hfPolicy.createFooter(org.openxmlformats.schemas.wordprocessingml.x2006.main.STHdrFtr.Enum
.forString("This is a Footer."));
/************* End Adding Footer *****************/
file = new File(documentFolder, documentName);
fos = new FileOutputStream(file);
document.write(fos);
} catch (Exception ex) {
System.out.println("Caught an: " + ex.getClass().getName());
System.out.println("Message: " + ex.getMessage());
System.out.println("Stacktrace follows:.....");
ex.printStackTrace(System.out);
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException ioEx) {
// I G N O R E
}
}
}
}
这里注意一下最新的jar:
1. poi-3.10-FINAL.jar
2. poi-ooxml-3.10-FINAL.jar
?3. poi-ooxml-schemas-3.10-FINAL.jar
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
建议你使用卓正软件的pageoffice 。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |