Example
of exporting word function 1. Create a word template
First create a template that you need to export with specific formats and styles. The data occupies the word template with placeholders in FreeMarker syntax.
2. Save and edit the xml file. After
creating the word template, you need to save the word as xml format file, which is easy to view and edit as an xml file that conforms to the FreeMarker template syntax to ensure the accuracy of the exported word data. There are many specific FreeMaker syntax online, so I won't go into details here.
import freemarker.template.Configuration; import freemarker.template.Template; import java.io.File; import java.io.IOException; import java.io.Writer; import java.util.Map; /** * */ public class WordUtil { private Configuration configuration = null; /** * Construction method */ public WordUtil() { try { configuration = new Configuration(); configuration.setDefaultEncoding("UTF-8"); } catch (Exception e) { e.printStackTrace (); } } /** * Get templates based on classpath * @param templatePath * @param templateName * @return * @throws IOException */ private Template getTemplate(String templatePath, String templateName) throws IOException { configuration.setDirectoryForTemplateLoading(new File(templatePath)); Template t = configuration.getTemplate(templateName); t.setEncoding("UTF-8"); return t; } /** * Generate word document * @param templatePath * @param templateName * @param dataMap * @param out */ public void write(String templatePath, String templateName, Map<String, Object> dataMap, Writer out) { try { Template t = getTemplate(templatePath, templateName); t.process(dataMap, out); out.close(); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace (); } } }
import com.blinkfox.util.WordUtil; import com.jfinal.kit.PathKit; import java.io.FileOutputStream; import java.io.OutputStreamWriter; import java.io.Writer; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * Created by */ public class ExportWordTest { /** * Construct test data * @return */ public static Map<String, Object> createDatas() { Map<String, Object> testMap = new HashMap<String, Object>(); // construct scattered data testMap.put("author", "Zhejiang Flame"); testMap.put("date", "2015-11-20"); // Construct the list loop data and store it in the ArrayList collection List<Map<String, String>> list = new ArrayList<Map<String, String>>(); for (int i = 0; i < 5; i++) { Map<String, String> map = new HashMap<String, String>(); map.put("xh", (i + 1) + ""); map.put("name", "张三" + i); map.put("phone", "1381111222" + i); list.add(map); } testMap.put("datas", list); return testMap; } /** * @param args */ public static void main(String[] args) { Map<String, Object> testMap = createDatas(); WordUtil handler = new WordUtil(); Writer out = null; try { // Generate the word file of test.doc to a file path FileOutputStream fos = new FileOutputStream("/home/blinkfox/文档/test.doc"); out = new OutputStreamWriter(fos, "UTF-8"); String templatePath = PathKit.getRootClassPath() + "/template/"; handler.write(templatePath, "test.xml", testMap, out); System.out.println("Export successfully!"); } catch (Exception e) { e.printStackTrace (); } } }