1. 程式人生 > 其它 >java匯出word 拿來直接用

java匯出word 拿來直接用

java匯出word

package com.boerxin.controller;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.io.Writer; import java.net.URLEncoder; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletResponse; import org.springframework.
beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import com.aliyuncs.
utils.IOUtils; import com.boerxin.entity.Model.AccessoryModel; import com.boerxin.entity.Model.RetrieveHistoryModel; import com.boerxin.service.RetrieveHistoryService; import com.boerxin.util.AjaxJson; import com.boerxin.util.FilePathUtil; import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException; @RestController @CrossOrigin @RequestMapping("/aaa") public class WordTest3 { @Autowired private RetrieveHistoryService retrieveHistoryService; @RequestMapping(value = "/a", method = RequestMethod.GET) public void createWord(HttpServletResponse resp,String rhId) throws IOException{ Map<String,Object> dataMap=new HashMap<String,Object>(); getData(dataMap,rhId); Configuration cfg = new Configuration(); cfg.setDefaultEncoding("UTF-8"); cfg.setClassForTemplateLoading(this.getClass(), "/templates"); Template t=null; try { t = cfg.getTemplate("QualityInspectionResults.ftl"); //獲取模板檔案 } catch (IOException e) { e.printStackTrace(); } //File outFile = new File("D:/outFile"+Math.random()*10000+".doc"); //匯出檔案 Writer out = null; resp.setContentType("application/vnd.ms-excel"); try { resp.addHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode("檔名", "UTF-8") + ".doc"); } catch (UnsupportedEncodingException e2) { // TODO Auto-generated catch block e2.printStackTrace(); } try { //out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile))); out = new BufferedWriter(new OutputStreamWriter(resp.getOutputStream())); } catch (FileNotFoundException e1) { e1.printStackTrace(); } try { t.process(dataMap, out); //將填充資料填入模板檔案並輸出到目標檔案 } catch (TemplateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } private void getData(Map<String, Object> dataMap,String rhId) { RetrieveHistoryModel model = retrieveHistoryService.selectOneRetrieveHistory(rhId); if(model!=null) { dataMap.put("searchContent", model.getSearchContent()); dataMap.put("klKeyword", model.getKlKeyword()); }else { dataMap.put("searchContent", ""); dataMap.put("klKeyword",""); } } }

ftl檔案就放這裡,程式碼總的路徑不用改
pom檔案

  <dependency>
		    <groupId>org.springframework.boot</groupId>
		    <artifactId>spring-boot-starter-freemarker</artifactId>
		</dependency>