07版的数据没显示出来
package com.imooc.service;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.poifs.filesystem.OfficeXmlFileException;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import com.imooc.dto.ImportWordParamDto;
import com.imooc.dto.ImportWordResultDto;
public class WordService {
public ImportWordResultDto imp(ImportWordParamDto dto) {
ImportWordResultDto result=new ImportWordResultDto();
//03版和07版word结构相差较大,基本上没有什么可以重用的或者说定义相同的方法去解析的
result.setTitle(dto.getTitle());
HWPFDocument doc=null;
//03版
try {
doc=new HWPFDocument(dto.getWord().getInputStream());
//内容已经读出来了;解析的是word里的内容,而不是格式
result.setContent(doc.getDocumentText().replace("\r", "<br/>"));
}catch(OfficeXmlFileException oe) {
System.out.println("这是一个07版的Word");
}
catch (Exception e) {
// TODO Auto-generated catch block
result.setMsg("这可能不是一个Word");
return result;
}finally {
if(doc != null) {
try {
doc.close();
return result;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//07版
XWPFDocument docx=null;
try {
docx=new XWPFDocument(dto.getWord().getInputStream());
//获取所有段落
List<XWPFParagraph> paragraphList=docx.getParagraphs();
//循环段落,把每一段拼成字符串
StringBuffer content=new StringBuffer();
for(int i=0;i<paragraphList.size();i++) {
if(i != 0) {
content.append("<br/>");
}
content.append(paragraphList.get(i).getText());
}
result.setContent(content.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(docx != null) {
try {
docx.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return result;
}
/*
* export
* 读出来的字符串,
* 我们可以把这个纯文字的字符串里内容替换掉,
* 之后,把这个内容和原来模板的格式再给它合上
*
* 对象提供的方法,能帮我们去做替换的动作,但还保留
* 原来的格式,只有这样才是在原来word模板上去做修改,
*
* 替换完之后,再将你替换完的这个对象,写入到新的文件里,
* 重新生成新的Word
*/
public HWPFDocument export03(Map<String,String> replaceContent) {
HWPFDocument doc=null;
//03版
try {
doc=new HWPFDocument(new FileInputStream("D:\\template\\template_03.doc"));
//获取范围
Range range=doc.getRange();
//循环map,把它迭代出来
for(Map.Entry<String, String> entry : replaceContent.entrySet()) {
//Range里有replaceText();将什么替换成什么
range.replaceText(entry.getKey(),entry.getValue());
}
}
catch (Exception e) {
return null;
}
return doc;
}
public XWPFDocument export07(Map<String,String> replaceContent) {
XWPFDocument docx=null;
//07版
try {
docx=new XWPFDocument(new FileInputStream("D:\\template\\template_07.docx"));
List<XWPFParagraph> paragraphList=docx.getParagraphs();
for(XWPFParagraph paragraph : paragraphList) {
List<XWPFRun> runs= paragraph.getRuns();
for(XWPFRun run : runs) {
String str=run.getText(run.getTextPosition());
for(Map.Entry<String, String> entry : replaceContent.entrySet()) {
str.replace(entry.getKey(), entry.getValue());
}
//把它在塞回run里面,这样的话,这个动作就完成了
//0表示是从获取的每一小段的第一个字开始给换掉
run.setText(str,0);
}
}
}
catch (Exception e) {
return null;
}
return docx;
}
}
package com.imooc.servlet;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import com.imooc.service.WordService;
public class ExportWordServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
req.setCharacterEncoding("UTF-8");
WordService service=new WordService();
Map<String,String> param=new HashMap<>();
param.put("${name}", req.getParameter("name"));
param.put("${age}", req.getParameter("age"));
param.put("${time}", req.getParameter("time"));
ServletOutputStream outputStream=resp.getOutputStream();
if(req.getParameter("isDocx") != null && !"".equals(req.getParameter("isDocx"))) {
XWPFDocument docx=service.export07(param);
resp.setHeader("Content-Disposition", "attachment;filename=export.docx");
docx.write(outputStream);
docx.close();
}else {
HWPFDocument doc=service.export03(param);
resp.setHeader("Content-Disposition", "attachment;filename=export.doc");
doc.write(outputStream);
doc.close();
}
outputStream.flush();
outputStream.close();
}
}
刚开始出错时,docx.write(outputStream);报的是这个位置是空指针异常,后来启动/调试几次,控制台就不报错了,就是没数据。
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星