Java利用freemaker和(excelXML表格或wordXML表格),導出自己任何想要格式的文檔

原文地址:https://www.cnblogs.com/qaz110/p/4936544.html

做管理系統比較愛用,還在像以前用html化報表,還像以前那樣用DIV固定格式,固定填充數據的位置,

1、寫好excel或者word

像這樣:  中間的單元格 你可以隨便填一些字母或者中文,這個是方便找它的位置,像我這樣是因爲我的JAVA代碼封裝了map,我可以通過這樣去取值

2、將這文件另存爲xml表(.xml)的格式,然後將文件扔在你的action包中,或者其他文件夾下。

3、這時候的文件還是.xml格式的文件,但是我們需要的freemaker的文件格式,所以你需要重命名將它的後綴改成.ftl的。

改好後,你可以打開它了,還記得圖片上的我寫了很多那種el表達式嗎,找着你模板中寫了這些表達式的位置:像這樣

4 這時候  我們的模板算是基本完成了。那麼接下來就要去寫方法,然後取出你要填寫到單元格的數據了。如果是WORD也是類似的,不用再像以前那樣用HTML畫DIV了。

5 首先寫好實現類的方法

6 加載數據到模板中。利用freemaker的list循環將數據填充到你需要的位置,如果是單條數據填充word 是不需要循環。(只是我的部分代碼)

@RequestMapping(value="/downloadzcexcel.action")
public void zcExcelImport(HttpServletRequest request, HttpServletResponse response)
throws Exception {
String fileName=null;
DocumentHandler handler=null;
String tempBasePath=null;
String tempName=null;
request.setCharacterEncoding("utf-8");
Map<String, Object> zcdata = new HashMap<String, Object>();
String sbzg=request.getParameter("sbzg");
String bbmc=request.getParameter("bbmc");
String year=request.getParameter("year");
String excelType=request.getParameter("excelType");
String xkzType=request.getParameter("xkzType");
bbmc=java.net.URLDecoder.decode(bbmc,"UTF-8");//參數解碼
sbzg=java.net.URLDecoder.decode(sbzg,"UTF-8");
String pid=request.getParameter("pid");
excelType=java.net.URLDecoder.decode(excelType,"UTF-8"); //報表類型
if("評委會投票表".equals(excelType)){
xkzType=java.net.URLDecoder.decode(xkzType,"UTF-8");
tempBasePath="/com/daqsoft/titlemgmt/Action"; 
tempName="zcpwhtp.ftl";
handler=new DocumentHandler(tempBasePath);
List<Map> list=zcExcelService.getExcelByPwh(sbzg, xkzType,year); 
zcdata.put("rows", list);
zcdata.put("BBMC", bbmc);
zcdata.put("XKZ", xkzType);
zcdata.put("COUNT", list.size());
}else{
if("706".equals(pid)){
tempBasePath="/com/daqsoft/titlemgmt/Action"; //資格評審、名冊報表(轉系列)
tempName="zcExcelByZxl.ftl";
handler=new DocumentHandler(tempBasePath);
List<Map> list=zcExcelService.getSbzgByZc(sbzg,year);
zcdata.put("rows", list);
zcdata.put("BBMC", bbmc);
zcdata.put("COUNT", list.size());
}else{
tempBasePath="/com/daqsoft/titlemgmt/Action"; //資格評審、名冊報表(正常的)
tempName="zcexcel.ftl";
handler=new DocumentHandler(tempBasePath);
List<Map> list=zcExcelService.getSbzgByZc(sbzg,year);
zcdata.put("rows", list);
zcdata.put("BBMC", bbmc);
zcdata.put("COUNT", list.size());
}
}
File file = null;
InputStream inputStream = null;
ServletOutputStream outServletOutputStream = null;
try {
file=handler.createDoc(zcdata, fileName, tempBasePath, tempName);
inputStream=new FileInputStream(file);
response.setCharacterEncoding("utf-8");
response.setContentType("application/msexcel");
response.addHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode(bbmc,"utf-8")+".xls");
outServletOutputStream=response.getOutputStream();
byte[] buffer = new byte[512]; 
int bytesToRead = -1;
while ((bytesToRead = inputStream.read(buffer)) != -1) {
outServletOutputStream.write(buffer, 0, bytesToRead);
}
} catch (Exception e) {
e.printStackTrace();
}finally{

if (inputStream != null)
inputStream.close();
if (outServletOutputStream != null)
outServletOutputStream.close();
if (file != null)
file.delete(); // 刪除臨時文件
}
}

 

7 下面是需要用到一個工具類,和數據流的輸出是一樣的

public File createDoc(Map<String,Object> dataMap,String fileName,String tempBasePath,String tempName) throws UnsupportedEncodingException {
//dataMap 要填入模本的數據文件
//設置模本裝置方法和路徑,FreeMarker支持多種模板裝載方法。可以重servlet,classpath,數據庫裝載,
//這裏我們的模板是放在template包下面
//tempBasePath:'/com/daqsoft/hrmanage/action'
//tempName : 'gbddTemp.ftl'


fileName=fileName!=null?fileName:"temp" + (int) (Math.random() * 100000);
Template t=null;
try {
//test.ftl爲要裝載的模板
t = configuration.getTemplate(tempName);
} catch (IOException e) {
e.printStackTrace();
}
//輸出文檔路徑及名稱
File outFile = new File(fileName);
Writer out = null;
try {
out=new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"); 
t.process(dataMap, out);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("------------完成文件封裝---------------");
return outFile;

 




}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章