poi实现word文档的导入(针对.doc .docx rtf)
来源:互联网 发布:控制女高中生软件漫画 编辑:程序博客网 时间:2024/05/09 21:51
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();
}
}
}
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
- poi实现word文档的导入(针对.doc .docx rtf)
- 利用poi操作word文档(针对docx格式)
- 利用poi操作word文档(针对docx格式)
- JAVA使用POI操作word文档实例,兼容doc,docx
- poi修改word文档doc/docx不支持图片
- java POI word的docx文档中的文字替换,并把docx转成pdf文档
- POI根据docx文件生成word文档(jeesite)
- POI实现DOC/DOCX转HTML
- POI读取.doc 和.docx的区别
- java读取word格式.doc或者.docx中的内容(APACHE POI)
- 国内首个开源js在线word文档编辑器oarword-doc/docx文档样式,导出导出doc/docx
- POI解析文档内容(txt,doc,docx,xls,xlsx,ppt,pdf)
- POI处理office文档(doc、docx、xls、xlsx、ppt、pptx)
- poi操作doc、docx、xls、xlsx文档总结
- java读取word表格导入数据库,支持doc、docx
- 如何在线看文档-07(docx)免费转03(doc)word
- Apache POI 简单操作word文档(2007 .docx格式)
- 转:JAVA实现word doc docx pdf excel的在线浏览
- C语言进阶总结3
- 音频常见的波形编码
- C++ 排序算法总结 堆排序 快速排序
- 打动面试官的五大技能
- 第一周--胖子伤不起
- poi实现word文档的导入(针对.doc .docx rtf)
- 前端开发的交流论坛
- 高并发量网站解决方案
- Poj 1631 Bridging signals(二分+DP 解 LIS)
- php 在各种web服务器的运行模式
- C提取文件名的编号
- 【6年开源路】海王星给你好看!FineUI v4.0正式版暨《FineUI3to4一键升级工具》发布!
- forward 和 redirect的区别
- PHP5.4+Apache2.2的环境配置