poi实现word文档的导入(针对.doc .docx rtf)

public void importUnitInfo() throws IOException{
String filePath = this.getHttpServletRequest().getParameter("docWord");
File file = new File(filePath);
POIFSFileSystem pfs = null;
List<String> list = new ArrayList<String>();
organization = new Organization();
String info="";
Boolean sign =true;
FileInputStream ins = new FileInputStream(filePath);// 载入文档
// WordExtractor extractor = new WordExtractor(ins);
// // 对DOC文件进行提取
// String text = extractor.getText();
XWPFWordExtractor docx = null;
int index = filePath.lastIndexOf(".");
String fileType =filePath.substring(index);
try {
if(fileType.equals(".docx")){

docx = new XWPFWordExtractor(POIXMLDocument.openPackage(filePath));//对docx文档的操作

}else if(fileType.equals(".doc")){

pfs = new POIFSFileSystem(ins); // 对doc文档的操作
}
} catch (Exception e) {
if(pfs==null&&docx==null){
sign=false;

}
}
if(sign==true){//导入的文件格式是word(doc或者docx)
try {
if(fileType.equals(".doc")){
HWPFDocument hwpf = new HWPFDocument(pfs);
Range range = hwpf.getRange();// 得到文档的读取范围
TableIterator it = new TableIterator(range);// 迭代文档中的表格
String cellString="";
if (it.hasNext()) {
TableRow tr = null;
TableCell td = null;
org.apache.poi.hwpf.usermodel.Paragraph para = null;
org.apache.poi.hwpf.usermodel.Table tb = it.next();
// 迭代行,从第1行开始
for (int i = 0; i < tb.numRows(); i++) {
tr = tb.getRow(i);
for (int j = 0; j < tr.numCells(); j++) {
td = tr.getCell(j);// 取得单元格
// 取得单元格的内容
for (int k = 0; k < td.numParagraphs(); k++) {
para = td.getParagraph(k);
cellString = para.text();
boolean flag = true;

if (cellString != null && cellString.compareTo("") != 0&&flag==true) {
// 如果不trim,取出的内容后会有一个乱码字符
cellString = cellString.trim();
}
info+= cellString;
}
}

}
}
}else if(fileType.equals(".docx")){
info = docx.getText();
info =info.replaceAll("\n", "");
info = info.replaceAll("\r", "");

}
info = info.replaceAll("\\s*", "");
.......(获得word中的内容(info)后,对具体内容操作很简单就不列出了) this.getHttpServletResponse().getWriter().write("success");
} catch (Exception e) {
this.getHttpServletResponse().getWriter().write("fail");
}
}else {//导入的文件是rtf格式
try {
RTFEditorKit rtf = new RTFEditorKit();
DefaultStyledDocument styledDoc = new DefaultStyledDocument();
FileInputStream in = new FileInputStream(filePath);
rtf.read(in, styledDoc, 0);
info = new String(styledDoc.getText(0,styledDoc.getLength()).getBytes("GBK")); //提取文本,读取中文需要使用ISO8859_1编码,否则会出现乱码
info = info.replaceAll("\n", "");
info = info.replaceAll("\\s*", "");
.......(获得word中的内容(info)后,对具体内容操作很简单就不列出了) [/color][color=black][size=xx-small][/size] this.getHttpServletResponse().getWriter().write("success");
} catch (IOException e) {
this.getHttpServletResponse().getWriter().write("fail");
e.printStackTrace();
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
可以使用Apache POI库来实现在Spring Boot应用程序中在线编辑docdocx文档。 首先,需要添加以下依赖项到`pom.xml`文件中: ```xml <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> ``` 然后,可以使用以下代码示例来创建和编辑docx文件: ```java import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.List; import java.util.Map; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; import org.springframework.stereotype.Service; @Service public class DocxService { public byte[] createDocx(List<Map<String, String>> data) throws IOException { XWPFDocument document = new XWPFDocument(); for (Map<String, String> map : data) { XWPFParagraph paragraph = document.createParagraph(); XWPFRun run = paragraph.createRun(); run.setText(map.get("text")); run.setFontSize(14); run.addBreak(); } ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); document.write(outputStream); document.close(); return outputStream.toByteArray(); } } ``` 在这个示例中,`createDocx`方法接收一个包含文本数据的`List`,并创建一个新的docx文件。该方法使用`XWPFDocument`类创建一个新的文档对象,并使用`XWPFParagraph`和`XWPFRun`类创建段落和文本运行。最后,该方法将文档写入`ByteArrayOutputStream`并返回字节数组。 可以使用类似的方式创建和编辑doc文件,只需要使用`HWPF`类库代替`XWPF`类库即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值