echarts图表与列表文字结合导出word文档_第1页
echarts图表与列表文字结合导出word文档_第2页
echarts图表与列表文字结合导出word文档_第3页
echarts图表与列表文字结合导出word文档_第4页
echarts图表与列表文字结合导出word文档_第5页
已阅读5页,还剩14页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

导出word文件需要jar包Echarts图表可以生成BASE64编码function inita(hotWords,word,xData,hotWordtwo)/ alert(xData);/$(#chart_wrapper).append(); 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图表 var myChart = ec.init(document.getElementById(word); var option = 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编码图片后要传到后台function AllAreaWord() var date =$(#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 =briefing/expword?date=+encodeURI(date); /后台将base64编码图片保存session中在执行导出word );后台处理将图片编码存到session中RequestMapping(value=exp)/简报生成echarts图表public String exp(Model model,HttpServletRequest request)String image1 = request.getParameter(image1);String image2 = request.getParameter(image2);String url = image1.split(,); String url2 = image2.split(,); String imageo=;String imaget=;if(image1!=&url.length1)imageo=url1;if(image2!=null&url2.length1)imaget=url21;request.getSession().setAttribute(SystemConstant.SESSION_IMGONE, imageo);request.getSession().setAttribute(SystemConstant.SESSION_IMGTWO, imaget);return waihu/generation;后台导出word先建一个类package com.ideal.mall.front.web.controller;import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import org.apache.poi.POIXMLDocument; import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; import org.apache.poi.xwpf.usermodel.XWPFTable; import org.apache.poi.xwpf.usermodel.XWPFTableCell; import org.apache.poi.xwpf.usermodel.XWPFTableRow; /* 适用于word 2007* poi 版本 3.7*/ public class WordUtil /* * 根据指定的参数值、模板,生成 word 文档 * param param 需要替换的变量 * param template 模板 */ public static CustomXWPFDocument generateWord(Map param, String template) CustomXWPFDocument doc = null; try OPCPackage pack = POIXMLDocument.openPackage(template); doc = new CustomXWPFDocument(pack); if (param != null & param.size() 0) /处理段落 List paragraphList = doc.getParagraphs(); processParagraphs(paragraphList, param, doc); /处理表格 Iterator it = doc.getTablesIterator(); while (it.hasNext() XWPFTable table = it.next(); List rows = table.getRows(); for (XWPFTableRow row : rows) List cells = row.getTableCells(); for (XWPFTableCell cell : cells) List paragraphListTable = cell.getParagraphs(); processParagraphs(paragraphListTable, param, doc); catch (Exception e) e.printStackTrace(); return doc; /* * 处理段落 * param paragraphList */ public static void processParagraphs(List paragraphList,Map param,CustomXWPFDocument doc) if(paragraphList != null & paragraphList.size() 0) for(XWPFParagraph paragraph:paragraphList) List runs = paragraph.getRuns(); for (XWPFRun run : runs) String text = run.getText(0); if(text != null) boolean isSetText = false; for (Entry entry : param.entrySet() String key = entry.getKey(); if(text.indexOf(key) != -1) isSetText = true; Object value = entry.getValue(); if (value instanceof String) /文本替换 text = text.replace(key, value.toString(); else if (value instanceof Map) /图片替换 text = text.replace(key, ); Map pic = (Map)value; int width = Integer.parseInt(pic.get(width).toString(); int height = Integer.parseInt(pic.get(height).toString(); int picType = getPictureType(pic.get(type).toString(); byte byteArray = (byte) pic.get(content); ByteArrayInputStream byteInputStream = new ByteArrayInputStream(byteArray); try int ind = doc.addPicture(byteInputStream,picType); doc.createPicture(ind, width , height,paragraph); catch (Exception e) e.printStackTrace(); if(isSetText) run.setText(text,0); /* * 根据图片类型,取得对应的图片类型代码 * param picType * return int */ private static int getPictureType(String picType) int res = CustomXWPFDocument.PICTURE_TYPE_PICT; if(picType != null) if(picType.equalsIgnoreCase(png) res = CustomXWPFDocument.PICTURE_TYPE_PNG; else if(picType.equalsIgnoreCase(dib) res = CustomXWPFDocument.PICTURE_TYPE_DIB; else if(picType.equalsIgnoreCase(emf) res = CustomXWPFDocument.PICTURE_TYPE_EMF; else if(picType.equalsIgnoreCase(jpg) | picType.equalsIgnoreCase(jpeg) res = CustomXWPFDocument.PICTURE_TYPE_JPEG; else if(picType.equalsIgnoreCase(wmf) res = CustomXWPFDocument.PICTURE_TYPE_WMF; return res; /* * 将输入流中的数据写入字节数组 * param in * return */ public static byte inputStream2ByteArray(InputStream in,boolean isClose) byte byteArray = null; try int total = in.available(); byteArray = new bytetotal; in.read(byteArray); catch (IOException e) e.printStackTrace(); finally if(isClose) try in.close(); catch (Exception e2) System.out.println(关闭流失败); return byteArray; 后台导出RequestMapping(value=exp)/简报生成echarts图表public String exp(Model model,HttpServletRequest request)String image1 = request.getParameter(image1);String image2 = request.getParameter(image2);String url = image1.split(,); String url2 = image2.split(,); String imageo=;String imaget=;if(image1!=&url.length1)imageo=url1;if(image2!=null&url2.length1)imaget=url21;request.getSession().setAttribute(SystemConstant.SESSION_IMGONE, imageo);request.getSession().setAttribute(SystemConstant.SESSION_IMGTWO, imaget);return waihu/generation;RequestMapping(value=expword)/简报生成ResponseBodypublic void expword(Map dataMap,Model model,HttpServletRequest request,HttpServletResponse response) throws ExceptionString userId = request.getParameter(userId);String date1 = request.getParameter(date); String imageo = (String) request.getSession().getAttribute(SystemConstant.SESSION_IMGONE);String imaget = (String) request.getSession().getAttribute(SystemConstant.SESSION_IMGTWO);Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE, -1); String yesterday = new SimpleDateFormat( yyyy-MM-dd).format(cal.getTime();if(date1=null)date1=yesterday;List zxlist = workListService.queryByWorkList(date1,咨询类);List qzlist = workListService.queryByWorkList(date1,求助投诉类);List rdlist = workListService.queryByconType(date1,5);Briefing briefing1 = new Briefing();Briefing briefing =briefingService.getBriefing(date1,);if(briefing=null)List list = workListService.queryByType(date1,);for(int i=0;ilist.size();i+)if(i=0)briefing1.setContentOne(list.get(i).toString();if(i=1)briefing1.setContentTwo(list.get(i).toString();briefing1.setContent();elsebriefing1=briefing;String aa= imageo.replaceAll( ,+);String bb= imaget.replaceAll( ,+);BASE64Decoder decoder = new BASE64Decoder(); byte a = decoder.decodeBuffer(aa);byte b = decoder.decodeBuffer(bb);testTemplateWrite(zxlist,qzlist,rdlist,briefing1,date1,a,b,response);public void testTemplateWrite(List zxlist,List qzlist,List rdlist,Briefing briefing,String date,byte image1,byte image2,HttpServletResponse response) throws Exception Map params = new HashMap(); String tomcatPath=System.getProperty(user.dir); Map header = new HashMap(); header.put(width, 550); header.put(height, 150); header.put(type, png); header.put(content, image1); params.put($image1,header); Map headers = new HashMap(); headers.put(width, 550); headers.put(height, 150); headers.put(type, png); headers.put(content, image2); params.put($image2,headers); params.put($date, date); params.put($content0, briefing.getContent(); params.put($content1, briefing.getContentOne(); params.put($content2, briefing.getContentTwo(); String filepath = ; if (tomcatPath.contains() filepath = tomcatPath.replace(bin, webappsarmy-infantryfileData) + rxjbmb.docx; InputStream is = new FileInputStream(filepath); CustomXWPFDocument doc = new CustomXWPFDocument(is); /替换表格里面的变量 if(zxlist.size()!=0) insertValueToTable(doc, generateTestData(zxlist),1,false); if(qzlist.size()!=0) insertValueToTable(doc, generateTestData(qzlist),2,true); if(rdlist.size()!=0) insertValueToTable(doc, generateTestDatas(rdlist),2,true); /替换段落里面的变量 List paragraphList = doc.getParagraphs(); WordUcessParagraphs(paragraphList, params, doc); String fileName =date+热线简报.docx; response.reset(); ServletOutputStream out = response.getOutputStream(); response.setContentType(application/vnd.ms-excel); response.setHeader(Content-disposition, attachment;filename= + new String(fileName.getBytes(GB2312), ISO-8859-1); BufferedInputStream bis = null; BufferedOutputStream bos = null; try bis = new BufferedInputStream(new FileInputStream(filepath); bos = new BufferedOutputStream(out); doc.write(out); catch (IOException e) throw e; finally if (bis != null) bis.close(); if (bos != null) bos.close(); this.close(is); public ListList generateTestData(List lists) ListList resultList = new ArrayListList(); for (int i = 0; i lists.size(); i+) List list = new ArrayList(); Object obj =(Object) lists.get(i); String obj6=0%; if(obj6!=null) obj6= obj6.toString()+%; String obj7=0%; if(obj7!=null) obj7= obj7.toString()+%; list.add(obj0.toString(); list.add(obj1.toString(); list.add(obj2.toString(); list.add(obj3.toString(); list.add(obj4.toString(); list.add(obj5.toString(); list.add(obj6); list.add(obj7); resultList.add(list); return resultList; public ListList generateTestDatas(List lists) ListList resultList = new ArrayListList(); for (int i = 0; i lists.size(); i+) List list = new ArrayList(); Object obj =(Object) lists.get(i); String obj3=0%; if(obj3!=null) obj3= obj3.toString()+%; list.add(obj0.toString(); list.add(obj1.toString(); list.add(obj2.toString(); list.add(obj3); resultList.add(list); return resultList; public void insertValueToTable(XWPFDocument doc, ListList resultList,int tableRowSize,boolean isDelTmpRow) throws Exception Iterator iter

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论