在项目中要实现一键导出的功能,就是点击导出按钮,就能够把所有页面的Echarts图表导入到word文档中。要实现这个功能需要第一要在后台产生Echarts图表,二是要把Echart图表转换为图片格式放入到word文档中。
1.ECharts-2.1.8.jar(这个不是官方的,是个人封装的,具体去百度一下)还有具体代码实现后台生成echarts图表
2.导出word文档使用的包,本项目使用jacob插件具体去百度一下
3.下载phantomjs(本项目使用的是 phantomjs-2.1.1-windows 版本)
MyEclipse新建一个web项目(echarts) ,过程不再多说
倒入相关的jar包,以下是测试代码
public void test() throws IOException {
//地址:http://echarts.baidu.com/doc/example/line5.html
EnhancedOption option = new EnhancedOption();
option.legend("高度(km)与气温(°C)变化关系");
option.toolbox().show(true).feature(
Tool.mark,
Tool.dataView,
new MagicType(Magic.line, Magic.bar),
Tool.restore,
Tool.saveAsImage);
option.calculable(true);
option.tooltip().trigger(Trigger.axis).formatter("Temperature :
{b}km : {c}°C");
ValueAxis valueAxis = new ValueAxis();
valueAxis.axisLabel().formatter("{value} °C");
option.xAxis(valueAxis);
option.animation(false);
CategoryAxis categoryAxis = new CategoryAxis();
categoryAxis.axisLine().onZero(false);
categoryAxis.axisLabel().formatter("{value} km");
categoryAxis.boundaryGap(false);
categoryAxis.data(0, 10, 20, 30, 40, 50, 60, 70, 80);
option.yAxis(categoryAxis);
Line line = new Line();
line.smooth(true).name("高度(km)与气温(°C)变化关系")
.data(15, -50, -56.5, -46.5, -22.1, -2.5, -27.7, -55.7, -76.5)
.itemStyle().normal().lineStyle().shadowColor("rgba(0,0,0,0.4)");
option.series(line);
option.exportToHtml("line5.html");
option.print();
option.view();
//以上是生成echarts图表数据
//爬取图片
String q= LineTest5.getAjaxCotnent("");
//System.out.println(System.getProperty("java.library.path"));
//把图片放到已经做好的word模板中
WordBean wordbean = null;
try {
wordbean = new WordBean();
String contextPath = getClass().getResource("/").getFile().toString().replace("WEB-INF/classes/", "").replace("/", "\\").substring(1);
System.out.println(contextPath);
String inputDocPath =contextPath+"template\\testpng.doc";
System.out.println(inputDocPath);
String outputDocPath = inputDocPath.replace("testpng.doc", "line.doc");
// String wordName;
wordbean.openDocument(inputDocPath);
wordbean.saveAs(outputDocPath);
wordbean.replaceImage("png", contextPath+"template\\line5.png",300,300);
wordbean.replaceText("kkk", "你好");
wordbean.saveAs(outputDocPath);
wordbean.close();
wordbean.quit();
String path = outputDocPath ;
System.out.println(path);
}catch (Exception e){
wordbean.close();
wordbean.quit();
e.printStackTrace();
}
}
public static String getAjaxCotnent(String url) throws IOException {
Runtime rt =Runtime.getRuntime();
Process p = rt.exec("phantomjs.exe D:/phantomjs-2.1.1-windows/bin/line5.js ");
InputStream is = p.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuffer sbf = new StringBuffer();
String tmp = "";
while((tmp = br.readLine())!=null){
sbf.append(tmp);
}
System.out.println(sbf.toString());
return sbf.toString();
}
本项目是以前做的,间隔时间比较长,不明白的可以