java word export

The word document to be exported is a template prepared in advance, and the location of the document data must be filled with the placeholders of the FreeMarker template syntax (eg: ${xxx}), and then save the word document (preferably save as, original The template is also retained at the end for later modification) as a file in xml format, and then use a text editor to open it to check and modify the illegal or write better FreeMarker syntax. Finally, use FreeMarker-related packages and classes on the background server to read the template, return the data variables required by the template, and output the word file.
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 ();
        }
    }

}


Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326553822&siteId=291194637