我尝试用java编辑.doc文件( WORD )。我使用了Apache POI库,但结果是否定的。我有一个以前的文档(模板),我想替换一个特定的单词。 (例如,我可以用字符串替换“name”,无论我想要什么)。
这是我尝试使用的互联网示例:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.CharacterRun;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public class Prueba {
public void searchAndReplace(String inputFilename,
String outputFilename,
HashMap<String, String> replacements) {
File inputFile = null;
File outputFile = null;
FileInputStream fileIStream = null;
FileOutputStream fileOStream = null;
BufferedInputStream bufIStream = null;
BufferedOutputStream bufOStream = null;
POIFSFileSystem fileSystem = null;
HWPFDocument document = null;
Range docRange = null;
Paragraph paragraph = null;
CharacterRun charRun = null;
Set<String> keySet = null;
Iterator<String> keySetIterator = null;
int numParagraphs = 0;
int numCharRuns = 0;
String text = null;
String key = null;
String value = null;
try {
inputFile = new File(inputFilename);
fileIStream = new FileInputStream(inputFile);
bufIStream = new BufferedInputStream(fileIStream);
fileSystem = new POIFSFileSystem(bufIStream);
document = new HWPFDocument(fileSystem);
docRange = document.getRange();
numParagraphs = docRange.numParagraphs();
keySet = replacements.keySet();
for(int i = 0; i < numParagraphs; i++) {
paragraph = docRange.getParagraph(i);
text = paragraph.text();
numCharRuns = paragraph.numCharacterRuns();
for(int j = 0; j < numCharRuns; j++) {
charRun = paragraph.getCharacterRun(j);
text = charRun.text();
keySetIterator = keySet.iterator();
while(keySetIterator.hasNext()) {
key = keySetIterator.next();
if(text.contains(key)) {
value = replacements.get(key);
int start = text.indexOf(key);
charRun.replaceText(key, value, start);
docRange = document.getRange();
paragraph = docRange.getParagraph(i);
charRun = paragraph.getCharacterRun(j);
text = charRun.text();
}
}
}
}
bufIStream.close();
bufIStream = null;
outputFile = new File(outputFilename);
fileOStream = new FileOutputStream(outputFile);
bufOStream = new BufferedOutputStream(fileOStream);
document.write(bufOStream);
}
catch(IOException 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(bufIStream != null) {
try {
bufIStream.close();
bufIStream = null;
}
catch(IOException ex) {
}
}
if(bufOStream != null) {
try {
bufOStream.flush();
bufOStream.close();
bufOStream = null;
}
catch(IOException ex) {
}
}
}
}
public static void main(String[] args) {
Prueba replacer = new Prueba();
HashMap<String, String> searchTerms = new HashMap<>();
searchTerms.put("search term 1", "replacement term 1");
searchTerms.put("search term 2", "replacement term 2");
searchTerms.put("search term 3", "replacement term 3");
searchTerms.put("search term 4", "replacement term 4");
replacer.searchAndReplace("Document.doc", // Source Document
"Replaced Document.doc", // Result Document
searchTerms); // Search/replacement items
}
}
当我执行我的代码时,错误是下一个:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/OldFileFormatException
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:760) at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:455)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:367)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at Prueba.searchAndReplace(Prueba.java:44)
at Prueba.main(Prueba.java:123)
Caused by: java.lang.ClassNotFoundException: org.apache.poi.OldFileFormatException
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 14 more