资源描述
echarts图表与列表文字结合导出word文档.docx
《echarts图表与列表文字结合导出word文档.docx》由会员分享,可在线阅读,更多相关《echarts图表与列表文字结合导出word文档.docx(29页珍藏版)》请在冰豆网上搜索。
echarts图表与列表文字结合导出word文档
导出word文件需要jar包
Echarts图表可以生成BASE64编码
functioninita(hotWords,word,xData,hotWordtwo)
{
//alert(xData);
//$("#chart_wrapper").append('180px"class="pro_zline"id="pro_zline'+i+'">
');
require.config({
paths:
{
'echarts':
'js/echarts',
'echarts/chart/line':
'js/echarts',//饼状图
'echarts/chart/bar':
'js/echarts'//柱状图
}
});
require(
[
'echarts',
'echarts/chart/line',//按需加载所需图表,如需动态类型切换功能,别忘了同时加载相应图表
'echarts/chart/bar'
],
function(ec){
//基于准备好的dom,初始化echarts图表
varmyChart=ec.init(document.getElementById(word));
varoption={
grid:
{
},
tooltip:
{
trigger:
'axis'
},
toolbox:
{
show:
true,
feature:
{
mark:
{show:
true},
dataView:
{show:
true,readOnly:
false},
magicType:
{show:
true,type:
['line','bar']},
restore:
{show:
true},
saveAsImage:
{show:
true}
}
},
calculable:
true,
animation:
false,
legend:
{
data:
['来电量','昨日数据对比']
},
xAxis:
[
{
type:
'category',
data:
xData
}
],
yAxis:
[
{
type:
'value',
name:
'来电量',
axisLabel:
{
formatter:
'{value}'
}
},
{
type:
'value',
name:
'昨日数据对比',
axisLabel:
{
formatter:
'{value}%'
}
}
],
series:
[
{
name:
'来电量',
type:
"bar",
data:
getPlatData(hotWords)
},
{
name:
'昨日数据对比',
type:
"line",
yAxisIndex:
1,
data:
getPlatData(hotWordtwo)
}
]
};
//为echarts对象加载数据
myChart.setOption(option);
$("#image1").val(myChart.getDataURL('png'));
//alert(myChart.getDataURL('png'))//获取base64编码
}
);
}
获取base64编码图片后要传到后台
functionAllAreaWord()
{vardate=$("#year").val();
$.ajax({
type:
'POST',
url:
'briefing/exp',//用户请求数据的URL
data:
"&date="+date+"&image1="+$("#image1").val()+"&image2="+$("#image2").val(),
beforeSend:
function(){changeImg();},
error:
function(XMLHttpRequest,textStatus,errorThrown){
alert(textStatus);
},
success:
function(data){
window.location.href="<%=basePath%>briefing/expword?
date="+encodeURI(date);//后台将base64编码图片保存session中在执行导出word
}
});
}
后台处理将图片编码存到session中
@RequestMapping(value="exp")//简报生成echarts图表
publicStringexp(Modelmodel,HttpServletRequestrequest){
Stringimage1=request.getParameter("image1");
Stringimage2=request.getParameter("image2");
String[]url=image1.split(",");
String[]url2=image2.split(",");
Stringimageo="";
Stringimaget="";
if(image1!
=""&&url.length>1){
imageo=url[1];
}
if(image2!
=null&&url2.length>1){
imaget=url2[1];
}
request.getSession().setAttribute(SystemConstant.SESSION_IMGONE,imageo);
request.getSession().setAttribute(SystemConstant.SESSION_IMGTWO,imaget);
return"waihu/generation";
}
后台导出word
先建一个类
packagecom.ideal.mall.front.web.controller;
importjava.io.ByteArrayInputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.util.Iterator;
importjava.util.List;
importjava.util.Map;
importjava.util.Map.Entry;
importorg.apache.poi.POIXMLDocument;
importorg.apache.poi.openxml4j.opc.OPCPackage;
importorg.apache.poi.xwpf.usermodel.XWPFParagraph;
importorg.apache.poi.xwpf.usermodel.XWPFRun;
importorg.apache.poi.xwpf.usermodel.XWPFTable;
importorg.apache.poi.xwpf.usermodel.XWPFTableCell;
importorg.apache.poi.xwpf.usermodel.XWPFTableRow;
/**
*适用于word2007
*poi版本3.7
*/
publicclassWordUtil{
/**
*根据指定的参数值、模板,生成word文档
*@paramparam需要替换的变量
*@paramtemplate模板
*/
publicstaticCustomXWPFDocumentgenerateWord(Mapparam,Stringtemplate){
CustomXWPFDocumentdoc=null;
try{
OPCPackagepack=POIXMLDocument.openPackage(template);
doc=newCustomXWPFDocument(pack);
if(param!
=null&¶m.size()>0){
//处理段落
ListparagraphList=doc.getParagraphs();
processParagraphs(paragraphList,param,doc);
//处理表格
Iteratorit=doc.getTablesIterator();
while(it.hasNext()){
XWPFTabletable=it.next();
Listrows=table.getRows();
for(XWPFTableRowrow:
rows){
Listcells=row.getTableCells();
for(XWPFTableCellcell:
cells){
ListparagraphListTable=cell.getParagraphs();
processParagraphs(paragraphListTable,param,doc);
}
}
}
}
}catch(Exceptione){
e.printStackTrace();
}
returndoc;
}
/**
*处理段落
*@paramparagraphList
*/
publicstaticvoidprocessParagraphs(ListparagraphList,Mapparam,CustomXWPFDocumentdoc){
if(paragraphList!
=null&¶graphList.size()>0){
for(XWPFParagraphparagraph:
paragraphList){
Listruns=paragraph.getRuns();
for(XWPFRunrun:
runs){
Stringtext=run.getText(0);
if(text!
=null){
booleanisSetText=false;
for(Entryentry:
param.entrySet()){
Stringkey=entry.getKey();
if(text.indexOf(key)!
=-1){
isSetText=true;
Objectvalue=entry.getValue();
if(valueinstanceofString){//文本替换
text=text.replace(key,value.toString());
}elseif(valueinstanceofMap){//图片替换
text=text.replace(key,"");
Mappic=(Map)value;
intwidth=Integer.parseInt(pic.get("width").toString());
intheight=Integer.parseInt(pic.get("height").toString());
intpicType=getPictureType(pic.get("type").toString());
byte[]byteArray=(byte[])pic.get("content");
ByteArrayInputStreambyteInputStream=newByteArrayInputStream(byteArray);
try{
intind=doc.addPicture(byteInputStream,picType);
doc.createPicture(ind,width,height,paragraph);
}catch(Exceptione){
e.printStackTrace();
}
}
}
}
if(isSetText){
run.setText(text,0);
}
}
}
}
}
}
/**
*根据图片类型,取得对应的图片类型代码
*@parampicType
*@returnint
*/
privatestaticintgetPictureType(StringpicType){
intres=CustomXWPFDocument.PICTURE_TYPE_PICT;
if(picType!
=null){
if(picType.equalsIgnoreCase("png")){
res=CustomXWPFDocument.PICTURE_TYPE_PNG;
}elseif(picType.equalsIgnoreCase("dib")){
res=CustomXWPFDocument.PICTURE_TYPE_DIB;
}elseif(picType.equalsIgnoreCase("emf")){
res=CustomXWPFDocument.PICTURE_TYPE_EMF;
}elseif(picType.equalsIgnoreCase("jpg")||picType.equalsIgnoreCase("jpeg")){
res=CustomXWPFDocument.PICTURE_TYPE_JPEG;
}elseif(picType.equalsIgnoreCase("wmf")){
res=CustomXWPFDocument.PICTURE_TYPE_WMF;
}
}
returnres;
}
/**
*将输入流中的数据写入字节数组
*@paramin
*@return
*/
publicstaticbyte[]inputStream2ByteArray(InputStreamin,booleanisClose){
byte[]byteArray=null;
try{
inttotal=in.available();
byteArray=newbyte[total];
in.read(byteArray);
}catch(IOExceptione){
e.printStackTrace();
}finally{
if(isClose){
try{
in.close();
}catch(Exceptione2){
System.out.println("关闭流失败");
}
}
}
returnbyteArray;
}
}
后台导出
@RequestMapping(value="exp")//简报生成echarts图表
publicStringexp(Modelmodel,HttpServletRequestrequest){
Stringimage1=request.getParameter("image1");
Stringimage2=request.getParameter("image2");
String[]url=image1.split(",");
String[]url2=image2.split(",");
Stringimageo="";
Stringimaget="";
if(image1!
=""&&url.length>1){
imageo=url[1];
}
if(image2!
=null&&url2.length>1){
imaget=url2[1];
}
request.getSession().setAttribute(SystemConstant.SESSION_IMGONE,imageo);
request.getSession().setAttribute(SystemConstant.SESSION_IMGTWO,imaget);
return"waihu/generation";
}
@RequestMapping(value="expword")//简报生成
@ResponseBody
publicvoidexpword(MapdataMap,Modelmodel,HttpServletRequestrequest,HttpServletResponseresponse)throwsException{
StringuserId=request.getParameter("userId");
Stringdate1=request.getParameter("date");
Stringimageo=(String)request.getSession().getAttribute(SystemConstant.SESSION_IMGONE);
Stringimaget=(String)request.getSession().getAttribute(SystemConstant.SESSION_IMGTWO);
Calendarcal=Calendar.getInstance();
cal.add(Calendar.DATE,-1);
Stringyesterday=newSimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
if(date1==null){
date1=yesterday;
}
Listzxlist=workListService.queryByWorkList(date1,"咨询类");
Listqzlist=workListService.queryByWorkList(date1,"求助投诉类");
Listrdlist=workListService.queryByconType(date1,"5");
Briefingbriefing1=newBriefing();
Briefingbriefing=briefingService.getBriefing(date1,"");
if(briefing==null){
Listlist=workListService.queryByType(date1,"");
for(inti=0;iif(i==0){
briefing1.setContentOne(list.get(i).toString());
}if(i==1){
briefing1.setContentTwo(list.get(i).toString());
}
}
briefing1.setContent("");
}else{
briefing1=briefing;
}
Stringaa=imageo.replaceAll("","\\+");
Stringbb=imaget.replaceAll("","\\+");
BASE64Decoderdecoder=newBASE64Decoder();
byte[]a=decoder.decodeBuffer(aa);
byte[]b=decoder.decodeBuffer(bb);
testTemplateWrite(zxlist,qzlist,rdlist,briefing1,date1,a,b,response);
}
publicvoidtestTemplateWrite(Listzxlist,Listqzlist,Listrdlist,Briefingbriefing,Stringdate,byte[]image1,byte[]image2,HttpServletResponseresponse)throwsException{
Mapparams=newHashMap();
StringtomcatPath=System.getProperty("user.dir");
Mapheader=newHashMap();
header.put("width",550);
header.put("height",150);
header.put("type","png");
header.put("content",image1);
params.put("${image1}",header);
Mapheaders=newHashMap();
headers.put("width",550);
headers.put("height",150);
headers.put("type","png");
headers.put("content",image2);
params.put("