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

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

基本工具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.*;
import java.util.List;

/**
 * Created by lenovo on 2016/9/28.
 */
public class JfreeChartUtils {
    private static Map<String
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值