java方法
public static ArrayList<String> getData(String in ,String name){ //in = in.replaceAll("\n",""); Pattern p = null; try { p = Pattern.compile(""+name+"\\s*=\\s*\\'\\w*\\S*\\s*\\'");//正则解析 } catch (Exception e) { e.printStackTrace(); } Matcher m = p.matcher(in); ArrayList<String> list = new ArrayList<String>(); //用List方便一些 while(m.find()){ String s[] = m.group().split("'"); if(s.length<=1){ s = " ' ' ".split("'"); //处理没有数据的情况 避免数组越界 } list.add(s[1]); } return list; }
然后是导出数据到Excel的方法(写Excel用的是jxl JAR包)
public static boolean expoertExcel(String res){//res便是传递给fusionchart的数据 如果是文件形式 可以通过输入流读取 ArrayList<String> capt = new ArrayList<String>(); ArrayList<String> seri = new ArrayList<String>(); ArrayList<String> setv = new ArrayList<String>(); ArrayList<String> name = new ArrayList<String>(); ArrayList<String> ysname = new ArrayList<String>(); capt = getData(res,"caption"); seri = getData(res,"seriesName"); setv = getData(res, "value"); name = getData(res, "name"); ArrayList<String> sw = getData(res, "xAxisName"); String xname = sw.get(0); ysname = getData(res, "yAxisName"); // jxl写 Excel File ex = new File("C:\\test.xls");//web项目的路径乱糟糟 运行的时候跑到tomcat的bin目录去了 先这样搞一下 if(ex.exists()){ ex.delete(); } try { if(!ex.exists()){ ex.createNewFile(); } } catch (Exception e) { e.printStackTrace(); } WritableWorkbook wk = null; try { wk = Workbook.createWorkbook(ex); } catch (IOException e) { e.printStackTrace(); } WritableSheet sheet = null; Label label = null; Number num=null; sheet = wk.createSheet(capt.get(0),0); label = new Label(0,0,capt.get(0)); try { sheet.addCell(label); } catch (RowsExceededException e1) { e1.printStackTrace(); } catch (WriteException e1) { e1.printStackTrace(); } int snum = seri.size(); int lnum = name.size(); int vnum = setv.size(); int[][] p = new int[lnum][vnum]; for(int i=0;i<snum;i++){ label = new Label(0,i,seri.get(i)); if(sheet != null){ try { sheet.addCell(label); } catch (Exception e) { e.printStackTrace(); } } for(int j=i*lnum;j<(i+1)*lnum;j++){ num = new Number(j-i*lnum+1,i,Double.parseDouble(setv.get(j))); try { sheet.addCell(num); } catch (RowsExceededException e) { e.printStackTrace(); } catch (WriteException e) { e.printStackTrace(); } } } for(int i=0;i<lnum;i++){ label = new Label(i+1,snum,name.get(i)+xname); try { sheet.addCell(label); } catch (Exception e) { e.printStackTrace(); } } label = new Label(0,snum+1,"数据单位是:"+ysname.get(0)); try { sheet.addCell(label); } catch (Exception e) { e.printStackTrace(); } try { wk.write(); } catch (IOException e) { e.printStackTrace(); } try { wk.close(); } catch (Exception e) { e.printStackTrace(); } if(ex.exists()){ System.out.println(ex.getPath()); System.out.println(ex.toURI()); return true; }else{ return false; } }//暂时还没想到好的思路 这算是一种笨方法吧 继续积累知识,找到更好的解决方法
捣鼓了几天的Dom4j 是可以解析fusionchart的xml文件的 代码如下:
import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import jxl.Workbook; import jxl.write.Label; import jxl.write.Number; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.io.SAXReader; public class TestDom4j { /** * @param args * author txz */ public static void main(String[] args) { test(); } public static void test(){ File file = new File("G:\\xml\\line.xml"); if(!file.exists()){ System.out.println("文件不存在"); return; } InputStream in = null; try { in = new FileInputStream(file); } catch (Exception e) { e.printStackTrace(); } SAXReader read = new SAXReader(); Document document = null; try { document = read.read(in); } catch (Exception e) { e.printStackTrace(); } String caption=""; List list = document.selectNodes("//chart/@caption");//获取caption属性值 Iterator it = list.iterator(); while (it.hasNext()) { Attribute cap = (Attribute)it.next(); caption = cap.getValue(); } System.out.println(caption); ArrayList<String> labelarList = new ArrayList<String>(); List labellist = document.selectNodes("//category/@label");//获取lable属性值 Iterator itl = labellist.iterator(); while (itl.hasNext()) { Attribute labelAttribute = (Attribute)itl.next(); labelarList.add(labelAttribute.getValue()); } for(int i=0;i<labelarList.size();i++){ System.out.println(labelarList.get(i));//测试一下 } ArrayList<String> dataArrayList = new ArrayList<String>(); List dataList = document.selectNodes("//dataset/@seriesName");//获取seriesName属性的值 Iterator itd = dataList.iterator(); while (itd.hasNext()) { Attribute dataAttribute = (Attribute)itd.next(); dataArrayList.add(dataAttribute.getValue()); } for(int i=0;i<dataArrayList.size();i++){ System.out.println(dataArrayList.get(i));//测试一下 } ArrayList<String> valueArrayList = new ArrayList<String>(); List valueList = document.selectNodes("//set/@value");//获取value属性的值 Iterator itv = valueList.iterator(); while (itv.hasNext()) { Attribute valueAttribute = (Attribute)itv.next(); valueArrayList.add(valueAttribute.getValue()); } for(int i=0;i<valueArrayList.size();i++){ System.out.println(valueArrayList.get(i));//测试一下 } //需要的数据都取到了 下边就可以写到Excel里了 File xls = new File("G:\\test.xls"); if(xls.exists()){ xls.delete(); } try { xls.createNewFile(); WritableWorkbook work = Workbook.createWorkbook(xls); WritableSheet sheet = work.createSheet(caption,2); int x=0,y=0,z=0; x = dataArrayList.size(); y = labelarList.size(); z = valueArrayList.size(); System.out.println(x+" "+y+" "+z); for(int i=0;i<x;i++){ Label data = new Label(0,i,dataArrayList.get(i)); sheet.addCell(data); for(int j=0;j<y;j++){ Double dou = Double.parseDouble(valueArrayList.get(i*y+j));// Number val = new Number(j+1,i,dou); sheet.addCell(val); } } for(int i=0;i<y;i++){ Label label = new Label(i+1,x,labelarList.get(i)); sheet.addCell(label); } work.write(); work.close(); } catch (Exception e) { e.printStackTrace(); } } }
下边是解析的fusionchart的xml文件
<chart caption='Daily Visits' subcaption='(from 8/6/2006 to 8/12/2006)' lineThickness='1' showValues='0' formatNumberScale='0' anchorRadius='2' divLineAlpha='20' divLineColor='CC3300' divLineIsDashed='1' showAlternateHGridColor='1' alternateHGridAlpha='5' alternateHGridColor='CC3300' shadowAlpha='40' labelStep="2" numvdivlines='5' chartRightMargin="35" bgColor='FFFFFF,CC3300' bgAngle='270' bgAlpha='10,10'> <categories > <category label='8/6/2006' /> <category label='8/7/2006' /> <category label='8/8/2006' /> <category label='8/9/2006' /> <category label='8/10/2006' /> <category label='8/11/2006' /> <category label='8/12/2006' /> </categories> <dataset seriesName='Offline Marketing' color='1D8BD1' anchorBorderColor='1D8BD1' anchorBgColor='1D8BD1'> <set value='1327' /> <set value='1826' /> <set value='1699' /> <set value='1511' /> <set value='1904' /> <set value='1957' /> <set value='1296' /> </dataset> <dataset seriesName='Search' color='F1683C' anchorBorderColor='F1683C' anchorBgColor='F1683C'> <set value='2042' /> <set value='3210' /> <set value='2994' /> <set value='3115' /> <set value='2844' /> <set value='3576' /> <set value='1862' /> </dataset> <dataset seriesName='Paid Search' color='2AD62A' anchorBorderColor='2AD62A' anchorBgColor='2AD62A'> <set value='850' /> <set value='1010' /> <set value='1116' /> <set value='1234' /> <set value='1210' /> <set value='1054' /> <set value='802' /> </dataset> <dataset seriesName='From Mail' color='DBDC25' anchorBorderColor='DBDC25' anchorBgColor='DBDC25'> <set value='541' /> <set value='781' /> <set value='920' /> <set value='754' /> <set value='840' /> <set value='893' /> <set value='451' /> </dataset> <styles> <definition> <style name='CaptionFont' type='font' size='12'/> </definition> <application> <apply toObject='CAPTION' styles='CaptionFont' /> <apply toObject='SUBCAPTION' styles='CaptionFont' /> </application> </styles> </chart>
需要说几句的是最开始学习dom4j写练习的时候一直在报错 费了些劲 原来简单的dom4j可不是就这一个jar包就能搞定的还要有 xerces.jar,jaxen-1.1.1.jar 后边写Excel又加上了 jxl.jar
不然就会报各种各样的错误。