注明:最近很多朋友跟我要源碼,其實(shí)很簡(jiǎn)單的東西,自己嘗試一下很快就解決了,目前我在外地出差,不能提供源代碼下載,我不再傳源碼,請(qǐng)見(jiàn)諒。
在云南期間,收到幾個(gè)朋友的郵件,希望發(fā)工程代碼,因?yàn)槲以谠颇希瑳](méi)有帶本子,說(shuō)聲抱歉!
因?yàn)轫?xiàng)目需要,前幾天完成了fusioncharts的基本使用,但是還不能滿(mǎn)足項(xiàng)目需求。
項(xiàng)目中需要實(shí)現(xiàn)按照客戶(hù)的需求導(dǎo)出指定格式的文件,而且可以圖表混編等,這就需要自己去擴(kuò)展fusioncharts本身的導(dǎo)出。
下面簡(jiǎn)單介紹如何在服務(wù)器端使用JSP導(dǎo)出各種文件格式,并能自由擴(kuò)展。因?yàn)闀r(shí)間比較緊,寫(xiě)的很凌亂,希望大家理解。
在J2EE框架下用JSP導(dǎo)出文件
1.將 exportAtClient導(dǎo)出屬性設(shè)置為0(服務(wù)器導(dǎo)出),exportHandler設(shè)置為'exp.jsp'。
可以在chart.exportChart({exportFormat:'pdf',exportHandler:'exp.jsp',exportFileName:'test',exportParameters:'test'});設(shè)置,也可以在XML里設(shè)置。(注意在JS里設(shè)置會(huì)重寫(xiě)XML里面的導(dǎo)出屬性設(shè)置)
2.exp.jsp為處理數(shù)據(jù)的文件,可實(shí)現(xiàn)各種文件格式的導(dǎo)出。
<%@ page language="java" import="com.hjtp.incas.chart.*" pageEncoding="utf-8"%>
<%@ page import="com.hjtp.incas.chart.helper.*"%>
<%@ page import="com.hjtp.incas.chart.exporthelper.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>導(dǎo)出圖片</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<%
try
{
ExportHelperBean exportHelperBean=FusionChartsExportHelper.parseExportRequestStream(request);
ExportFactory exportFactory=ExportFactory.getInstance();
String type=(String)exportHelperBean.getExportParameterValue("exportformat");
String fileName = (String)exportHelperBean.getExportParameterValue("exportfilename");
ExportFormat exportFormat=new ExportFormat(type);
String contentType=exportFormat.getContentType();
String fix=exportFormat.getPostfix();
response.reset();
response.setContentType(contentType);
response.addHeader("Content-Disposition","attachment; filename=\""+fileName+"."+fix+"\"");
java.io.OutputStream os=response.getOutputStream();
IFusionCharts iFusionCharts=exportFactory.getFusionCharts(type);
iFusionCharts.export(exportHelperBean,os);
os.flush();
os.close();
out.clear();
out=pageContext.pushBody();
}
catch (Exception e)
{
e.printStackTrace();
}
%>
</body>
</html>
3.如何擴(kuò)展導(dǎo)出的文件格式為項(xiàng)目所需。
1.自定義導(dǎo)出
將服務(wù)器端導(dǎo)出參數(shù)設(shè)為exportHandler:'exp.jsp',即為使用我們自己
寫(xiě)的導(dǎo)出方式導(dǎo)出。
2.如何擴(kuò)展自定義導(dǎo)出
(1)在exp.jsp中,可以看到如下兩行代碼
IFusionCharts iFusionCharts=exportFactory.getFusionCharts(type);
iFusionCharts.export(exportHelperBean,os);
其中exportFactory為一個(gè)工廠類(lèi),起分發(fā)器的作用,通過(guò)此類(lèi)可以根據(jù)
擴(kuò)展實(shí)際需求進(jìn)行自定義擴(kuò)展。
(2)自定義擴(kuò)展實(shí)現(xiàn) ,例如像實(shí)現(xiàn)對(duì)txt文件格式支持的導(dǎo)出,需要在新增加
一個(gè)類(lèi)實(shí)現(xiàn)IFusionCharts 接口的export方法即可,同時(shí)在exportFactory
工廠類(lèi)里添加對(duì)此支持的判斷分發(fā)實(shí)現(xiàn)即可。