WordUtil java匯出word工具類

來源:互聯網
上載者:User

標籤:string   exists   app   .class   cep   test   tput   下載檔案   encode   

 

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.URLEncoder;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import freemarker.template.Configuration;
import freemarker.template.Template;
/**
* @Desc:word操作工具類
*
* */
public class WordUtil {
private static Logger log = Logger.getLogger(WordUtil.class);
/**
* @Desc:產生word檔案
* @paramdataMap word中需要展示的動態資料,用map集合來儲存
* @paramtemplateName word模板名稱,例如:test.ftl
* @paramfilePath檔案產生的目標路徑,例如:D:/wordFile/
* @paramfileName產生的檔案名稱,例如:test.doc
* */
public static void createWord(Map<String, Object>dataMap,String templateName,String filePath,String fileName){
try {
//建立配置執行個體
Configuration configuration = new Configuration();
//設定編碼
configuration.setDefaultEncoding("UTF-8");
//ftl模板檔案
File file = new File(filePath);
configuration.setDirectoryForTemplateLoading(file);
//擷取模板
Template template = configuration.getTemplate(templateName);
//輸出檔案
File outFile = new File(filePath + File.separator + fileName);
//如果輸出目標檔案夾不存在,則建立
if (!outFile.getParentFile().exists()){
outFile.getParentFile().mkdirs();
}
//將模板和資料模型合并組建檔案
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile),"UTF-8"));
//組建檔案
template.process(dataMap, out);
//關閉流
out.flush();
out.close();
} catch (Exception e) {
log.error("產生 word文檔(WordUtil)出錯:【msg:"+e.getMessage()+"】 ,檔案名稱:" + fileName);
e.printStackTrace();
}
}
/** 檔案下載
* @param path 檔案路徑全路徑,包含檔案名稱
* @param response
* @return
* */
public static HttpServletResponse downFile(String path, HttpServletResponse response) {
try {
// path是指欲下載的檔案的路徑。
File file = new File(path);
// 取得檔案名稱。
String filename = file.getName();
// 以流的形式下載檔案。
InputStream fis = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
// 設定response的Header
String fileName = URLEncoder.encode(filename,"UTF-8");
if(fileName.length()>150){ //解決IE 6.0 bug
fileName=new String(filename.getBytes("GBK"),"ISO-8859-1"); }
response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
response.addHeader("Content-Length", "" + file.length());
OutputStream outs = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
outs.write(buffer);
outs.flush();
outs.close();
file.delete();
}catch (IOException e) {
log.error("下載文檔(WordUtil)出錯:【msg:"+e.getMessage()+"】 "); e.printStackTrace(); }
return response;
}
}

WordUtil java匯出word工具類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.