这几天再弄一个报表,要统计一些信息最终的部分展示结果如下:
基本工具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