freemarker+Jfreechart生成Word文档(含图片)

本文介绍了使用Freemarker和JFreechart生成包含折线图和饼图的Word文档过程,包括遇到的问题及解决办法。在创建Word模板时需要注意XML格式的选择,处理图片时需转换为base64编码,以及如何解决可能出现的标签配对错误、乱码等问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

     这几天再弄一个报表,要统计一些信息最终的部分展示结果如下:

基本工具freemarker,jfreechart

工程的部分结构如下


与生成Word有关的类主要有FreemarkerConfiguration和WordGenerator代码如下:

import com.bqs.ares.common.utils.CommonUtils;
import freemarker.template.Configuration;

import java.io.File;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by lenovo on 2016/9/27.
 */
public class FreemarkerConfiguration {
    private static Logger log = LoggerFactory.getLogger(FreemarkerConfiguration.class);
    private final static String filepath = "/freemarkerTemplate";
    private static Configuration configuration=null;
    public static Configuration getConfiguration(){
        if(configuration==null){
            configuration=new Configuration();
            try {
                configuration.setDirectoryForTemplateLoading(new File(CommonUtils.class.getResource(filepath).getFile()));
            }catch (Exception e){
                log.error(e.getMessage());
            }
        }
        return  configuration;
    }
}

import com.bqs.risk.dvp.common.PDFUtils.freemarker.FreemarkerConfiguration;
import freemarker.template.Configuration;
import freemarker.template.Template;

import java.io.*;
import java.util.Map;

/**
 * Created by lenovo on 2016/10/9.
 */
public class WordGenerator {
    /**
     * Generate html string.
     *
     * @param template   the name of freemarker teamlate.
     * @param variables  the data of teamlate.
     * @return htmlStr
     * @throws Exception
     */
    public static void generate(String template, Map<String,Object> variables, String htmlName) throws Exception{
        String basePath=HtmlGenerator.class.getResource("/").getPath()+"/freemarkerTemplate/word/";
        Configuration config = FreemarkerConfiguration.getConfiguration();
        config.setDefaultEncoding("UTF-8");
        Template tp = config.getTemplate(template);
        tp.setEncoding("UTF-8");

        String htmlPath=basePath+htmlName+".doc";
        File file = new File(htmlPath);
        if (!file.exists())
            file.createNewFile();
        Writer out = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream(file), "utf-8"));
        tp.process(variables, out);
        out.flush();
        out.close();
    }
}

用jfreeChart生成折线图和饼图的代码如下:

import com.bqs.risk.dvp.common.InfoPoint;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.ui.TextAnchor;

import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.text.NumberFormat;
import java.util.*;
impo
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值