用j产生xl文件

来源:互联网 发布:郑州美工工资 编辑:程序博客网 时间:2024/04/27 23:25

package org.travel.xml;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.taglibs.standard.lang.jstl.ELEvaluator;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import com.opensymphony.oscache.util.StringUtil;
import com.sun.xml.internal.ws.util.xml.CDATA;

public class KuXuneachtravelMain {
 
 protected void executeCircuitry() throws SQLException{
  String path = File.separator + "travel_platform_hotel1111111" + File.separator;
  String sql="select    from  biao";
        //获取数据库连接
  DB db = new DB();
  Connection conn = db.getConnection();
  ResultSet rs = null;
  ResultSet rs1 = null;
  PreparedStatement ps = null;
        //线路类型
  int typ=0;
     //航班信息
  String flights1="";
  //酒店
  String hotes1="";
   //线路类型名字
  String circuitrytypename="";
  //交通
  String traffic="";
   //行程安排
  String journeydes="";
   //行程特色
  String circuitrydesc="";
  //服务标准
  String tipserver="";
  try {
   ps = conn.prepareStatement(sql);
   rs = ps.executeQuery();
  OutputFormat format = OutputFormat.createPrettyPrint();
  format.setEncoding("UTF-8");
  Document document = DocumentHelper.createDocument();
  //主节点
  Element dajiaxianlus = document.addElement("dujia_xianlus");
  int y=1;
  //此处进行循环
  while(rs.next()){
   System.out.println((y++)+"");
  //具体线路的节点
  Element dajiaxianlu = dajiaxianlus.addElement("dujia_xianlu");
  Element type=dajiaxianlu.addElement("type");
  typ=1;
  type.setText("1");
  circuitrytypename=rs.getString("circuitrytypename");
  //出游范围数字类型,(( 不可为空 )); 1=国内游 2=出境游 3=周边游  补充:"周边游"的产品不可生成为“国内游”产品,否则与出发地相同的目的地会被删除。
  Element rangetype=dajiaxianlu.addElement("rangetype");
  if(circuitrytypename.indexOf("出境游")!=-1||circuitrytypename.indexOf("港澳台游")!=-1){
   rangetype.setText("2");
  }else if(circuitrytypename.indexOf("省内游")!=-1){
   rangetype.setText("3");
  }else {
   rangetype.setText("1");
  }
  //出行方式,数字类型,(( 不可为空 )); 11=双飞  22=火车去火车回  33=客车去客车回 44=邮轮来回  12=飞机去火车回 13=飞机去客车回 14=飞机去邮轮回  21=火车去飞机回 23=火车去客车回 24=火车去邮轮回  31=客车去飞机回 32=客车去火车回 34=汽车去邮轮回  43=邮轮去客车回 42=邮轮去火车回 41=邮轮去飞机回  99=其他
  traffic=rs.getString("traffic");
  Element traffictype=dajiaxianlu.addElement("traffictype");
  if(traffic!=null&&traffic.equals("1")){
  traffictype.setText("22");
  }else if( traffic!=null && traffic.equals("2")){
   traffictype.setText("33");
  }else if(traffic!=null && traffic.equals("3")){
   traffictype.setText("11");
  }else if( traffic!=null&& traffic.equals("5")){
   traffictype.setText("44");
  }else{
   traffictype.setText("99");
  }
  //原网站的线路链接
  Element link=dajiaxianlu.addElement("link");
  link.addCDATA()  //线路唯一编号--------------已经超出范围
  Element code=dajiaxianlu.addElement("code");
  code.setText(rs.getString("t3code"));
  
  //【线路名称】字符串类型,(( 不可为空 )),长度不超过128个字符。
  Element title=dajiaxianlu.addElement("title");
  title.addCDATA(rs.getString("t3circuitryName"));
  
  //针对不同供应商不同线路的电话预约
  Element telephone=dajiaxianlu.addElement("telephone");
  telephone.setText("4007-917-917");
  //出发城市,出发城市的名称不要带“市”字
  Element fromcity=dajiaxianlu.addElement("fromcity");
   if(rs.getString("startplacename").indexOf("市")!=-1){
    fromcity.setText(rs.getString("startplacename").substring(0,rs.getString("startplacename").indexOf("市")));
   }else{
    fromcity.setText(rs.getString("startplacename"));
   }
  
  //线路起价
  Element price=dajiaxianlu.addElement("price");
  price.setText(rs.getString("maxprice"));
  //行程天数
  Element days=dajiaxianlu.addElement("days");
  days.setText(rs.getString("traveldays"));
  //行程天数的文字描述
  Element daysinfo=dajiaxianlu.addElement("daysinfo");
  daysinfo.addCDATA(rs.getString("traveldays")+"天");
  //【是否可延期·自由行特有字段】数字类型 ;1=可延期  2=不可延期
  Element isdelay=dajiaxianlu.addElement("isdelay");
  isdelay.setText("2");
  //【发团周期】字符串类型 。如:每周二发团、每月1-5号发团
  Element startdayinfo=dajiaxianlu.addElement("startdayinfo");
  startdayinfo.addCDATA("按周出发");
  //具体发团日期
  Element startdays=dajiaxianlu.addElement("startdays");
  startdays.setText(gettravelDays(rs.getString("t6gooutteamdate")));
  //行程安排或自由行的推荐行程的内容放到这里面
  Element routes=dajiaxianlu.addElement("routes");
   //无法将每一天的行程信息提取出来的“行程安排”可以只放到一个route中,pos为空,title中放“以下为具体行程安排:”字样
  for(int j=0;j<1;j++){
  Element route=routes.addElement("route");
  Element pos=route.addElement("pos");
  pos.setText("");
  Element title1=route.addElement("title");
  //此处要修改
  title1.addCDATA("以下为具体行程安排");
  Element description=route.addElement("description");
  //[行程描述]字符串类型;请一定要在描述中适当添加br和p两个html标签,可以增加页面展现的美观程度。
  if(rs.getBytes("t3journeydes")!=null){
  journeydes=new String(rs.getBytes("t3journeydes"));
  description.addCDATA(journeydes);
  }else{
   description.addCDATA("");
  }
  //今日游览景点
  Element sightnames=route.addElement("sightnames");
  sightnames.setText("请看具体行程安排中的");
  }
   //自由行的酒店描述信息,飞机描述信,自由行的特有字段
  if(typ==2){
   //航班信息
   if(!(flights1.equals(""))){
   Element flights=dajiaxianlu.addElement("flights");
   for(int k=0;k<2;k++){
   Element flight=flights.addElement("flight");
   //序号,描述飞机航班的排列顺序
   Element pos=flight.addElement("pos");
   pos.setText(k+1+"");
   //第几天
   Element title2=flight.addElement("title");
   title2.addCDATA("第"+(y+1)+"天");
   //起飞时间
   Element fromtime=flight.addElement("fromtime");
   fromtime.setText("6:50");
   //起飞机场
   Element fromairport=flight.addElement("fromairport");
   fromairport.addCDATA("北京首都国际机场");
   //到达时间
   Element totime=flight.addElement("totime");
   totime.setText("10:30");
   //到达机场
   Element toairport=flight.addElement("toairport");
   toairport.addCDATA("上海虹桥机场");
   //航空公司
   Element airline=flight.addElement("airline");
   airline.addCDATA("南方航空");
   //航班
   Element code1=flight.addElement("code");
   code1.setText("CZ6716");
   //机型
   Element model=flight.addElement("model");
   model.setText("737");
   //舱位
   Element seat=flight.addElement("seat");
   seat.addCDATA("经济舱");
   //【航班是否可更改】数字类型。 1=可改  2=不可更改
   Element ischange=flight.addElement("ischange");
   ischange.setText("1");
   }
  }else{
   Element flights=dajiaxianlu.addElement("flights");
   Element flight=flights.addElement("flight");
   Element description=flight.addElement("description");
   description.addCDATA("航班和飞机的具体描述信息");
   
  }
  //酒店--------下面的数据暂时没有用到
  if(!(hotes1.equals(""))){
   for(int k=0;k<2;k++){
    Element hotels=dajiaxianlu.addElement("hotels");
    Element hotel =hotels.addElement("hotel");
    Element pos=hotel.addElement("pos");
    pos.setText(k+1+"");
    Element title3=hotel.addElement("title");
    title3.addCDATA("第1晚,第3晚");
    //入住城市
    Element city=hotel.addElement("city");
    city.setText("上海");
    //入住酒店
    Element hotelname=hotel.addElement("hotelname");
    hotelname.addCDATA("浦东区速八酒店");
    //【酒店星级描述】字符串类型。描述酒店级别
    Element star=hotel.addElement("star");
    star.addCDATA("3星级");
    //【房间类型】字符串类型,房间类型描述如:标准间、总统套房、商务间
    Element type1=hotel.addElement("type");
    type1.addCDATA("标准间");
    //【酒店是否可更改】数字类型。 1=可改  2=不可更改
    Element ischange=hotel.addElement("ischange");
    ischange.setText("1");
   }
   
  }else{
   Element hotels=dajiaxianlu.addElement("hotels");
   Element hotel =hotels.addElement("hotel");
   Element description=hotel.addElement("description");
   description.addCDATA("对没有酒店具体信息的,可以提供酒店的描述信息,格式就按照这种格式提供。");
  }
  }
  //费用包含
    Element costin=dajiaxianlu.addElement("costin");
    if(rs.getBytes("t3serviceroutinedes")!=null){
   tipserver=new String(rs.getBytes("t3serviceroutinedes"));
   costin.addCDATA(tipserver);
   }else{
    costin.addCDATA("");
     }
    //费用不包含
    Element costout=dajiaxianlu.addElement("costout");
    if(rs.getBytes("t3serviceroutinedes")!=null){
   tipserver=new String(rs.getBytes("t3serviceroutinedes"));
   costout.addCDATA(tipserver);
   }else{
    costout.addCDATA("");
     }
    Element costinfo=dajiaxianlu.addElement("costinfo");
    costinfo.addCDATA("");
    Element presigndays=dajiaxianlu.addElement("presigndays");
    presigndays.setText("");
    Element tip_feature=dajiaxianlu.addElement("tip_feature");
    if(rs.getBytes("t3circuitrydesc")!=null){
   circuitrydesc=new String(rs.getBytes("t3circuitrydesc"));
   tip_feature.addCDATA(circuitrydesc);
   }else{
    tip_feature.addCDATA("");
   }
    Element tip_notice=dajiaxianlu.addElement("tip_notice");
    tip_notice.addCDATA("");
    Element tip_friendly=dajiaxianlu.addElement("tip_friendly");
    tip_friendly.addCDATA("");
    Element tip_present=dajiaxianlu.addElement("tip_present");
    tip_present.addCDATA("");
    //线路特色
    Element tip_service=dajiaxianlu.addElement("tip_service");
    if(rs.getBytes("t3serviceroutinedes")!=null){
   tipserver=new String(rs.getBytes("t3serviceroutinedes"));
   tip_service.addCDATA(tipserver);
   }else{
    tip_service.addCDATA("");
 }
    //自费项目,旅行团特有字段
    Element tip_nofreeitems=dajiaxianlu.addElement("tip_nofreeitems");
    tip_nofreeitems.addCDATA("");
    //  购物说明,旅行团特有字段
    Element tip_shoppinginfo=dajiaxianlu.addElement("tip_shoppinginfo");
    tip_shoppinginfo.addCDATA("");
    //是否支持在线预订
    Element isbook=dajiaxianlu.addElement("isbook");
    isbook.setText("1");
    sql = " select filepath from t_attachment where relationid='"
   + rs.getString("t3id") + "' and type='BIGPIC'";
       //取得数据
  PreparedStatement ps2 = conn.prepareStatement(sql);
  rs1 = ps2.executeQuery();
    Element imgurl=dajiaxianlu.addElement("imgurl");
    int rsNum  = 0;
    while(rs1.next()){
      rsNum++;
     imgurl.addCDATA()     }
    if(rsNum == 0){
     // 没数据在这做操作
     imgurl.addCDATA()    }
    if (ps2 != null) {
   ps2.close();
   ps2 = null;
  }
   
    Element sightnames=dajiaxianlu.addElement("sightnames");
    sightnames.setText("");
    Element tocitys=dajiaxianlu.addElement("tocitys");
    tocitys.setText("");
    Element  tags=dajiaxianlu.addElement("tags");
    tags.setText("");
    Element istop=dajiaxianlu.addElement("istop");
    istop.setText("1");
  
  }
  try {
   //生成文件
   //XMLWriter output = new XMLWriter(new FileOutputStream(new File(path+"kuxxml/eachtravelkx.xml")),format);
   XMLWriter output = new XMLWriter(new FileOutputStream(new File("C:/Program Files/apache-tomcat-6.0.32/webapps/travel_platform/kuxxml/eachtravelkx.xml")),format);
   output.write(document);
   output.close();
  } catch (IOException e) {
   System.out.println(e.getMessage());
  }
  
  }catch (Exception e) {
   e.printStackTrace();
  } finally {
   System.out.println("kuxun的任务调度完成!!!");
   try {
    if (rs1 != null) {
    rs1.close();
    rs1 = null;
      }
    if (rs != null) {
     rs.close();
     rs = null;
    }
    if (ps != null) {
     ps.close();
     ps = null;
    }
    if (conn != null) {
     conn.close();
     conn = null;
    }
   } catch (Exception e) {
    e.printStackTrace();
   }
  }
  }
 //对日前进行修改
 public static  String  gettravelDays(String test) {
  String newTest1[]=test.split(";");
  String newtest1="";
  for(int i=0;i<newTest1.length;i++){
   String str=newTest1[i].toString();
   str=str.substring(0,str.indexOf("("));
   if("".equals(newtest1)){
    newtest1=str;
   }else{
   newtest1=newtest1+","+str;
   }
   if(i==19){
    break;
   }
  }
  return newtest1;
 }
 /**
  * @param args
  * @throws SQLException
  */
 public static void main(String[] args) throws SQLException {
  KuXuneachtravelMain kuxun=new KuXuneachtravelMain();
  kuxun.executeCircuitry();

 }

}