❶ java 利用poi 可以直接读取word中的表格保持样式生成新的word么
1.读取word
2003及word
2007需要的jar包
读取
2003
版本(.doc)的word文件相对来说比较简单,只需要
poi-3.5-beta6-20090622.jar
和
poi-scratchpad-3.5-beta6-20090622.jar
两个
jar
包即可,
而
2007
版本(.docx)就麻烦多,我说的这个麻烦不是我们写代码的时候麻烦,是要导入的
jar
包比较的多,有如下
7
个之多:
1.
openxml4j-bin-beta.jar
2.
poi-3.5-beta6-20090622.jar
3.
poi-ooxml-3.5-beta6-20090622.jar
4
.dom4j-1.6.1.jar
5.
geronimo-stax-api_1.0_spec-1.0.jar
6.
ooxml-schemas-1.0.jar
7.
xmlbeans-2.3.0.jar
其中
4-7
是
poi-ooxml-3.5-beta6-20090622.jar
所依赖的
jar
包(在
poi-bin-3.5-beta6-20090622.tar.gz
中的
ooxml-lib
目录下可以找到)。
2.换行符号
硬换行:文件中换行,如果是键盘中使用了"enter"的换行。
软换行:文件中一行的字符数容量有限,当字符数量超过一定值时,会自动切到下行显示。
对程序来说,硬换行才是可以识别的、确定的换行,软换行与字体大小、缩进有关。
3.读取的注意事项
值得注意的是:
POI
在读取不会读取
word
文件中的图片信息;
还有就是对于
2007
版的
word(.docx),
如果
word
文件中有表格,所有表格中的数据都会在读取出来的字符串的最后。
4.读取word文本内容代码
1
import
java.io.File;
2
import
java.io.FileInputStream;
3
import
java.io.InputStream;
4
5
import
org.apache.poi.POIXMLDocument;
6
import
org.apache.poi.POIXMLTextExtractor;
7
import
org.apache.poi.hwpf.extractor.WordExtractor;
8
import
org.apache.poi.openxml4j.opc.OPCPackage;
9
import
org.apache.poi.xwpf.extractor.XWPFWordExtractor;
10
11
public
class
Test
{
12
public
static
void
main(String[]
args)
{
13
try
{
14
InputStream
is
=
new
FileInputStream(new
File("2003.doc"));
15
WordExtractor
ex
=
new
WordExtractor(is);
16
String
text2003
=
ex.getText();
17
System.out.println(text2003);
18
19
OPCPackage
opcPackage
=
POIXMLDocument.openPackage("2007.docx");
20
POIXMLTextExtractor
extractor
=
new
XWPFWordExtractor(opcPackage);
21
String
text2007
=
extractor.getText();
22
System.out.println(text2007);
23
24
}
catch
(Exception
e)
{
25
e.printStackTrace();
26
}
27
}
28
}
❷ java 中用poi读取word和用docx4j读取word
不知道你是具体读取Word里面的什么元素,下面以读取文字和图片为例吧,两个代码示例,你参考看看:
读取文本
import com.spire.doc.Document;
import java.io.FileWriter;
import java.io.IOException;
public class ExtractText {
public static void main(String[] args) throws IOException {
//加载Word文档
Document document = new Document();
document.loadFromFile("C:\Users\Administrator\Desktop\sample.docx");
//获取文档中的文本保存为String
String text=document.getText();
//将String写入Txt文件
writeStringToTxt(text,"ExtractedText.txt");
}
public static void writeStringToTxt(String content, String txtFileName) throws IOException {
FileWriter fWriter= new FileWriter(txtFileName,true);
try {
fWriter.write(content);
}catch(IOException ex){
ex.printStackTrace();
}finally{
try{
fWriter.flush();
fWriter.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}}
2. 读取图片
import com.spire.doc.Document;
import com.spire.doc.documents.DocumentObjectType;
import com.spire.doc.fields.DocPicture;
import com.spire.doc.interfaces.ICompositeObject;
import com.spire.doc.interfaces.IDocumentObject;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
public class ExtractImages {
public static void main(String[] args) throws IOException {
//加载Word文档
Document document = new Document();
document.loadFromFile("C:\Users\Administrator\Desktop\sample.docx");
//创建Queue对象
Queue nodes = new LinkedList();
nodes.add(document);
//创建List对象
List images = new ArrayList();
//遍历文档中的子对象
while (nodes.size() > 0) {
ICompositeObject node = nodes.poll();
for (int i = 0; i < node.getChildObjects().getCount(); i++) {
IDocumentObject child = node.getChildObjects().get(i);
if (child instanceof ICompositeObject) {
nodes.add((ICompositeObject) child);
//获取图片并添加到List
if (child.getDocumentObjectType() == DocumentObjectType.Picture) {
DocPicture picture = (DocPicture) child;
images.add(picture.getImage());
}
}
}
}
//将图片保存为PNG格式文件
for (int i = 0; i < images.size(); i++) {
File file = new File(String.format("output/图片-%d.png", i));
ImageIO.write(images.get(i), "PNG", file);
}
}
}
注意这里使用的jar包是spire.doc.jar,需要在java程序中先导入jar文件。
❸ JAVA使用POI读写word 乱码
写
public static void main(String args[])
throws Exception
{
XWPFDocument doc = new XWPFDocument();
XWPFParagraph p1 = doc.createParagraph();
p1.setAlignment(ParagraphAlignment.CENTER);
p1.setBorderBottom(Borders.DOUBLE);
p1.setBorderTop(Borders.DOUBLE);
p1.setBorderRight(Borders.DOUBLE);
p1.setBorderLeft(Borders.DOUBLE);
p1.setBorderBetween(Borders.SINGLE);
p1.setVerticalAlignment(TextAlignment.TOP);
XWPFRun r1 = p1.createRun();
r1.setBold(true);
r1.setText("The quick brown fox");
r1.setBold(true);
r1.setFontFamily("Courier");
r1.setUnderline(UnderlinePatterns.DOT_DOT_DASH);
r1.setTextPosition(100);
XWPFParagraph p2 = doc.createParagraph();
p2.setAlignment(ParagraphAlignment.RIGHT);
p2.setBorderBottom(Borders.DOUBLE);
p2.setBorderTop(Borders.DOUBLE);
p2.setBorderRight(Borders.DOUBLE);
p2.setBorderLeft(Borders.DOUBLE);
p2.setBorderBetween(Borders.SINGLE);
XWPFRun r2 = p2.createRun();
r2.setText("jumped over the lazy dog");
r2.setStrike(true);
r2.setFontSize(20);
XWPFRun r3 = p2.createRun();
r3.setText("and went away");
r3.setStrike(true);
r3.setFontSize(20);
r3.setSubscript(VerticalAlign.SUPERSCRIPT);
XWPFParagraph p3 = doc.createParagraph();
p3.setWordWrap(true);
p3.setPageBreak(true);
p3.setAlignment(ParagraphAlignment.BOTH);
p3.setSpacingLineRule(LineSpacingRule.EXACT);
p3.setIndentationFirstLine(600);
XWPFRun r4 = p3.createRun();
r4.setTextPosition(20);
r4.setText("To be, or not to be: that is the question: Whether 'tis nobler in the mind to suffer The slings and arrows of outrageous fortune, Or to take arms against a sea of troubles, And by opposing end them? To die: to sleep; ");
r4.addBreak(BreakType.PAGE);
r4.setText("No more; and by a sleep to say we end The heart-ache and the thousand natural shocks That flesh is heir to, 'tis a consummation Devoutly to be wish'd. To die, to sleep; To sleep: perchance to dream: ay, there's the rub; .......");
r4.setItalic(true);
XWPFRun r5 = p3.createRun();
r5.setTextPosition(-10);
r5.setText("For in that sleep of death what dreams may come");
r5.addCarriageReturn();
r5.setText("When we have shuffled off this mortal coil,Must give us pause: there's the respectThat makes calamity of so long life;");
r5.addBreak();
r5.setText("For who would bear the whips and scorns of time,The oppressor's wrong, the proud man's contumely,");
r5.addBreak(BreakClear.ALL);
r5.setText("The pangs of despised love, the law's delay,The insolence of office and the spurns.......");
FileOutputStream out = new FileOutputStream("simple.docx");
doc.write(out);
out.close();
}
❹ java用POI第三方API操作word的时候,读取最终状态
java读取word文档时,虽然网上介绍了很多插件poi、java2Word、jacob、itext等等,poi无法读取格式(新的API估计行好像还在处于研发阶段,不太稳定,做项目不太敢用);java2Word、jacob容易报错找不到注册,比较诡异,我曾经在不同的机器上试过,操作方法完全一致,有的机器不报错,有的报错,去他们论坛找高人解决也说不出原因,项目部署用它有点玄;itxt好像写很方便但是我查了好久资料没有见到过关于读的好办法。经过一番选择还是折中点采用rtf最好,毕竟rtf是开源格式,不需要借助任何插件,只需基本IO操作外加编码转换即可。rtf格式文件表面看来和doc没啥区别,都可以用word打开,各种格式都可以设定。