// freemarker 工具类
package com.mx.travel.util;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Map;
import org.apache.struts2.ServletActionContext;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
@SuppressWarnings("deprecation")
public class WordHelper {
private Configuration configuration = null;
public WordHelper() {
configuration = new Configuration();
configuration.setDefaultEncoding("UTF-8");
}
/**
*
* @method : "../op/template" teamSightShop.ftl
* @author : ry_zhongy
* @date : 2016-9-6 下午4:26:27
* @param dataMap:参数 outUrl:生成文件路径 templatePath:.ftl路径 ftl:ftl名称
* @retrun : void
*/
@SuppressWarnings("rawtypes")
public void createDoc( Map dataMap,String outPath,String templatePath,String ftl) {
configuration.setClassForTemplateLoading(this.getClass(),templatePath);
Template template = null;
try {
template = configuration.getTemplate(ftl);
template.setEncoding("utf-8");
} catch (IOException e) {
e.printStackTrace();
}
// 输出文档路径及名称
File outFile = new File(outPath);
Writer out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"));
} catch (Exception e1) {
e1.printStackTrace();
}
// 关闭流
try {
template.process(dataMap, out);
out.close();
} catch (TemplateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @param dataMap
* @param outPath
* @param ftlName
* Nov 18, 2016
* xt
*/
@SuppressWarnings("rawtypes")
public void createDoc(Map dataMap,String outPath,String ftlName) {
String filePath = ServletActionContext.getServletContext().getRealPath("/template/op");
Template template = null;
try {
configuration.setDirectoryForTemplateLoading(new File(filePath));
template = configuration.getTemplate(ftlName);
template.setEncoding("utf-8");
}catch(NullPointerException e){
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
// 输出文档路径及名称
File outFile = new File(outPath);
Writer out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"));
} catch (Exception e1) {
e1.printStackTrace();
}
// 关闭流
try {
template.process(dataMap, out);
out.close();
} catch (TemplateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
=========================================
// sight.xml
<!-- 预览生成下载 -->
<action name="getTeamsRow" method="getTeamsRow" class="sightShopAction">
<result name="success">web/resourceCenter/sight/shop/showTeamsList.jsp</result>
</action>
<!-- 生成下载 -->
<action name="getTeamsDownload" method="getTeamsDownload" class="sightShopAction">
<result name="success">web/resourceCenter/sight/shop/loadDownFile.jsp</result>
</action>
//
/**
*
* @method : 商店预览下载信息
* @author : ry_zhongy
* @throws Exception
* @date : 2016-8-31 下午3:52:19
* @retrun : String
*/
public String getTeamsRow () throws Exception {
// 定义变量
StringBuffer sBuffer = new StringBuffer();
StringBuffer dayAndMothodSub = new StringBuffer();
String[] monthArry = {"",".JAN",".FEB",".MAR",".APR",".MAY",".JUN",".JUL",".AUG",".SEP",".OCT",".NOV",".DEC",".JAN"};
List<TeamSightShop> teamSightShopList = null;
List<Map<String, Object>> teamSightShopMap = new ArrayList<Map<String, Object>>();
Map<String, Object> mapObjMap =new HashMap<String, Object>();
List<Guide> guideList = new ArrayList<Guide>();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
List<String> dayAndMonthArry = new ArrayList<String>();
List<TeamSightShopLt> teamSightShopLtList = new ArrayList<TeamSightShopLt>();
List<Team> teamList = null;
Calendar calendar_arrive = Calendar.getInstance();
Calendar calendar_leave = Calendar.getInstance();
// 获取参数
HttpServletRequest request = super.getRequest();
String shopCnNameStr = this.getTitle();
String numPlan = request.getParameter("numPlan");
String proString = request.getParameter("productCode");
String proTeamId = request.getParameter("proTeamId");
String tsIds = request.getParameter("tsIds");
String checked = request.getParameter("checked");
// 下载全部
if (checked.equals("checked")) {
String shopId = request.getParameter("shopId");
List<TeamSightShop> tssList = null;
String resultSightShop = SightShopHelper.getSightShopById(Integer.parseInt(shopId));
SightShop sightShop = null;
if(WSUtil.isSuccess(resultSightShop)){
sightShop = JSON.parseObject(WSUtil.getResultData(resultSightShop),SightShop.class);
}
StringBuffer teamSightShopHql = getTeamSightShopHql(sightShop);
String jsonItemCount = WSUtil.getResultData(TeamSightShopHelper.findAll(teamSightShopHql.toString()));
if ( !Util.isEqual(jsonItemCount) ) {
tssList = JSON.parseArray(jsonItemCount, TeamSightShop.class);
if (tssList != null && tssList.size() > 0) {
for (TeamSightShop teamSightShop : tssList) {
tsIds = tsIds + String.valueOf(teamSightShop.getTsId())+",";
numPlan = numPlan + teamSightShop.getTeam().getNumPlan() + ",";
proString = proString + teamSightShop.getTeam().getProductCode() + ",";
proTeamId = proTeamId + String.valueOf(teamSightShop.getTeam().getTeamId()) + ",";
}
tsIds = tsIds.substring(0, tsIds.length()-1);
numPlan = numPlan.substring(0, numPlan.length()-1);
proString = proString.substring(0, proString.length()-1);
proTeamId = proTeamId.substring(0, proTeamId.length()-1);
}
}
}
String[] productCodeArr = proString.split(",");
String[] numPlanArry = numPlan.split(",");
sBuffer.append("("+tsIds+")");
String hql = " from TeamSightShop t where 1=1 and t.tsId in "+sBuffer.toString();
String teamSightShopAll = TeamSightShopHelper.findAll(hql);
if ( WSUtil.isSuccess(teamSightShopAll) ) {
// 加载日期以及月份
teamSightShopList = JSON.parseArray(WSUtil.getResultData(teamSightShopAll), TeamSightShop.class);
if (teamSightShopList != null && teamSightShopList.size() > 0) {
int j = 0 ;
for (TeamSightShop teamSightShop : teamSightShopList) {
TeamSightShopLt teamSightShopLt = new TeamSightShopLt();
Date arriveCityDate = format.parse(teamSightShop.getArriveCityDate());
Date LeaveCityDate = format.parse(teamSightShop.getLeaveCityDate());
calendar_arrive.setTime(arriveCityDate);
String arriveCityStr = String.valueOf(calendar_arrive.get(Calendar.DATE));
calendar_leave.setTime(LeaveCityDate);
String leaveCityStr = String.valueOf(calendar_leave.get(Calendar.DATE));
if (calendar_arrive.get(Calendar.MONTH) != calendar_leave.get(Calendar.MONTH)) {
dayAndMothodSub.append(arriveCityStr += monthArry[calendar_arrive.get(Calendar.MONTH)+1]);
dayAndMothodSub.append("-");
dayAndMothodSub.append(leaveCityStr += monthArry[calendar_leave.get(Calendar.MONTH)+1]);
} else {
dayAndMothodSub.append(arriveCityStr+"-"+leaveCityStr);
dayAndMothodSub.append(monthArry[calendar_leave.get(Calendar.MONTH)+1]);
}
dayAndMonthArry.add(dayAndMothodSub.toString());
teamSightShopLt.setDateMonth(dayAndMothodSub.toString());
teamSightShopLt.setTourescort(productCodeArr[j]);
teamSightShopLt.setGroupsize(numPlanArry[j]);
dayAndMothodSub.delete(0, dayAndMothodSub.length());
// 加载导游个数以及计算导游or领队数量
if (proTeamId.length() > 0 && proTeamId != null ) {
List<TeamSightShopLt> listT = new ArrayList<TeamSightShopLt>();
Map<String, Object> mapT = new HashMap<String, Object>();
List<GuideContact> guideContactList = null;
String hqlTeam = " from Team t where t.teamId ="+proTeamId.split(",")[j];
String teamObj = TeamHelper.findAll(hqlTeam);
if ( WSUtil.isSuccess(teamObj) ) {
teamList = JSONObject.parseArray(WSUtil.getResultData(teamObj), Team.class);
if ( teamList != null && teamList.size() > 0) {
for (Team team : teamList) {
List<TeamGuide> teamGuidesList = null;
//组合导游名称
String teamGuidesHql = " from TeamGuide t where t.team.teamId = '"+team.getTeamId()+"'";
String teamGuidesResult = TeamGuideHelper.findAll(teamGuidesHql);
if(WSUtil.isSuccess(teamGuidesResult)){
teamGuidesList = JSON.parseArray(WSUtil.getResultData(teamGuidesResult), TeamGuide.class);
}
if ( teamGuidesList.size() > 0 && teamGuidesList != null){
for (TeamGuide teamGuide : teamGuidesList) {
String hqlGuide = " from Guide g where g.isEnable=1 and g.guideCode ='"+teamGuide.getGuideCode().toString()+"'";
String guideFindObj = GuideHelper.findAll(hqlGuide);
if ( WSUtil.isSuccess(guideFindObj) ) {
guideList = JSONObject.parseArray(WSUtil.getResultData(guideFindObj), Guide.class);
if ( guideList != null && guideList.size() > 0 ) {
StringBuffer hqlGuideContact = new StringBuffer(" from GuideContact gu where 1=1 ");
for (Guide guide : guideList) {
TeamSightShopLt teamSightShopLtObj = new TeamSightShopLt();
String liString = "";
if( guide.getIsGuide()==true && guide.getIsLeader()== false ) {
liString="(导游)";
} else if ( guide.getIsGuide()==false && guide.getIsLeader()==true ) {
liString="(领队)";
} else if (guide.getIsGuide()==false && guide.getIsLeader()==true ) {
liString="";
} else {
liString="(导游/领队)";
}
teamSightShopLtObj.setGuidePYCnName(guide.getNameEn()+liString);
// 拼接字符串TOUR ESCORT
teamSightShopLtObj.setGuideHZCnName(guide.getNameCn());
hqlGuideContact.append(" and gu.guide.guideId="+guide.getGuideId());
String guideContactObj = GuideContactHelper.findAll(hqlGuideContact.toString());
if ( WSUtil.isSuccess(guideContactObj) ) {
guideContactList = JSONObject.parseArray(WSUtil.getResultData(guideContactObj), GuideContact.class);
if ( guideContactList.size() > 0 && guideContactList != null ) {
StringBuffer telStringBuffer = new StringBuffer();
for (GuideContact guideContact : guideContactList) {
telStringBuffer.append(guideContact.getTel()+"/");
}
telStringBuffer.delete(telStringBuffer.length()-1, telStringBuffer.length());
teamSightShopLtObj.setTel(telStringBuffer.toString());
}
}
liString="";
listT.add(teamSightShopLtObj);
}
}
}
// 加载团总数
if (numPlanArry != null && numPlanArry.length > 0) {
StringBuffer numPlanBuffer = new StringBuffer();
String lvOrTuanNum = teamGuidesList.size() == 0?"0":String.valueOf(teamGuidesList.size());
teamSightShopLt.setGroupsize(numPlanArry[j]);
teamSightShopLt.setGuideNumber(lvOrTuanNum);
numPlanBuffer.delete(0, numPlanBuffer.length());
guideList.clear();
}
}
} else {
teamSightShopLt.setGuideNumber("0");
}
}
}
}
mapT.put("guideMap", listT);
teamSightShopLt.setMap(mapT);
}
teamSightShopLtList.add(teamSightShopLt);
j++;
}
}
}
mapObjMap.put("teamSightShopMap", teamSightShopLtList);
teamSightShopMap.add(mapObjMap);
// 设置request返回值
setRequestAttribute("teamSightShopList",teamSightShopMap);
setRequestAttribute("shopCnNameStr",shopCnNameStr);
return SUCCESS;
}
/**
*
* @method : 下载word
* @author : ry_zhongy
* @date : 2016-9-5 下午3:06:49
* @retrun : String
*/
public String getTeamsDownload () {
// 定义参数
List<Map<String, Object>> returnList = new ArrayList<Map<String, Object>>();
Map<String, Object> dataMap = new HashMap<String, Object>();
// 获取参数
HttpServletRequest request = super.getRequest();
String shopCnNameStr = request.getParameter("shopCnNameStr");
dataMap.put("parTitle", shopCnNameStr);
String team = request.getParameter("teamArray");
String[] teamArry = team.split("\\.\\.\\.");
for (int n=0; n < teamArry.length; n++) {
List<Map<String, Object>> guidesList = new ArrayList<Map<String, Object>>();
Map<String, Object> returnMap = new HashMap<String, Object>();
String[] strArrStrings = teamArry[n].split(";");
returnMap.put("parShopCode", strArrStrings[0].replace(",", ""));
returnMap.put("parMonth", strArrStrings[1]);
returnMap.put("parGuides", strArrStrings[2]);
returnMap.put("guideNumber", strArrStrings[3].equals("")?"0":strArrStrings[3]);
String guideStr = teamArry[n].substring(teamArry[n].indexOf("\\")+1, teamArry[n].length());
String[] guideArry = guideStr.split("\\\\");
for (int g = 0 ; g < guideArry.length ; g ++) {
String[] guidStrings = guideArry[g].split(";");
for ( int k = 0 ; k < 3 ; ) {
Map<String, Object> guideMap = new HashMap<String, Object>();
guideMap.put("guidePYCnName", guidStrings[0].equals("false")?"":guidStrings[0].trim()); // 导游英文名称
guideMap.put("guideHZCnName", guidStrings[1].equals("false")?"":guidStrings[1].trim()); // 导游中文名称
guideMap.put("tel", guidStrings[2].equals("false")?"":guidStrings[2].trim()); // 导游电话名称
guidesList.add(guideMap);
break;
}
}
returnMap.put("guideMap", guidesList);
returnList.add(returnMap);
}
dataMap.put("returnList", returnList);
WordHelper wordHelper = new WordHelper();
wordHelper.createDoc(dataMap, request.getSession().getServletContext().getRealPath("/")+"template/ba/商店商品目录模板.doc","teamSightShopTemplate.ftl");
return SUCCESS;
}
// 对应的jsp
<%@ page contentType="text/html; charset=utf-8" language="java" errorPage="/common/error.jsp" %>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML>
<html>
<head>
<s:include value="/common/basePath.jsp" />
<META HTTP-EQUIV="pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-revalidate">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>预览下载</title>
<script type="text/javascript" src="js/common/search.js"></script>
<script type="text/javascript" src="js/jquery/jquery.min.js"></script>
<script type="text/javascript" src="plugin/extjs/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="plugin/extjs/ext-all.js"></script>
<script type="text/javascript" src="js/common/popWin.js"></script>
<script type="text/javascript" src="js/common/popApp.js"></script>
</head>
<style type="text/css">
body{
background-color:gray;
font-family: sans-serif;
}
tr{
height:30px;
}
td{
font-size: 11px;
}
.container{
border-radius: 2px;
min-height: 760px;
padding:30px 60px;
}
.com_top{
background-color: #fff;
margin: 20px auto;
width: 740px;
height: auto;
}
.divContBody{
padding-top: 10px;
}
#float_banner{
position:absolute;
top:0;
left:50%;
width:900px;
margin-right: 0px;
height:30px;
line-height:30px;
text-align:center;
font-size:14px;
font-weight:bold;
z-index:2;
}
button{
background-color: #6e7eff;
}
</style>
<script type="text/javascript">
var speed = 100;
var scrollTop = null;
var hold = 0;
var float_banner;
var pos = "";
var timer = null;
var moveHeight = null;
window.οnscrοll=scroll_ad;
function scroll_ad(){
float_banner = document.getElementById("float_banner");
scrollTop = document.documentElement.scrollTop+document.body.scrollTop;
pos = scrollTop - float_banner.offsetTop;
pos = pos/10
moveHeight = pos>0?Math.ceil(pos):Math.floor(pos);
if(moveHeight!=0){
float_banner.style.top = float_banner.offsetTop+moveHeight+"px";
setTimeout(scroll_ad,speed);
}
}
</script>
<script type="text/javascript">
function getTeamsDownload (){
var teamArray = [];
var guideArry = [];
var rows = $(".coinName").parent();
var shopCnNameStr = document.getElementById("shopCnNameStr").value; // 获取商店信息
for (var i = 0 ; i < rows.length ; i++) {
var guidesList = "";
var productCode = $(rows[i]).children("td").eq(3).children("input")[0].value; // 获取TOUR CODE
var shopCnName = $(rows[i]).children("td").eq(3).children("input")[1].value; // 获取Date
var groupsize = $(rows[i]).children("td").eq(2).children("input")[0].value; // 获取GROUP SIZE 团总数
var guideNumber = $(rows[i]).children("td").eq(2).children("input")[1].value; // 获取GROUP SIZE 导游总数
var spans = $(rows[i]).children("td").eq(4).children("span"); // 获取导游数量
for (var j = 0 ; j < spans.length; j++) {
var guides = "";
var guidePYCnName = $(spans[j]).children("input")[0].value; // 获取导游英文名称
guidePYCnName = (guidePYCnName==""?"false":guidePYCnName);
var guideHZCnName = $(spans[j]).children("input")[1].value; // 获取导游中文名称
guideHZCnName = (guideHZCnName==""?"false":guideHZCnName);
var tel = $(spans[j]).children("input")[2].value; // 获取导游电话
tel = (tel==""?"false":tel);
guides = guidePYCnName+";"+guideHZCnName+";"+tel+";";
guidesList = guidesList +"\\"+ guides;
}
if (spans.length < 1) {
guides = "false;false;false;";
guidesList = guidesList +"\\"+ guides;
}
var arry = productCode+";"+shopCnName+";"+groupsize+";"+guideNumber+";"+ guidesList;
shopEnName = (guideNumber==""?"false":guideNumber);
teamArray.push(arry+"...");
}
var url = "getTeamsDownload?teamArray="+teamArray+"&shopCnNameStr="+shopCnNameStr;
window.location.href = url;
}
</script>
<body>
<s:include value="/common/incBody.jsp" />
<div id="float_banner" align="center" >
<button type="button" id="btuSubmit" name="btuSubmit" value="下载" οnclick="getTeamsDownload()">确认下载</button>
</div>
<div class="com_top">
<div class="container">
<form action="getTeamsDownload" method="post" id="actSubmit">
<div class="divContHead" id="div_top">
<img src="images/logo/utour120.png">
</div>
<div>
<div class="divContBody" align="center" id="div_boyd" style="height: 220px;width: 100%">
<table style="border-collapse:collapse;width: 100%" border="1" cellspacing="0" cellpadding="3" id="div_boy_top" width="100%" bordercolor="black">
<tr>
<td width="30%" align="center"><b>商店信息</b></td>
<td width="60%" align="center"><b>众信信息</b></td>
</tr>
<tr>
<td>
<input type="hidden" id="shopCnNameStr" name="shopCnNameStr" value='${shopCnNameStr}'>
${shopCnNameStr}
</td>
<td> 联系人:王岚、贺茜<br>
Tel:0086-10-64489885 / 0086-10-64489696转8393<br>
Mobile:13911058530<br>
Wechat:13911058530<br>
</td>
</tr>
</table>
<table align="left" style="padding: 6px" width="100%" id="div_con" >
<tr>
<td>尊敬的合作伙伴:</td>
</tr>
<tr>
<td>您好,请接收以下团队信息,有任何问题请随时来电保持联系,几点提醒请您留意:</td>
</tr>
<tr>
<td> 1、NO SHOW团队请及时邮件通知贺茜,便于第一时间监控团队进店,提高购物成绩;</td>
</tr>
<tr>
<td> 2、每月成绩报告请于下个月的5号之前以邮件形式发给王岚和贺茜,便于及时对账;</td>
</tr>
<tr>
<td>3、成绩报告请增加人均购物一项,便于我们监控团队购物情况,与您做更好的配合。</td>
</tr>
</table>
</div>
</div>
<br>
<div align="center" style="height:auto;width: 100%">
<table style="border-collapse:collapse;height: auto" border="1" cellspacing="0" cellpadding="3" align="center" bordercolor="black" width="100%">
<thead style="background-color:#c2e7ff">
<tr>
<td>TOUR CODE</td>
<td>DATE</td>
<td>GROUP SIZE</td>
<td>TOUR ESCORT</td>
</tr>
</thead>
<tbody>
<s:if test="#request.teamSightShopList != null">
<s:iterator status="st" value="#request.teamSightShopList">
<s:iterator status="map" value="teamSightShopMap">
<tr>
<td class="coinName"><s:property value="tourescort" /></td>
<td><s:property value="dateMonth" /></td>
<td>
<s:property value="groupsize" />
<s:if test="%{map.guideNumber!='0'}">
+ <s:property value="guideNumber" />
</s:if>
<input type="hidden" id="groupsize" name="groupsize" value='<s:property value="groupsize" />'/>
<input type="hidden" id="guideNumber" name="guideNumber" value='<s:property value="guideNumber" />'/>
</td>
<td style="display:none;">
<input type="hidden" id="productCode" name="productCode" value='<s:property value="tourescort" />'/>
<input type="hidden" id="shopCnName" name="shopCnName" value='<s:property value="dateMonth" />'/>
</td>
<td >
<s:iterator status="guide" value="%{map.guideMap}">
<s:property value="guidePYCnName" /><br>
<s:property value="guideHZCnName" />
<s:property value="tel" /> <br>
<span>
<input type="hidden" id="guidePYCnName" name="guidePYCnName" value='<s:property value="guidePYCnName" />'/>
<input type="hidden" id="guideHZCnName" name="guideHZCnName" value='<s:property value="guideHZCnName" />'/>
<input type="hidden" id="tel" name="tel" value='<s:property value="tel" />'/>
</span>
</s:iterator>
</td>
</tr>
</s:iterator>
</s:iterator>
</s:if>
</tbody>
</table>
</div>
</form>
</div>
<div class="div_foot" align="left" style="padding-top: 30px; ">
<img src="images/module/ba/log_fott.png" width="740px">
<%-- <div style="padding: 15px;width: 100%">
<div align="left" style="width: 100%;">
<span style="font-size:10px"><b>北京众信国际旅行社股份有限公司</b></span>
<span style="font-size:8px;"> 北京市朝阳区东土城路12号怡和阳光大厦B座4F 100013</span>
<span style="font-size:8px"> T:86-10-6448 9885 F:+86-10-6448 9955</span>
</div>
<div align="left" style="width: 100%;">
<span style="font-size:8px">Beijing UTour lnternational Travel Service Co.,Ltd.</span>
<span style="font-size:8px "> Add:B-4F,YiHe YangGuang Plaza,No.12 Dong TuCheng Road,Chaoyang District,Beijing China 100013</span>
</div>
</div> --%>
</div>
</div>
</body>
</html>