WORD匯出
阿新 • • 發佈:2018-11-22
- public class HwpfTest {
- @Test
- public void testWrite() throws Exception {
- String templatePath = "D:\\word\\template.doc"
- InputStream is = new FileInputStream(templatePath);
- HWPFDocument doc = new HWPFDocument(is);
- Range range = doc.getRange();
- //把range範圍內的${reportDate}替換為當前的日期
- range.replaceText("${reportDate}", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
- range.replaceText("${appleAmt}"
- range.replaceText("${bananaAmt}", "200.00");
- range.replaceText("${totalAmt}", "300.00");
- OutputStream os = new FileOutputStream("D:\\word\\write.doc");
- //把doc輸出到輸出流中
- doc.write(os);
- this.closeStream(os);
- this.closeStream(is);
- //瀏覽器匯出
response.reset(); response.setContentType("application/msword; charset=UTF-8"); // response.setCharacterEncoding ("utf-8");//設定編碼集,檔名不會發生中文亂碼 response.setHeader("Content-Disposition", "Attachment;filename= " + new String((adYear+"年"+adMonth+"月計劃.doc").toString().getBytes( //"UTF-8"),"UTF-8")); "gb2312"), "ISO8859_1")); OutputStream outstream= null; try { outstream = response.getOutputStream(); document.write(outstream); }catch (Exception e){ e.printStackTrace(); }finally { try { if(tempFileInputStream!=null){ outstream.close(); } if(outstream!=null){ outstream.close(); } }catch (Exception io){ io.printStackTrace(); } }
- }
- /**
- * 關閉輸入流
- * @param is
- */
- private void closeStream(InputStream is) {
- if (is != null) {
- try {
- is.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- /**
- * 關閉輸出流
- * @param os
- */
- private void closeStream(OutputStream os) {
- if (os != null) {
- try {
- os.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
1.2 例項2
需要的jar包:poi-3.10.1.jar;poi-ooxml-3.10.1.jar;poi-ooxml-schemas-3.10.1.jar;poi-scratchpad-3.10.1.jar
* POI方式匯出word文件,需要提前建立word匯出模板
* POI方式匯出word時,本機安裝的office版本是2007以前的版本,使用到的是HWPFDocument
* POI方式匯出word時,本機安裝的office版本是2007+的版本,使用到的是XWPFDocument
- package officeWordDoc;
- import java.io.FileInputStream;
- import java.net.URLEncoder;
- import java.util.HashMap;
- import java.util.Map;
- import javax.faces.context.FacesContext;
- import javax.servlet.ServletOutputStream;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.poi.hwpf.HWPFDocument;
- import org.apache.poi.hwpf.usermodel.Range;
- /**
- * @ClassName:WordDocDemo_POI
- * @Description:使用POI方式實現word文件匯出功能
- * 需要的jar包:poi-3.10.1.jar;poi-ooxml-3.10.1.jar;
- * poi-ooxml-schemas-3.10.1.jar;poi-scratchpad-3.10.1.jar
- * POI方式匯出word文件,需要提前建立word匯出模板
- * POI方式匯出word時,本機安裝的office版本是2007以前的版本,使用到的是HWPFDocument
- * POI方式匯出word時,本機安裝的office版本是2007+的版本,使用到的是XWPFDocument
- * @date:2017年5月12日
- * 修改備註:
- */
- public class WordDocDemo_POI{
- public static void main(String[] args){
- WordDocDemo_POI poi = new WordDocDemo_POI();
- poi.exportWord();
- }
- /**
- * @Description:使用POI實現word匯出功能,JSF頁面請求,此處涉及到JSF框架中FacesContext物件,可摘除,
- * @date: 2017年5月12日 下午6:50:51
- * @修改備註:
- */
- public void exportWord() {
- FacesContext context = FacesContext.getCurrentInstance();
- context.responseComplete();
- HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
- HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
- response.reset();
- try {
- String fileName = "關於"+"eventInfo.getEventName()"+"的情況報告.doc";
- fileName = URLEncoder.encode(fileName, "UTF-8");
- response.setHeader("Content-disposition", "attachment;filename="+fileName);
- response.setContentType("application/msword");
- ServletOutputStream outputStream = response.getOutputStream();
- //引入word模板,轉化成流
- //String wordTemp = "E:\\專案\\匯出報告模板\\reportTemplate.doc";
- String wordTemp = request.getSession().getServletContext().getRealPath("eventInfo.export.reportTemplate");
- FileInputStream inputStream = new FileInputStream(wordTemp);
- HWPFDocument document = new HWPFDocument(inputStream);
- //替換內容
- Range range = document.getRange();
- Map<String, String> content = this.getReplaceContent();
- for(java.util.Map.Entry<String, String> entry : content.entrySet()){
- range.replaceText(entry.getKey(), entry.getValue());
- }
- document.write(outputStream);
- inputStream.close();
- outputStream.flush();
- outputStream.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- FacesContext.getCurrentInstance().responseComplete();
- }
- /**
- * 獲取模板中需要替換的文字
- * @param report
- * @return
- */
- private Map<String, String> getReplaceContent(){
- Map<String, String> content = new HashMap<String, String>();
- content.put("${eventType}", "typeName型別名稱");
- content.put("${reportName}", "report.getReportName報告名稱");
- content.put("${reporterUnit}", "report.getReporterUnit報送單位");
- content.put("${loginUser}", "loginUser登入人員資訊");
- content.put("${nowTime}", "yyyy年MM月dd日時間訊息");
- content.put("${occurTime}", "yyyy年MM月dd日事發時間");
- content.put("${disaster}", "非必填寫1");
- content.put("${eventDesc}", "非必填寫2");
- content.put("${eventDesc}", "非必填寫3");
- return content;
- }
- }
https://blog.csdn.net/QQ578473688/article/details/72082085?locationNum=15&fps=1