❶ 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打開,各種格式都可以設定。