org.apache.poi
poi-ooxml
3.15
org.apache.poi
poi
3.15
代码
package com.dxy.study_0628.util.word;
import com.deepoove.poi.XWPFTemplate;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFdocument;
import org.apache.xmlbeans.XmlException;
import java.io.*;
import java.util.Map;
public class WordUtil {
public static String toword(Map datas,String mbpath,String descpath) throws IOException {
String filePath = mbpath;
String outPath = descpath;
XWPFTemplate template = XWPFTemplate.compile(filePath)
.render(datas);
FileOutputStream out = new FileOutputStream(outPath);
template.write(out);
out.flush();
out.close();
template.close();
return descpath;
}
public static String readDocx(String path)
{
//都是只能用String,不能用Stringbuffer,还不知道原因
String text = null;
try {
InputStream is = new FileInputStream(path);
XWPFdocument doc = new XWPFdocument(is);
XWPFWordExtractor extractor = new XWPFWordExtractor(doc);
text = extractor.getText();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return text;
}
public static void main(String[] args) throws IOException, OpenXML4JException, XmlException {
//生成word
String s = WordUtil.readDocx("C:\Users\asus\Desktop\吉首大学-邓新宇-java方向.docx");
System.out.println(s);
}
}
问题解决



