Using freemarker to save the ecrats picture to word
In a business in an existing field, it is often necessary to make statistics on a kind of data, generate echarts charts and export them with corresponding description materials:
Save eckarts locally
java reads pictures for base64 encoding
Making freemarker templates
Fill freemarker template
Download word
Save eckarts locally
Use js to send the corresponding request:
//Let the picture load for a while
setTimeout(exportImage, 2000);
function exportImage(){
var data = "a="+encodeURIComponent(myChart.getDataURL("png"));
var xmlhttp;
if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("POST","<%=path%>/servlet/saveImage",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
alert("Save successfully");
}
}
xmlhttp.send(data);
}
}
);
The java code is as follows:
String a = request.getParameter("a");
try {
String[] url = a.split(",");
String u = url[1];
// Base64 decoding
byte[] b = new BASE64Decoder().decodeBuffer(u);
WordUtil wordUtil = new WordUtil();
String fileStr = wordUtil.saveFile();
// Generate pictures
OutputStream out = new FileOutputStream(new File(fileStr+"\\test.png"));
out.write(b);
out.flush();
out.close();
java reads pictures for base64 encoding
Convert the file to byte array and code base64 as follows
public String getImageStr(String imgFile) {
InputStream in = null;
byte[] data = null;
try {
in = new FileInputStream(imgFile);
data = new byte[in.available()];
in.read(data);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);
}
Making freemarker templates
It's easy to make freemarker templates. Note the version of word:
The word documents are typeset, pictures and words are fixed. office is used to save word as xml file. Open xml file and ${*} is used to replace words and pictures, save xml and change suffix to ftl.
In this way, our template is ready. Next, we call the tool class to fill the content into the template. The code is as follows:
public class TemplateTool {
private Configuration configuration = null;
public TemplateTool(){
configuration = new Configuration();
configuration.setDefaultEncoding("utf-8");
}
// Parameters: template path, template name, generated file path, data map to be added
public void createFile(String temPath, String temName, String filePath, Map dataMap){
configuration.setClassForTemplateLoading(this.getClass(), temPath);
Template tl = null;
try{
tl = configuration.getTemplate(temName);
}catch (IOException e){
e.printStackTrace();
}
File outFile = new File(filePath);
Writer out = null;
try{
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile)));
}catch(FileNotFoundException e1){
e1.printStackTrace();
}
try{
tl.process(dataMap, out, ObjectWrapper.BEANS_WRAPPER);
out.flush();
out.close();
}catch (TemplateException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
}
Here, the word with pictures and texts has been generated. Next, as long as the file download is provided, the function will be realized.