引言
java後臺代碼,定時生成word,可包含圖片,文字,表格等。本文只用到圖片,文字。
技術指標
- FreeMarker
- jar包:freemarker-2.3.13.jar
- spring定時任務
過程概述
1.先定義好word模板,編輯好樣式,另存爲xml;
2.打開xml,用
3.將xml後綴名改爲ftl文件;
4.準備數據;
5.執行程序,填充數據,保存成doc文件;
6.方法放入spring定時任務中。
主要代碼
## 生成word代碼 ##
public class DocFreeMarker {
private Configuration configuration = null;
public DocFreeMarker() {
configuration = new Configuration();
configuration.setDefaultEncoding("UTF-8");
}
public String getImageStr(String imgFile){
InputStream in=null;
byte[] data=null;
try {
//in=new FileInputStream(imgFile);
if(imgFile.indexOf("http")==0){
URL url = new URL(imgFile);
//in = url.openStream();
URLConnection connection = url.openConnection();
connection.connect();
in = connection.getInputStream();
}else if(imgFile.length()>0){
in = new FileInputStream(imgFile);
}
data=new byte[in.available()];
in.read(data);
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
BASE64Encoder encoder=new BASE64Encoder();
return encoder.encode(data);
}
public void createDoc(Map<String,Object> dataMap,String downloadType,String savePath){
try {
//加載需要裝填的模板
Template template=null;
//設置模板裝置方法和路徑,FreeMarker支持多種模板裝載方法。可以重servlet,classpath,數據庫裝載。
//加載模板文件,放在testDoc下
configuration.setClassForTemplateLoading(this.getClass(), "/com/zhwy/auto/template");
//設置對象包裝器
// configure.setObjectWrapper(new DefaultObjectWrapper());
//設置異常處理器
configuration.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
//定義Template對象,注意模板類型名字與downloadType要一致,xml和ftl好像都可以。
template = configuration.getTemplate(downloadType+".ftl","UTF-8");
File outFile = new File(savePath);
Writer out=null;
out=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "UTF-8"));
template.process(dataMap, out);
if(out!=null){
out.flush();
out.close();
}
//out.close();
} catch (IOException e) {
e.printStackTrace();
} catch (TemplateException e) {
e.printStackTrace();
}
}
public Map<String,Object> createData(){
Map<String,Object> dataMap = new HashMap<String, Object>();
dataMap.put("RAIN_1H_TEXT", "從20日18時到19時,保定、滄州及以南的大部地區出現降雨天氣");
dataMap.put("RAIN_3H_TEXT", "從20日18時到19時,保定、滄州及以南的大部地區出現降雨天氣。");
dataMap.put("RAIN_6H_TEXT", "從20日18時到19時,保定、滄州及以南的大部地區出現降雨天氣。");
dataMap.put("RAIN_12H_TEXT", "從20日18時到19時,保定、滄州及以南的大部地區出現降雨天氣。");
dataMap.put("RAIN_24H_TEXT", "從20日18時到19時,保定、滄州及以南的大部地區出現降雨天氣。");
dataMap.put("RAIN_1H_IMG", getImageStr("E:\\FreeMarker\\1.png"));
dataMap.put("RAIN_3H_IMG", getImageStr("E:\\FreeMarker\\3.png"));
dataMap.put("RAIN_6H_IMG", getImageStr("E:\\FreeMarker\\6.png"));
dataMap.put("RAIN_12H_IMG", getImageStr("E:\\FreeMarker\\12.png"));
dataMap.put("RAIN_24H_IMG", getImageStr("E:\\FreeMarker\\24.png"));
return dataMap;
}
public void createWord1() {
DocFreeMarker test = new DocFreeMarker();
Map<String, Object> dataMap = test.createData();
String downloadType = "RainKuaiBao";
long time = System.currentTimeMillis();
String savePath = "E:/FreeMarker/RainKuaiBao" + time + ".doc";
test.createDoc(dataMap, downloadType, savePath);
}
public static void main(String[] args) {
DocFreeMarker dfm = new DocFreeMarker();
dfm.createWord1();
}
}
## spring定時任務代碼 ##
@Component
public class ProductAutoTask {
@Autowired
private ProductAuto ProductAuto;
@Scheduled(cron="0 10 * * * ?") //秒 分鐘 小時 天 月 星期 年
public void test(){
System.out.println("<<<*****自動生成雨情快報定時任務開始*****>>>");
ProductAuto.docAuto_rainKuaiBao();
System.out.println("<<<---------雨情快報生成結束--------->>>");
}
}
srping配置:
<beans
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.1.xsd"
>
<!-- 增加定時任務插件 -->
<context:annotation-config></context:annotation-config>
<!-- 開啓這個配置 spring才能識別@Scheduled註解 -->
<task:annotation-driven scheduler="qbScheduler" mode="proxy"/>
<task:scheduler id="qbScheduler" pool-size="10"/>
踩過的坑
- word模板中應添完全不一樣的圖片,要不然,保存成xml的時候,只會找到一處被base64編碼的位置。
- 程序中必須替換ftl中定義的${}標籤,並且內容不能爲null,可以爲空“”,若是圖片,生成的word,扔保留圖片的位置。
參考資料
1.FreeMarker之概念介紹 http://blog.csdn.net/y_love_f/article/details/41595647
2.FreeMarker三宗罪 http://www.iteye.com/topic/17468