java word导出

需要导出的word文档是事先准备好的一个模板,文档数据所在的位置则必须按FreeMarker模板语法的占位符(如:${xxx})填充,然后将word文档保存(最好另存为,原模板也最后保留,便于以后修改)为xml格式的文件,然后使用文本编辑器打开检查并修改不合法或书写更好的FreeMarker语法。最后在后台服务端使用FreeMarker相关的包和类读取模板,返回模板所需的数据变量,输出word文件即可。
导出word功能示例

1、创建word模板

先创建你需要导出具体格式和样式的模板,数据用FreeMarker语法的占位符占据word模板
2、另存并编辑xml文件

创建好word模板之后,需要将word另存为xml格式的文件,这样易于查看和编辑为符合FreeMarker模板语法的xml文件,保证导出word数据的准确性,具体FreeMaker语法网上有很多,这里就不细说了。
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;

    /**
     * 构造方法
     */
    public WordUtil() {
        try {
            configuration = new Configuration();
            configuration.setDefaultEncoding("UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据类路径获取模板
     * @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;
    }

    /**
     * 生成word文档
     * @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 {

    /**
     * 构造测试数据
     * @return
     */
    public static Map<String, Object> createDatas() {
        Map<String, Object> testMap = new HashMap<String, Object>();
//      构造散数据
        testMap.put("author", "浙江火焰");
        testMap.put("date", "2015-11-20");

//      构造列表循环数据存放在ArrayList集合中
        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 {
//          生成test.doc的word文件到某文件路径下
            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("导出成功!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}


猜你喜欢

转载自skyfar666.iteye.com/blog/2308184