echarts图表与列表文字结合导出word文档
导出word文件需要jar包
Echarts图表可以生成BASE64编码
function inita(hotWords,word,xData,hotWordtwo)
{
ppend('
id="pro_zline'+i+'">
({
paths: {
'echarts' : 'js/echarts',
'echarts/chart/line' : 'js/echarts', al('png'));
al();
$.ajax({
type:'POST',
url:'briefing/exp', al()+"&image2="+$("#image2").val(),
beforeSend:function () { changeImg(); },
error:function (XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus);
},
success: function (data) {
="<%=basePath%>briefing/expword?date="+encodeURI(date); etAttribute, imageo);
().setAttribute, imaget);
return"waihu/generation";
}
后台导出word
先建一个类
package
import
import
import
import
import
import
import
import
import
import
import
import
import
/**
* 适用于word 2007
* poi 版本
*/
public class WordUtil {
/**
* 根据指定的参数值、模板,生成 word 文档
* @param param 需要替换的变量
* @param template 模板
*/
public static CustomXWPFDocument generateWord(Map
CustomXWPFDocument doc = null;
try {
OPCPackage pack = (template);
doc = new CustomXWPFDocument(pack);
if (param != null && () > 0) {
oString());
int height = ("height").toString());
int picType = getPictureType("type").toString());
byte[] byteArray = (byte[]) ("content");
ByteArrayInputStream byteInputStream = new ByteArrayInputStream(byteArray);
try {
int ind = (byteInputStream,picType);
(ind, width , height,paragraph);
} catch (Exception e) {
();
}
}
}
}
if(isSetText){
(text,0);
}
}
}
}
}
}
/**
* 根据图片类型,取得对应的图片类型代码
* @param picType
* @return int
*/
private static int getPictureType(String picType){
int res = ;
if(picType != null){
if("png")){
res = ;
}else if("dib")){
res = ;
}else if("emf")){
res = ;
}else if("jpg") || ("jpeg")){
res = ;
}else if("wmf")){
res = ;
}
}
return res;
}
/**
* 将输入流中的数据写入字节数组
* @param in
* @return
*/
public static byte[] inputStream2ByteArray(InputStream in,boolean isClose){ byte[] byteArray = null;
try {
int total = ();
byteArray = new byte[total];
(byteArray);
} catch (IOException e) {
();
}finally{
if(isClose){
try {
();
} catch (Exception e2) {
"关闭流失败");
}
}
}
return byteArray;
}
}
后台导出
@RequestMapping(value="exp")etAttribute, imageo);
().setAttribute, imaget);
return"waihu/generation";
}
@RequestMapping(value="expword")etAttribute;
String imaget = (String) ().getAttribute;
Calendar cal = ();
, -1);
String yesterday = new SimpleDateFormat( "yyyy-MM-dd").format());
if(date1==null){
date1=yesterday;
}
List zxlist = (date1,"咨询类");
List qzlist = (date1,"求助投诉类");
List rdlist = (date1,"5");
Briefing briefing1 = new Briefing();
Briefing briefing =(date1,"");
if(briefing==null){
List list = (date1,"");
for(int i=0;i<();i++){
if(i==0){
(i).toString());
}if(i==1){
(i).toString());
}
}
("");
}else{
briefing1=briefing;
}
String aa= (" ","\\+");
String bb= (" ","\\+");
BASE64Decoder decoder = new BASE64Decoder();
byte[] a = (aa);
byte[] b = (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
String tomcatPath=("");
Map
("width", 550);
("height", 150);
("type", "png");
("content", image1);
("${image1}",header);
Map
("width", 550);
("height", 150);
("type", "png");
("content", image2);
("${image2}",headers);
("${date}", date);
("${content0}", ());
("${content1}", ());
("${content2}", ());
String filepath = "";
if ("\\")) {
filepath = ("\\bin", "\\webapps\\army-infantry\\fileData\\") + "";
}
InputStream is = new FileInputStream(filepath);
CustomXWPFDocument doc = new CustomXWPFDocument(is);
ocx";
();
ServletOutputStream out = ();
("application/");
("Content-disposition", "attachment;filename=" + new String("GB2312"), "ISO-8859-1"));
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream(filepath));
bos = new BufferedOutputStream(out);
(out);
} catch (IOException e) { throw e;
} finally {
if (bis != null)
();
if (bos != null)
();
}
(is);
}
public List> generateTestData(List lists) { List
> resultList = new ArrayList
>();
for (int i = 0; i < (); i++) {
List
Object[] obj =(Object[]) (i);
String obj6="0%";
if(obj[6]!=null){
obj6= obj[6].toString()+"%";
}
String obj7="0%";
if(obj[7]!=null){
obj7= obj[7].toString()+"%";
}
(obj[0].toString());
(obj[1].toString());
(obj[2].toString());
(obj[3].toString());
(obj[4].toString());
(obj[5].toString());
(obj6);
(obj7);
(list);
}
return resultList;
}
public List> generateTestDatas(List lists) { List
> resultList = new ArrayList
>();
for (int i = 0; i < (); i++) {
List
Object[] obj =(Object[]) (i);
String obj3="0%";
if(obj[3]!=null){
obj3= obj[3].toString()+"%";
}
(obj[0].toString());
(obj[1].toString());
(obj[2].toString());
(obj3);
(list);
}
return resultList;
}
public void insertValueToTable(XWPFDocument doc,
List> resultList,int tableRowSize,boolean isDelTmpRow) throws Exception {
Iterator
XWPFTable table = null;
List
List
List
flag=true;
break;
}
}
}
if(!flag){
return;
}
tmpCells=();
for(int i=0,len=();i XWPFTableRow row=(); ()); List cells=(); etW().getW()); } etVal().getVal()); ize()>0){ CTP ctp=().get(0); if()!=null){ if().getJc()!=null){ ().get(0).addNewPPr().addNewJc().setVal().getJc().getVal()); } } } et(0); XWPFParagraph cellP=().get(0); XWPFRun tmpR =null; if()!=null&&().size()>0){ tmpR=().get(0); XWPFRun cellR = (); (text); sSetRPr()){ CTRPr tmpRPr =().getRPr(); if()){ CTFonts tmpFonts=(); CTRPr cellRPr=().isSetRPr() ? ().getRPr() : ().addNewRPr(); CTFonts cellFonts = () ? () : (); ()); ()); ()); ()); ()); ()); ()); ()); } } } } etPPr()!=null){ CTPPr tmpPPr = ().getPPr(); CTPPr cellPPr = ().getPPr() != null ? ().getPPr() : ().addNewPPr(); ?)\\}", ; Matcher matcher = (str); return matcher; } /** * 关闭输入流 * @param is */ private void close(InputStream is) { if (is != null) { try { (); } catch (IOException e) { (); } } } 模版 导出的文件