Commit ee9aa6a6fd8e86115122b957a9e2208aac49ee8e

Authored by 王通
1 parent 7753da64

1.添加”刷卡客流统计“报表

src/main/java/com/bsth/controller/LsStationRouteController.java
@@ -5,12 +5,15 @@ import com.bsth.entity.LsSectionRoute; @@ -5,12 +5,15 @@ import com.bsth.entity.LsSectionRoute;
5 import com.bsth.entity.LsStationRoute; 5 import com.bsth.entity.LsStationRoute;
6 import com.bsth.service.LsStationRouteService; 6 import com.bsth.service.LsStationRouteService;
7 import com.fasterxml.jackson.databind.ObjectMapper; 7 import com.fasterxml.jackson.databind.ObjectMapper;
  8 +import org.joda.time.format.DateTimeFormat;
  9 +import org.joda.time.format.DateTimeFormatter;
8 import org.slf4j.Logger; 10 import org.slf4j.Logger;
9 import org.slf4j.LoggerFactory; 11 import org.slf4j.LoggerFactory;
10 import org.springframework.beans.factory.annotation.Autowired; 12 import org.springframework.beans.factory.annotation.Autowired;
11 import org.springframework.data.domain.Page; 13 import org.springframework.data.domain.Page;
12 import org.springframework.web.bind.annotation.*; 14 import org.springframework.web.bind.annotation.*;
13 15
  16 +import java.text.DateFormat;
14 import java.util.*; 17 import java.util.*;
15 18
16 /** 19 /**
@@ -241,4 +244,25 @@ public class LsStationRouteController extends BaseController<LsStationRoute, Int @@ -241,4 +244,25 @@ public class LsStationRouteController extends BaseController<LsStationRoute, Int
241 244
242 return result; 245 return result;
243 } 246 }
  247 +
  248 + @RequestMapping(value = "/findByLineDirectionDate", method = RequestMethod.GET)
  249 + public Map<String, Object> findByLineDirectionDate(@RequestParam Map<String, Object> params) {
  250 + Map<String, Object> result = new HashMap<>();
  251 + try {
  252 + Object lineId = params.get("line"), direction = params.get("direction"), begin = params.get("rqBegin"), end = params.get("rqEnd");
  253 + if (lineId == null || direction == null || begin == null || end == null) {
  254 + throw new RuntimeException("参数异常");
  255 + }
  256 + DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyyMMddHH");
  257 + List<LsStationRoute> list = lsStationRouteService.findByLineDirectionDate(Integer.valueOf(lineId.toString()), Integer.valueOf(direction.toString()), formatter.parseDateTime(begin.toString()).toDate(), formatter.parseDateTime(end.toString()).toDate());
  258 + result.put("data", list);
  259 + result.put("status", ResponseCode.SUCCESS);
  260 + } catch (Exception e) {
  261 + result.put("status", ResponseCode.ERROR);
  262 + result.put("msg", e.getMessage());
  263 + log.error("", e);
  264 + }
  265 +
  266 + return result;
  267 + }
244 } 268 }
src/main/java/com/bsth/controller/report/PassengerStatisticController.java 0 → 100644
  1 +package com.bsth.controller.report;
  2 +
  3 +import com.bsth.controller.BaseController;
  4 +import com.bsth.entity.report.PassengerStatistic;
  5 +import com.bsth.service.report.PassengerStatisticService;
  6 +import com.bsth.service.report.ReportService;
  7 +import com.bsth.util.ReportUtils;
  8 +import com.fasterxml.jackson.databind.ObjectMapper;
  9 +import org.springframework.beans.factory.annotation.Autowired;
  10 +import org.springframework.web.bind.annotation.RequestMapping;
  11 +import org.springframework.web.bind.annotation.RequestMethod;
  12 +import org.springframework.web.bind.annotation.RequestParam;
  13 +import org.springframework.web.bind.annotation.RestController;
  14 +
  15 +import java.text.SimpleDateFormat;
  16 +import java.util.*;
  17 +
  18 +@RestController
  19 +@RequestMapping("/api/passenger-statistic")
  20 +public class PassengerStatisticController extends BaseController<PassengerStatistic, Integer> {
  21 +
  22 + @Autowired
  23 + private PassengerStatisticService passengerStatisticService;
  24 +
  25 + private ObjectMapper mapper = new ObjectMapper();
  26 +
  27 + @RequestMapping(value = "/groupByStation",method = RequestMethod.GET)
  28 + public List<PassengerStatistic> groupByStation(PassengerStatistic passengerStatistic) {
  29 + return passengerStatisticService.groupByStation(passengerStatistic);
  30 + }
  31 +
  32 + @RequestMapping(value = "/groupByStation/export",method = RequestMethod.GET)
  33 + public Map<String, Object> groupByStationExport(PassengerStatistic passengerStatistic) {
  34 + List<Iterator<?>> iterators = new ArrayList<>();
  35 + ReportUtils reportUtils = new ReportUtils();
  36 + List<PassengerStatistic> list = passengerStatisticService.groupByStation(passengerStatistic);
  37 + List<Map<String, Object>> maps = new ArrayList<>();
  38 + int count = 1;
  39 + for (PassengerStatistic ps : list) {
  40 + Map<String, Object> map = mapper.convertValue(ps, Map.class);
  41 + map.put("i", count++);
  42 + maps.add(map);
  43 + }
  44 +
  45 + try {
  46 + iterators.add(maps.iterator());
  47 + String path = this.getClass().getResource("/").getPath() + "static/pages/forms";
  48 + reportUtils.excelReplace(iterators, new Object[] { new HashMap<>() }, String.format("%s/mould/passenger-statistic-station.xls", path),
  49 + String.format("%s/export/%s至%s-刷卡客流统计(站点).xls", path, passengerStatistic.getRqBegin(), passengerStatistic.getRqEnd()));
  50 + } catch (Exception e) {
  51 + e.printStackTrace();
  52 + }
  53 +
  54 + return new HashMap<>();
  55 + }
  56 +
  57 + @RequestMapping(value = "/groupByVehicle",method = RequestMethod.GET)
  58 + public List<PassengerStatistic> groupByVehicle(PassengerStatistic passengerStatistic) {
  59 + return passengerStatisticService.groupByVehicle(passengerStatistic);
  60 + }
  61 +
  62 + @RequestMapping(value = "/groupByVehicle/export",method = RequestMethod.GET)
  63 + public Map<String, Object> groupByVehicleExport(PassengerStatistic passengerStatistic) {
  64 + List<Iterator<?>> iterators = new ArrayList<>();
  65 + ReportUtils reportUtils = new ReportUtils();
  66 + List<PassengerStatistic> list = passengerStatisticService.groupByVehicle(passengerStatistic);
  67 + List<Map<String, Object>> maps = new ArrayList<>();
  68 + int count = 1;
  69 + for (PassengerStatistic ps : list) {
  70 + Map<String, Object> map = mapper.convertValue(ps, Map.class);
  71 + map.put("i", count++);
  72 + maps.add(map);
  73 + }
  74 +
  75 + try {
  76 + iterators.add(maps.iterator());
  77 + String path = this.getClass().getResource("/").getPath() + "static/pages/forms";
  78 + reportUtils.excelReplace(iterators, new Object[] { new HashMap<>() }, String.format("%s/mould/passenger-statistic-vehicle.xls", path),
  79 + String.format("%s/export/%s至%s-刷卡客流统计(车辆).xls", path, passengerStatistic.getRqBegin(), passengerStatistic.getRqEnd()));
  80 + } catch (Exception e) {
  81 + e.printStackTrace();
  82 + }
  83 +
  84 + return new HashMap<>();
  85 + }
  86 +}
src/main/java/com/bsth/entity/report/PassengerStatistic.java 0 → 100644
  1 +package com.bsth.entity.report;
  2 +
  3 +import javax.persistence.*;
  4 +import java.util.Date;
  5 +
  6 +@Entity
  7 +@Table(name = "bsth_c_passenger_statistic")
  8 +public class PassengerStatistic {
  9 +
  10 + @Id
  11 + @GeneratedValue(strategy = GenerationType.IDENTITY)
  12 + private Integer id;
  13 +
  14 + /**
  15 + * 日期 yyyyMMdd
  16 + */
  17 + private String rq;
  18 +
  19 + @Transient
  20 + private String rqBegin;
  21 +
  22 + @Transient
  23 + private String rqEnd;
  24 +
  25 + /**
  26 + * 线路代码
  27 + */
  28 + private String line;
  29 +
  30 + /**
  31 + * 线路名称
  32 + */
  33 + private String lineName;
  34 +
  35 + /**
  36 + * 车辆内部编码
  37 + */
  38 + private String insideCode;
  39 +
  40 + /**
  41 + * 上下行
  42 + */
  43 + private int direction;
  44 +
  45 + /**
  46 + * 站名
  47 + */
  48 + private String stationName;
  49 +
  50 + /**
  51 + * 站点编码
  52 + */
  53 + private String stationCode;
  54 +
  55 + /**
  56 + * 站序
  57 + */
  58 + private String stationRouteCode;
  59 +
  60 + /**
  61 + * 刷卡次数
  62 + */
  63 + private int number;
  64 +
  65 + @Transient
  66 + private int statistic;
  67 +
  68 + public Integer getId() {
  69 + return id;
  70 + }
  71 +
  72 + public void setId(Integer id) {
  73 + this.id = id;
  74 + }
  75 +
  76 + public String getRq() {
  77 + return rq;
  78 + }
  79 +
  80 + public void setRq(String rq) {
  81 + this.rq = rq;
  82 + }
  83 +
  84 + public String getRqBegin() {
  85 + return rqBegin;
  86 + }
  87 +
  88 + public void setRqBegin(String rqBegin) {
  89 + this.rqBegin = rqBegin;
  90 + }
  91 +
  92 + public String getRqEnd() {
  93 + return rqEnd;
  94 + }
  95 +
  96 + public void setRqEnd(String rqEnd) {
  97 + this.rqEnd = rqEnd;
  98 + }
  99 +
  100 + public String getLine() {
  101 + return line;
  102 + }
  103 +
  104 + public void setLine(String line) {
  105 + this.line = line;
  106 + }
  107 +
  108 + public String getLineName() {
  109 + return lineName;
  110 + }
  111 +
  112 + public void setLineName(String lineName) {
  113 + this.lineName = lineName;
  114 + }
  115 +
  116 + public String getInsideCode() {
  117 + return insideCode;
  118 + }
  119 +
  120 + public void setInsideCode(String insideCode) {
  121 + this.insideCode = insideCode;
  122 + }
  123 +
  124 + public int getDirection() {
  125 + return direction;
  126 + }
  127 +
  128 + public void setDirection(int direction) {
  129 + this.direction = direction;
  130 + }
  131 +
  132 + public String getDirectionStr() {
  133 + return direction == 0 ? "上行" : "下行";
  134 + }
  135 +
  136 + public String getStationName() {
  137 + return stationName;
  138 + }
  139 +
  140 + public void setStationName(String stationName) {
  141 + this.stationName = stationName;
  142 + }
  143 +
  144 + public String getStationCode() {
  145 + return stationCode;
  146 + }
  147 +
  148 + public void setStationCode(String stationCode) {
  149 + this.stationCode = stationCode;
  150 + }
  151 +
  152 + public String getStationRouteCode() {
  153 + return stationRouteCode;
  154 + }
  155 +
  156 + public void setStationRouteCode(String stationRouteCode) {
  157 + this.stationRouteCode = stationRouteCode;
  158 + }
  159 +
  160 + public int getNumber() {
  161 + return number;
  162 + }
  163 +
  164 + public void setNumber(int number) {
  165 + this.number = number;
  166 + }
  167 +
  168 + public int getStatistic() {
  169 + return statistic;
  170 + }
  171 +
  172 + public void setStatistic(int statistic) {
  173 + this.statistic = statistic;
  174 + }
  175 +
  176 + @Override
  177 + public String toString() {
  178 + return "PassengerStatistic{" +
  179 + "rq='" + rq + '\'' +
  180 + ", line='" + line + '\'' +
  181 + ", lineName='" + lineName + '\'' +
  182 + ", insideCode='" + insideCode + '\'' +
  183 + ", direction=" + direction +
  184 + ", stationName='" + stationName + '\'' +
  185 + ", stationCode='" + stationCode + '\'' +
  186 + ", stationRouteCode='" + stationRouteCode + '\'' +
  187 + ", number=" + number +
  188 + '}';
  189 + }
  190 +}
src/main/java/com/bsth/repository/LsStationRouteRepository.java
1 package com.bsth.repository; 1 package com.bsth.repository;
2 2
  3 +import java.util.Date;
3 import java.util.List; 4 import java.util.List;
4 import java.util.Map; 5 import java.util.Map;
5 6
@@ -172,4 +173,15 @@ public interface LsStationRouteRepository extends BaseRepository&lt;LsStationRoute, @@ -172,4 +173,15 @@ public interface LsStationRouteRepository extends BaseRepository&lt;LsStationRoute,
172 */ 173 */
173 @Query("select r from LsStationRoute r where r.line.id=?1 and r.versions=?2 and r.directions=?3 and r.destroy=0 order by r.stationRouteCode") 174 @Query("select r from LsStationRoute r where r.line.id=?1 and r.versions=?2 and r.directions=?3 and r.destroy=0 order by r.stationRouteCode")
174 List<LsStationRoute> findByLineVersion(Integer lineId, Integer version, Integer direction, Integer stationRouteCode); 175 List<LsStationRoute> findByLineVersion(Integer lineId, Integer version, Integer direction, Integer stationRouteCode);
  176 +
  177 + /**
  178 + *
  179 + * @param lineId
  180 + * @param direction
  181 + * @param begin
  182 + * @param end
  183 + * @return
  184 + */
  185 + @Query(value = "SELECT * FROM bsth_c_ls_stationroute WHERE line = :#{#lineId} AND directions = :#{#direction} AND versions IN (SELECT versions FROM bsth_c_line_versions WHERE line = 12148 AND (:#{#begin} BETWEEN start_date AND end_date OR :#{#end} BETWEEN start_date AND end_date)) ORDER BY versions,station_route_code", nativeQuery = true)
  186 + List<LsStationRoute> findByLineDirectionDate(Integer lineId, Integer direction, Date begin, Date end);
175 } 187 }
src/main/java/com/bsth/repository/report/PassengerStatisticRepository.java 0 → 100644
  1 +package com.bsth.repository.report;
  2 +
  3 +import com.bsth.entity.report.PassengerStatistic;
  4 +import com.bsth.repository.BaseRepository;
  5 +import org.springframework.data.jpa.repository.Query;
  6 +import org.springframework.stereotype.Repository;
  7 +
  8 +import java.util.Date;
  9 +import java.util.List;
  10 +
  11 +@Repository
  12 +public interface PassengerStatisticRepository extends BaseRepository<PassengerStatistic, Integer> {
  13 +
  14 + @Query(value = "SELECT line_name,direction,station_name,sum(number) number FROM bsth_c_passenger_statistic WHERE rq BETWEEN :#{#statistic.rqBegin} AND :#{#statistic.rqEnd} AND line = :#{#statistic.line} AND direction = :#{#statistic.direction} AND CASE WHEN ISNULL(:#{#statistic.stationCode}) THEN 1 = 1 ELSE station_code = :#{#statistic.stationCode} END GROUP BY line_name,direction,station_name,station_route_code ORDER BY line_name,direction,station_name,station_route_code", nativeQuery = true)
  15 + List<Object[]> groupByStation(PassengerStatistic statistic);
  16 +
  17 + @Query(value = "SELECT line_name,inside_code,sum(number) number FROM bsth_c_passenger_statistic WHERE rq BETWEEN :#{#statistic.rqBegin} AND :#{#statistic.rqEnd} AND line = :#{#statistic.line} AND CASE WHEN ISNULL(:#{#statistic.insideCode}) THEN 1 = 1 ELSE inside_code = :#{#statistic.insideCode} END GROUP BY line_name,inside_code ORDER BY line_name,inside_code", nativeQuery = true)
  18 + List<Object[]> groupByVehicle(PassengerStatistic statistic);
  19 +}
src/main/java/com/bsth/service/LsStationRouteService.java
@@ -3,6 +3,7 @@ package com.bsth.service; @@ -3,6 +3,7 @@ package com.bsth.service;
3 import com.bsth.entity.LsSectionRoute; 3 import com.bsth.entity.LsSectionRoute;
4 import com.bsth.entity.LsStationRoute; 4 import com.bsth.entity.LsStationRoute;
5 5
  6 +import java.util.Date;
6 import java.util.List; 7 import java.util.List;
7 import java.util.Map; 8 import java.util.Map;
8 9
@@ -99,4 +100,14 @@ public interface LsStationRouteService extends BaseService&lt;LsStationRoute, Integ @@ -99,4 +100,14 @@ public interface LsStationRouteService extends BaseService&lt;LsStationRoute, Integ
99 * @return 100 * @return
100 */ 101 */
101 Map<String, Object> circularRouteHandle(Integer lineId, Integer version); 102 Map<String, Object> circularRouteHandle(Integer lineId, Integer version);
  103 +
  104 + /**
  105 + * 根据线路、上下行、日期区间查询对应时间段内多版本站点路由信息
  106 + * @param lineId
  107 + * @param direction
  108 + * @param begin
  109 + * @param end
  110 + * @return
  111 + */
  112 + List<LsStationRoute> findByLineDirectionDate(Integer lineId, Integer direction, Date begin, Date end);
102 } 113 }
src/main/java/com/bsth/service/impl/LsStationRouteServiceImpl.java
@@ -24,10 +24,7 @@ import org.springframework.util.StringUtils; @@ -24,10 +24,7 @@ import org.springframework.util.StringUtils;
24 24
25 import java.sql.PreparedStatement; 25 import java.sql.PreparedStatement;
26 import java.sql.SQLException; 26 import java.sql.SQLException;
27 -import java.util.ArrayList;  
28 -import java.util.HashMap;  
29 -import java.util.List;  
30 -import java.util.Map; 27 +import java.util.*;
31 28
32 /** 29 /**
33 * @author Hill 30 * @author Hill
@@ -396,6 +393,11 @@ public class LsStationRouteServiceImpl extends BaseServiceImpl&lt;LsStationRoute, I @@ -396,6 +393,11 @@ public class LsStationRouteServiceImpl extends BaseServiceImpl&lt;LsStationRoute, I
396 return new HashMap<>(); 393 return new HashMap<>();
397 } 394 }
398 395
  396 + @Override
  397 + public List<LsStationRoute> findByLineDirectionDate(Integer lineId, Integer direction, Date begin, Date end) {
  398 + return lsStationRouteRepository.findByLineDirectionDate(lineId, direction, begin, end);
  399 + }
  400 +
399 protected void centerPoint(Station station) { 401 protected void centerPoint(Station station) {
400 // 中心点坐标信息 402 // 中心点坐标信息
401 String wkt = station.getCenterPointWkt(); 403 String wkt = station.getCenterPointWkt();
src/main/java/com/bsth/service/report/PassengerStatisticService.java 0 → 100644
  1 +package com.bsth.service.report;
  2 +
  3 +import com.bsth.entity.report.PassengerStatistic;
  4 +import com.bsth.entity.sheet.CalcSheet;
  5 +import com.bsth.entity.sheet.Sheet;
  6 +import com.bsth.service.BaseService;
  7 +
  8 +import java.util.Date;
  9 +import java.util.List;
  10 +import java.util.Map;
  11 +
  12 +public interface PassengerStatisticService extends BaseService<PassengerStatistic, Integer>{
  13 +
  14 + List<PassengerStatistic> groupByStation(PassengerStatistic passengerStatistic);
  15 +
  16 + List<PassengerStatistic> groupByVehicle(PassengerStatistic passengerStatistic);
  17 +}
0 \ No newline at end of file 18 \ No newline at end of file
src/main/java/com/bsth/service/report/impl/PassengerStatisticServiceImpl.java 0 → 100644
  1 +package com.bsth.service.report.impl;
  2 +
  3 +import com.bsth.entity.report.PassengerStatistic;
  4 +import com.bsth.repository.report.PassengerStatisticRepository;
  5 +import com.bsth.service.impl.BaseServiceImpl;
  6 +import com.bsth.service.report.PassengerStatisticService;
  7 +import org.springframework.beans.factory.annotation.Autowired;
  8 +import org.springframework.stereotype.Service;
  9 +
  10 +import java.math.BigDecimal;
  11 +import java.util.ArrayList;
  12 +import java.util.Collections;
  13 +import java.util.Date;
  14 +import java.util.List;
  15 +
  16 +@Service
  17 +public class PassengerStatisticServiceImpl extends BaseServiceImpl<PassengerStatistic, Integer> implements PassengerStatisticService {
  18 +
  19 + @Autowired
  20 + private PassengerStatisticRepository passengerStatisticRepository;
  21 +
  22 + @Override
  23 + public List<PassengerStatistic> groupByStation(PassengerStatistic passengerStatistic) {
  24 + List<PassengerStatistic> result = new ArrayList<>();
  25 + List<Object[]> list = passengerStatisticRepository.groupByStation(passengerStatistic);
  26 + for (Object[] o : list) {
  27 + PassengerStatistic ps = new PassengerStatistic();
  28 + ps.setLineName((String) o[0]);
  29 + ps.setDirection((Integer) o[1]);
  30 + ps.setStationName((String) o[2]);
  31 + ps.setNumber(((BigDecimal) o[3]).intValue());
  32 +
  33 + result.add(ps);
  34 + }
  35 +
  36 + return result;
  37 + }
  38 +
  39 + @Override
  40 + public List<PassengerStatistic> groupByVehicle(PassengerStatistic passengerStatistic) {
  41 + List<PassengerStatistic> result = new ArrayList<>();
  42 + List<Object[]> list = passengerStatisticRepository.groupByVehicle(passengerStatistic);
  43 + for (Object[] o : list) {
  44 + PassengerStatistic ps = new PassengerStatistic();
  45 + ps.setLineName((String) o[0]);
  46 + ps.setInsideCode((String) o[1]);
  47 + ps.setNumber(((BigDecimal) o[2]).intValue());
  48 +
  49 + result.add(ps);
  50 + }
  51 +
  52 + return result;
  53 + }
  54 +}
0 \ No newline at end of file 55 \ No newline at end of file
src/main/java/com/bsth/util/ReportUtils.java
1 -package com.bsth.util;  
2 -  
3 -import java.io.File;  
4 -import java.io.FileInputStream;  
5 -import java.io.FileOutputStream;  
6 -import java.util.ArrayList;  
7 -import java.util.HashMap;  
8 -import java.util.Iterator;  
9 -import java.util.List;  
10 -import java.util.Map;  
11 -import java.util.TreeMap;  
12 -import java.util.regex.Matcher;  
13 -import java.util.regex.Pattern;  
14 -  
15 -import com.bsth.common.ResponseCode;  
16 -import org.apache.poi.hssf.usermodel.HSSFCell;  
17 -import org.apache.poi.hssf.usermodel.HSSFCellStyle;  
18 -import org.apache.poi.hssf.usermodel.HSSFFont;  
19 -import org.apache.poi.hssf.usermodel.HSSFRow;  
20 -import org.apache.poi.hssf.usermodel.HSSFSheet;  
21 -import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
22 -import org.apache.poi.poifs.filesystem.POIFSFileSystem;  
23 -import org.apache.poi.ss.usermodel.Cell;  
24 -import org.apache.poi.ss.usermodel.Workbook;  
25 -import org.apache.poi.ss.util.CellRangeAddress;  
26 -  
27 -import com.bsth.entity.Line;  
28 -import com.bsth.entity.realcontrol.ScheduleRealInfo;  
29 -import org.apache.poi.ss.util.RegionUtil;  
30 -import org.apache.poi.xssf.usermodel.XSSFCell;  
31 -  
32 -public class ReportUtils {  
33 - // private final String packaegName = "com.bsth.entity.";  
34 - private final String packaegName = "com.bsth.entity.realcontrol.";  
35 -  
36 - /**  
37 - * /**  
38 - *  
39 - * @param list  
40 - * 模板中,需要重复显示的行所需的数据  
41 - * @param map  
42 - * 模板中除以上list外所有的数据  
43 - * @param index  
44 - * 需要重复的行号,该值为行号减1  
45 - * @param sourcePath  
46 - * 模板路径  
47 - * @param targetPath  
48 - * 生成路径  
49 - */  
50 - public void excelReplace(List<Iterator<?>> list, Object[] tArray,  
51 - String sourcePath, String targetPath) {  
52 - try {  
53 - // 把源文件放入流中  
54 - POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(  
55 - sourcePath));  
56 - HSSFWorkbook wb = new HSSFWorkbook(fs);  
57 - if(tArray.length != 0 && tArray[0] instanceof java.util.Map){  
58 - Map<String, Object> m = (Map<String, Object>)tArray[0];  
59 - if(m.containsKey("sheetName") && m.get("sheetName")!=null  
60 - && m.get("sheetName").toString().trim().length()!=0)  
61 - wb.setSheetName(0, m.get("sheetName").toString());  
62 - }  
63 - HSSFSheet sheet = wb.getSheetAt(0);  
64 - HSSFRow row;  
65 - HSSFCell cell = null;  
66 - String key;  
67 - // 取得总行数  
68 - int rowNum = sheet.getLastRowNum();  
69 - // 取得总列数  
70 - int cellNum = sheet.getRow(0).getLastCellNum();  
71 -  
72 - // 遍历行  
73 - for (int i = 0; i < rowNum; i++) {  
74 - row = sheet.getRow(i);  
75 - // 遍历列  
76 - for (int j = 0; j < cellNum; j++) {  
77 - if (row == null) {  
78 - continue;  
79 - }  
80 - cell = row.getCell(j);  
81 - if (cell == null) {  
82 - continue;  
83 - }  
84 - // 取得每列的内容,如果列内容是$key$格式,则替换内容  
85 - key = getCellValue(cell);  
86 - if (key != null && (key.indexOf("$") != -1 || key.indexOf("#list#") != -1)) {  
87 - // * 列中内容有#list#,则表示该行为模板行,需要以该行为模板  
88 - // * 例如:模板行格式 #list#0_0 $Car.id$  
89 - // * 第一个0表示需要在list中取iterator的索引值  
90 - // * 第二个0表示在iterator中取的第几个对象,如果不为0,则取下一个对象,而不是沿用前面获取的对象  
91 - // * $Car.id$表示所取的对象为Car的对象,并且取值为id的值  
92 - if (key.indexOf("#list#") != -1) {  
93 - key = key.replace("#list#", "").trim();  
94 - String[] lists = key.split(" ");  
95 - // 取得list中的索引值  
96 - int listIndex = Integer  
97 - .valueOf(lists[0].split("_")[0]);  
98 - Iterator<?> iterator = list.get(listIndex);  
99 - // 根据模板创建行并填弃数据,返回增加的行数  
100 - int rowCount = iteratorFillCellValue(wb, sheet,  
101 - cell, iterator, i, rowNum, key);  
102 - rowNum += rowCount;  
103 - i += rowCount;  
104 - break;  
105 - } else {  
106 - // 直接填充数据的列,从对象数组中取得值,这里的数组不传值  
107 - getValueAndSetCellValue(cell, key, tArray, new String[]{""});  
108 - }  
109 - }  
110 -  
111 - }  
112 - }  
113 - // 创建目标文件夹  
114 - createFolder(targetPath);  
115 - // 输出文件  
116 - FileOutputStream fileOut = new FileOutputStream(targetPath);  
117 - wb.write(fileOut);  
118 - fileOut.close();  
119 - } catch (Exception e) {  
120 - e.printStackTrace();  
121 - }  
122 - }  
123 -  
124 -  
125 - /**  
126 - *  
127 - * @param sheetList 多sheet模板中,需要重复显示的行所需的数据  
128 - * @param tArray  
129 - * @param sourcePath 模板路径  
130 - * @param targetPath 生成路径  
131 - */  
132 - public void excelMoreSheetReplace(List<List<Iterator<?>>> sheetList, Object[] tArray,  
133 - String sourcePath, String targetPath) {  
134 - try {  
135 - // 把源文件放入流中  
136 - POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(  
137 - sourcePath));  
138 - HSSFWorkbook wb = new HSSFWorkbook(fs);  
139 - for (int s=0; s<sheetList.size(); s++){  
140 - List<Iterator<?>> list = sheetList.get(s);  
141 - if(tArray.length != 0 && tArray[0] instanceof java.util.Map){  
142 - Map<String, Object> m = (Map<String, Object>)tArray[0];  
143 - if(m.containsKey("sheetName"+s+1) && m.get("sheetName"+s+1)!=null  
144 - && m.get("sheetName"+s+1).toString().trim().length()!=0)  
145 - wb.setSheetName(0, m.get("sheetName"+s+1).toString());  
146 - }  
147 - HSSFSheet sheet = wb.getSheetAt(s);  
148 - HSSFRow row;  
149 - HSSFCell cell = null;  
150 - String key;  
151 - // 取得总行数  
152 - int rowNum = sheet.getLastRowNum();  
153 - // 取得总列数  
154 - int cellNum = sheet.getRow(0).getLastCellNum();  
155 -  
156 - // 遍历行  
157 - for (int i = 0; i <= rowNum; i++) {  
158 - row = sheet.getRow(i);  
159 - // 遍历列  
160 - for (int j = 0; j < cellNum; j++) {  
161 - if (row == null) {  
162 - continue;  
163 - }  
164 - cell = row.getCell(j);  
165 - if (cell == null) {  
166 - continue;  
167 - }  
168 - // 取得每列的内容,如果列内容是$key$格式,则替换内容  
169 - key = getCellValue(cell);  
170 - if (key != null && (key.indexOf("$") != -1 || key.indexOf("#list#") != -1)) {  
171 - // * 列中内容有#list#,则表示该行为模板行,需要以该行为模板  
172 - // * 例如:模板行格式 #list#0_0 $Car.id$  
173 - // * 第一个0表示需要在list中取iterator的索引值  
174 - // * 第二个0表示在iterator中取的第几个对象,如果不为0,则取下一个对象,而不是沿用前面获取的对象  
175 - // * $Car.id$表示所取的对象为Car的对象,并且取值为id的值  
176 - if (key.indexOf("#list#") != -1) {  
177 - key = key.replace("#list#", "").trim();  
178 - String[] lists = key.split(" ");  
179 - // 取得list中的索引值  
180 - int listIndex = Integer  
181 - .valueOf(lists[0].split("_")[0]);  
182 - Iterator<?> iterator = list.get(listIndex);  
183 - // 根据模板创建行并填充数据,返回增加的行数  
184 - int rowCount = iteratorFillCellValue(wb, sheet,  
185 - cell, iterator, i, rowNum, key);  
186 - rowNum += rowCount;  
187 - i += rowCount;  
188 - break;  
189 - } else {  
190 - // 直接填充数据的列,从对象数组中取得值,这里的数组不传值  
191 - getValueAndSetCellValue(cell, key, tArray, new String[]{""});  
192 - }  
193 - }  
194 -  
195 - }  
196 - }  
197 - }  
198 -  
199 - // 创建目标文件夹  
200 - createFolder(targetPath);  
201 - // 输出文件  
202 - FileOutputStream fileOut = new FileOutputStream(targetPath);  
203 - wb.write(fileOut);  
204 - fileOut.close();  
205 - } catch (Exception e) {  
206 - e.printStackTrace();  
207 - }  
208 - }  
209 -  
210 - /**  
211 - * 将file1中的一页sheet复制到file2中  
212 - *  
213 - * @param file1  
214 - * 原sheet所在的excel文件  
215 - * @param file2  
216 - * 目标excel文件  
217 - * @param page  
218 - * 原excel中要被复制的sheet的位置(从0开始)  
219 - * @param rate  
220 - * 调整复制后的缩放倍率(列如:145,则为缩放145%)  
221 - */  
222 - public void copySheetByFile(File file1, File file2, int page, int rate) {  
223 - try {  
224 - // 把源文件放入流中  
225 - POIFSFileSystem fs1 = new POIFSFileSystem(new FileInputStream(file1));  
226 - HSSFWorkbook wb1 = new HSSFWorkbook(fs1);  
227 - HSSFSheet sheet = wb1.getSheetAt(page);  
228 - POIFSFileSystem fs2 = new POIFSFileSystem(new FileInputStream(file2));  
229 - HSSFWorkbook wb2 = new HSSFWorkbook(fs2);  
230 - HSSFSheet createSheet = wb2.createSheet(sheet.getSheetName());  
231 - HSSFCellStyle createCellStyle = wb2.createCellStyle();  
232 - HSSFRow row;  
233 -  
234 - createSheet.setZoom(rate, 100);  
235 - for(int i = 0; i < sheet.getRow(0).getPhysicalNumberOfCells(); i++){  
236 - createSheet.setColumnWidth(i, sheet.getColumnWidth(i));  
237 - }  
238 -  
239 - List<CellRangeAddress> mergedRegions = sheet.getMergedRegions();  
240 - for(int l = 0; l < mergedRegions.size(); l++){  
241 - //复制源表中的合并单元格  
242 - createSheet.addMergedRegion(mergedRegions.get(l));  
243 - }  
244 - int firstRow = sheet.getFirstRowNum();  
245 - int lastRow = sheet.getLastRowNum();  
246 - for(int k = firstRow; k <= lastRow; k++){  
247 - // 创建新建excel Sheet的行  
248 - HSSFRow rowCreat = createSheet.createRow(k);  
249 - // 取得源有excel Sheet的行  
250 - row = sheet.getRow(k);  
251 -// rowCreat.setHeight(row.getHeight()); //设置行高  
252 - // 单元格式样  
253 - int firstCell = row.getFirstCellNum();  
254 - int lastCell = row.getLastCellNum();  
255 - for (int j = firstCell; j < lastCell; j++) {  
256 - // 自动适应列宽 貌似不起作用  
257 -// createSheet.autoSizeColumn(j);  
258 -// System.out.println(row.getCell(j));  
259 - rowCreat.createCell(j);  
260 - String strVal = "";  
261 - if (row.getCell(j)==null) {  
262 -  
263 - } else {  
264 - strVal = row.getCell(j).getStringCellValue();  
265 - rowCreat.getCell(j).setCellValue(strVal);  
266 - copyCellStyle(wb1, row.getCell(j).getCellStyle(), createCellStyle);  
267 - createCellStyle.setBorderTop((short)1);  
268 - createCellStyle.setBorderLeft((short)1);  
269 - createCellStyle.setBorderRight((short)1);  
270 - createCellStyle.setBorderBottom((short)1);  
271 - rowCreat.getCell(j).setCellStyle(createCellStyle);  
272 - }  
273 - }  
274 - }  
275 -  
276 -// int firstRowNum = createSheet.getFirstRowNum();  
277 -// int lastRowNum = createSheet.getLastRowNum();  
278 -// int test = 0;  
279 -// for(int k = firstRowNum; k <= lastRowNum; k++){  
280 -// HSSFRow createRow = createSheet.getRow(k);  
281 -// int firstCellNum = createRow.getFirstCellNum();  
282 -// int lastCellNum = createRow.getLastCellNum();  
283 -// for(int i = firstCellNum; i < lastCellNum; i++){  
284 -// HSSFCell cell = createRow.getCell(i);  
285 -// cell.getCellStyle().setBorderTop(HSSFCellStyle.BORDER_THIN);  
286 -// cell.getCellStyle().setBorderLeft(HSSFCellStyle.BORDER_THIN);  
287 -// cell.getCellStyle().setBorderRight(HSSFCellStyle.BORDER_THIN);  
288 -// cell.getCellStyle().setBorderBottom(HSSFCellStyle.BORDER_THIN);  
289 -// test ++;  
290 -// }  
291 -// }  
292 -// System.out.println("test = " + test);  
293 -  
294 - FileOutputStream fileOut = new FileOutputStream(file2);  
295 - wb2.write(fileOut);  
296 - fileOut.close();  
297 - wb2.close();  
298 - wb1.close();  
299 - fs2.close();  
300 - fs1.close();  
301 - file1.delete();  
302 -// // 创建目标文件夹  
303 -// createFolder(targetPath);  
304 - // 输出文件  
305 - } catch (Exception e) {  
306 - e.printStackTrace();  
307 - }  
308 - }  
309 -  
310 - public void test(File file){  
311 - POIFSFileSystem fs;  
312 - try {  
313 - fs = new POIFSFileSystem(new FileInputStream(file));  
314 - HSSFWorkbook wb = new HSSFWorkbook(fs);  
315 - for(int j = 0; j < wb.getNumberOfSheets(); j++){  
316 - HSSFSheet sheet = wb.getSheetAt(j);  
317 - int firstRowNum = sheet.getFirstRowNum();  
318 - int lastRowNum = sheet.getLastRowNum();  
319 - int test = 0;  
320 - for(int k = firstRowNum; k <= lastRowNum; k++){  
321 - HSSFRow createRow = sheet.getRow(k);  
322 - int firstCellNum = createRow.getFirstCellNum();  
323 - int lastCellNum = createRow.getLastCellNum();  
324 - for(int i = firstCellNum; i < lastCellNum; i++){  
325 - HSSFCell cell = createRow.getCell(i);  
326 - HSSFCellStyle cellStyle = wb.createCellStyle();  
327 -  
328 - cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);  
329 - cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);  
330 - cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);  
331 - cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);  
332 - cell.setCellStyle(cellStyle);  
333 - test ++;  
334 - }  
335 - }  
336 - System.out.println("test = " + test);  
337 -  
338 - FileOutputStream fileOut = new FileOutputStream(file);  
339 - wb.write(fileOut);  
340 - fileOut.close();  
341 - }  
342 - } catch (Exception e) {  
343 - // TODO Auto-generated catch block  
344 - e.printStackTrace();  
345 - }  
346 -  
347 - }  
348 -  
349 - public String getCellValue(HSSFCell cell) {  
350 - int cellType = 0;  
351 - String result = "";  
352 - double d;  
353 - if(cell != null) {  
354 - // 获取列数据类型  
355 - cellType = cell.getCellType();  
356 - // 不同列数据类型,取值方法不同  
357 - switch(cellType) {  
358 - // String  
359 - case HSSFCell.CELL_TYPE_STRING:  
360 - result = cell.getStringCellValue().toString();  
361 - break;  
362 - // numeric类型,excel中,日期格式会转成数字格式存储  
363 - case HSSFCell.CELL_TYPE_NUMERIC:  
364 - result = cell.getNumericCellValue()+"";  
365 - break;  
366 - // 公式类型  
367 - case HSSFCell.CELL_TYPE_FORMULA:  
368 - result = cell.getCellFormula() + "";  
369 - break;  
370 - // boolean类型  
371 - case HSSFCell.CELL_TYPE_BOOLEAN:  
372 - result = cell.getBooleanCellValue() + "";  
373 - break;  
374 - // 空格  
375 - case HSSFCell.CELL_TYPE_BLANK:  
376 - result = null;  
377 - break;  
378 - // 错误值  
379 - case HSSFCell.CELL_TYPE_ERROR:  
380 - result = null;  
381 - break;  
382 - default :  
383 - System.out.println("其它");  
384 - break;  
385 - }  
386 - }  
387 - return result;  
388 - }  
389 -  
390 - /**  
391 - * 根据iterator,以及模板中的标识,填充模板  
392 - *  
393 - * @param wb  
394 - * @param sheet  
395 - * @param cell  
396 - * @param iterator  
397 - * iterator  
398 - * @param index  
399 - * 模板行索引  
400 - * @param rowNum  
401 - * 表格总行数  
402 - * @param key  
403 - * 表格内容  
404 - * @return  
405 - */  
406 - private int iteratorFillCellValue(HSSFWorkbook wb, HSSFSheet sheet,  
407 - HSSFCell cell, Iterator<?> iterator, int index, int rowNum,  
408 - String key) {  
409 - int rowCount = 0;  
410 - Object obj = null;  
411 - int p = 0;  
412 - int i = index;  
413 - HSSFRow newRow = null;  
414 - int tmpCellNum = 0;  
415 - int k = 0;  
416 - int listIndex = 0;  
417 - // 取得模板行  
418 - HSSFRow orgRow = sheet.getRow(index);  
419 - HSSFCellStyle style= wb.createCellStyle();  
420 - try {  
421 - while (iterator.hasNext()) {  
422 - // 取得iterator的对象  
423 - obj = iterator.next();  
424 - // 移动当前编辑行以下的所有行  
425 - if (p != 0) {  
426 - rowNum += 1;  
427 - i += 1;  
428 - rowCount += 1;// 增加的总行数  
429 - // 把当前行以下的所有行往下移动1行  
430 - sheet.shiftRows(i, rowNum, 1);  
431 - }  
432 - p = 1;  
433 - // 创建新行  
434 - newRow = sheet.createRow(index + k++);  
435 - // 把新行的内容换成和模板行一样  
436 - copyRow(wb, orgRow, newRow, true,style);  
437 - tmpCellNum = newRow.getLastCellNum();  
438 - for (int l = 0; l < tmpCellNum; l++) {  
439 - cell = newRow.getCell(l);  
440 - key = getCellValue(cell);  
441 - /**  
442 - * 如果单无格内容为#list#,表示该行是模板行 #list#0_0  
443 - * 第一个0表示需要在list中取iterator的索引值  
444 - * 第二个0表示在iterator中取的第几个对象,如果不为0,则取下一个对象,而不是沿用前面获取的对象  
445 - */  
446 - if(key == null || key.equals("")){  
447 - obj = iterator.next();  
448 - }  
449 - if (key.indexOf("#list#") != -1 && key.indexOf("_0") == -1) {  
450 - if (iterator.hasNext()) {  
451 - obj = iterator.next();  
452 - } else {  
453 - obj = null;  
454 - }  
455 - }  
456 - if (key.trim().indexOf(" ") != -1) {  
457 - key = key.split(" ")[1];  
458 - }  
459 - getValueAndSetCellValue(cell, key, obj,new String[]{listIndex+""});  
460 - }  
461 - // list的数量  
462 - listIndex ++;  
463 - }  
464 - } catch (Exception e) {  
465 - e.printStackTrace();  
466 - }  
467 - return rowCount;  
468 - }  
469 -  
470 - /**  
471 - * 取到相应的值并填入相应的列中  
472 - *  
473 - * @param cell 列  
474 - * @param key 列内容  
475 - * @param obj 数据源对象  
476 - * @param args 其他参数 数组  
477 - * 数组内容:  
478 - * 0位:在list中的第几条数据  
479 - */  
480 - private void getValueAndSetCellValue(HSSFCell cell, String key, Object obj, String[] args) {  
481 - try {  
482 - // 保有存单元格的内容  
483 - String cellValue = key = key.replace("\\n", "");  
484 - String tmpKey;  
485 - // 判断单元格内容是否是公式  
486 - if(cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA){  
487 - Pattern p=Pattern.compile("(\\d+)"); //  
488 - Matcher m=p.matcher(key);  
489 - if(m.find()){  
490 - cellValue = key.replace(m.group(1),  
491 - Integer.valueOf(m.group(1))+Integer.valueOf(args[0])+"");  
492 - cell.setCellFormula(cellValue);  
493 - return;  
494 - }  
495 - }else{//其他格式  
496 -  
497 - // 循环截取两个$中间的内容,反射出值  
498 - while (key.indexOf("$") != -1) {  
499 - key = key.substring(key.indexOf("$") + 1);  
500 - // 取两个$中间的内容  
501 - tmpKey = key.substring(0, key.indexOf("$"));  
502 - key = key.substring(key.indexOf("$") + 1);  
503 - // 如果内容是如下格式Cars.id,则从obj中值得相应的对象的值  
504 - if (tmpKey.indexOf(".") != -1) {  
505 - String className = tmpKey.substring(0, tmpKey.indexOf("."));  
506 - // 取得类的全限定名  
507 - String classWholeName = packaegName + className;  
508 - String fieldName = tmpKey.substring(tmpKey.indexOf(".") + 1);  
509 - // 如果obj是数组,循环判断哪个对象是对应的  
510 - if (obj instanceof Object[]) {  
511 - Object[] objs = (Object[]) obj;  
512 - for (int k = 0; k < objs.length; k++) {  
513 - if (objs[k].getClass().getName().equals(classWholeName)) {  
514 - cellValue = cellValue.replace("$" + tmpKey  
515 - + "$", getKeyValue(objs[k], fieldName)  
516 - + "");  
517 - } else if(objs[k].getClass().getName().equals("java.util.HashMap")){  
518 - Map<String,Object> map = (HashMap<String,Object>)objs[k];  
519 - cellValue = cellValue.replace("$" + tmpKey + "$",map.get(fieldName)+"");  
520 - }  
521 - }  
522 - } else if (obj.getClass().getName().equals(classWholeName)) {  
523 - cellValue = cellValue.replace("$" + tmpKey + "$",getKeyValue(obj, fieldName) + "");  
524 - } else if (obj.getClass().getName().equals("java.util.HashMap")){  
525 - Map<String,Object> map = (HashMap<String,Object>)obj;  
526 - cellValue = cellValue.replace("$" + tmpKey + "$",map.get(fieldName)+"");  
527 - }  
528 - }  
529 - }  
530 - }  
531 - cell.setCellValue(cellValue);  
532 - } catch (Exception e) {  
533 - e.printStackTrace();  
534 - }  
535 -  
536 - }  
537 -  
538 - /**  
539 - * 给列填充数据  
540 - *  
541 - * @param cell  
542 - * 列  
543 - * @param obj  
544 - * 数据源对象  
545 - * @param fieldName  
546 - * 需要取数据的字段  
547 - */  
548 - private Object getKeyValue(Object obj, String fieldName) {  
549 - Object value = "";  
550 - try {  
551 - if (obj != null) {  
552 - ReportRelatedUtils test = new ReportRelatedUtils();  
553 - value = test.getValue(obj, fieldName) == null ? "" : test .getValue(obj, fieldName);  
554 - }  
555 - } catch (Exception e) {  
556 - e.printStackTrace();  
557 - }  
558 - return value;  
559 - }  
560 -  
561 - public static void main(String[] args) {  
562 -  
563 - try {  
564 - ReportUtils ee = new ReportUtils();  
565 - List<Iterator<?>> list = new ArrayList<Iterator<?>>();  
566 - Line line = new Line();  
567 - line.setId(1);  
568 - line.setName("line1");  
569 -  
570 - List<Object> dataList = new ArrayList<Object>();  
571 -  
572 -  
573 - ScheduleRealInfo srr = new ScheduleRealInfo();  
574 - srr.setId((long) 111);  
575 - srr.setXlName("abc11");  
576 -  
577 - ScheduleRealInfo sr = new ScheduleRealInfo();  
578 - sr.setZdsj("06:10");  
579 - sr.setZdsjActual("06:25");  
580 - dataList.add(sr);  
581 - sr = new ScheduleRealInfo();  
582 - sr.setZdsj("06:20");  
583 - sr.setZdsjActual("");  
584 - dataList.add(sr);  
585 - list.add(dataList.iterator());  
586 -  
587 - ee.excelReplace(list, new Object[] { srr }, "D:/waybill.xls",  
588 - "D:/22.xls");  
589 - System.out.println("ok");  
590 - } catch (Exception e) {  
591 - e.printStackTrace();  
592 - }  
593 - }  
594 -  
595 - /**  
596 - * 行复制功能  
597 - *  
598 - * @param fromRow  
599 - * @param toRow  
600 - */  
601 - private void copyRow(HSSFWorkbook wb, HSSFRow fromRow, HSSFRow toRow,  
602 - boolean copyValueFlag, HSSFCellStyle style) {  
603 - for (Iterator<Cell> cellIt = fromRow.cellIterator(); cellIt.hasNext();) {  
604 - HSSFCell tmpCell = (HSSFCell) cellIt.next();  
605 - HSSFCell newCell = toRow.createCell(tmpCell.getColumnIndex(), 0);  
606 - copyCell(wb, tmpCell, newCell, copyValueFlag, tmpCell.getCellStyle());  
607 - }  
608 - }  
609 -  
610 - /**  
611 - * 复制单元格  
612 - *  
613 - * @param srcCell  
614 - * @param distCell  
615 - * @param copyValueFlag  
616 - * true则连同cell的内容一起复制  
617 - */  
618 - public void copyCell(HSSFWorkbook wb, HSSFCell srcCell, HSSFCell distCell,  
619 - boolean copyValueFlag, HSSFCellStyle newstyle) {  
620 -// HSSFCellStyle newstyle = wb.createCellStyle();  
621 - copyCellStyle(wb, srcCell.getCellStyle(), newstyle);  
622 - // 样式  
623 - distCell.setCellStyle(newstyle);  
624 - // 评论  
625 - if (srcCell.getCellComment() != null) {  
626 - distCell.setCellComment(srcCell.getCellComment());  
627 - }  
628 - // 不同数据类型处理  
629 - int srcCellType = srcCell.getCellType();  
630 - distCell.setCellType(srcCellType);  
631 - if (copyValueFlag) {  
632 - if (srcCellType == HSSFCell.CELL_TYPE_NUMERIC) {  
633 - distCell.setCellValue(srcCell.getDateCellValue());  
634 - } else if (srcCellType == HSSFCell.CELL_TYPE_STRING) {  
635 - distCell.setCellValue(srcCell.getRichStringCellValue());  
636 - } else if (srcCellType == HSSFCell.CELL_TYPE_BLANK) {  
637 -  
638 - } else if (srcCellType == HSSFCell.CELL_TYPE_BOOLEAN) {  
639 - distCell.setCellValue(srcCell.getBooleanCellValue());  
640 - } else if (srcCellType == HSSFCell.CELL_TYPE_ERROR) {  
641 - distCell.setCellErrorValue(srcCell.getErrorCellValue());  
642 - } else if (srcCellType == HSSFCell.CELL_TYPE_FORMULA) {  
643 - distCell.setCellFormula(srcCell.getCellFormula());  
644 - } else {  
645 - }  
646 - }  
647 - }  
648 -  
649 - /**  
650 - * 复制一个单元格样式到目的单元格样式  
651 - *  
652 - * @param fromStyle  
653 - * @param toStyle  
654 - */  
655 - public void copyCellStyle(HSSFWorkbook wb, HSSFCellStyle fromStyle,  
656 - HSSFCellStyle toStyle) {  
657 - toStyle.setAlignment(fromStyle.getAlignment());  
658 - // 边框和边框颜色  
659 - toStyle.setBorderBottom(fromStyle.getBorderBottom());  
660 - toStyle.setBorderLeft(fromStyle.getBorderLeft());  
661 - toStyle.setBorderRight(fromStyle.getBorderRight());  
662 - toStyle.setBorderTop(fromStyle.getBorderTop());  
663 - toStyle.setTopBorderColor(fromStyle.getTopBorderColor());  
664 - toStyle.setBottomBorderColor(fromStyle.getBottomBorderColor());  
665 - toStyle.setRightBorderColor(fromStyle.getRightBorderColor());  
666 - toStyle.setLeftBorderColor(fromStyle.getLeftBorderColor());  
667 -  
668 - // 背景和前景  
669 - toStyle.setFillBackgroundColor(fromStyle.getFillBackgroundColor());  
670 - toStyle.setFillForegroundColor(fromStyle.getFillForegroundColor());  
671 -  
672 - toStyle.setDataFormat(fromStyle.getDataFormat());  
673 - toStyle.setFillPattern(fromStyle.getFillPattern());  
674 - toStyle.setHidden(fromStyle.getHidden());  
675 - toStyle.setIndention(fromStyle.getIndention());// 首行缩进  
676 - toStyle.setLocked(fromStyle.getLocked());  
677 - toStyle.setRotation(fromStyle.getRotation());// 旋转  
678 - toStyle.setVerticalAlignment(fromStyle.getVerticalAlignment());  
679 - toStyle.setWrapText(fromStyle.getWrapText());  
680 - // 字体  
681 - toStyle.setFont(fromStyle.getFont(wb));  
682 -  
683 - }  
684 -  
685 - /**  
686 - * 创建文件夹,并删除原有文件  
687 - *  
688 - * @param path  
689 - */  
690 - private void createFolder(String path) {  
691 - File targetFile = null;  
692 - targetFile = new File(path);  
693 - if (targetFile.exists()) {// 删除原有文件  
694 - targetFile.delete();  
695 - }  
696 - // 创建目标文件夹  
697 - targetFile = new File(path.substring(0, path.lastIndexOf("/")));  
698 - if (!targetFile.exists()) {  
699 - targetFile.mkdirs();  
700 - }  
701 - }  
702 -  
703 - public void createFlie(List<List<String>> list, String name, String type){  
704 - HSSFWorkbook workbook = new HSSFWorkbook();  
705 - // 生成一个样式  
706 - HSSFCellStyle style = workbook.createCellStyle();  
707 - // 设置这些样式  
708 -// style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);  
709 -// style.setFillPattern(HSSFCellStyle.BORDER_THIN);  
710 - style.setBorderBottom(HSSFCellStyle.BORDER_THIN);  
711 - style.setBorderLeft(HSSFCellStyle.BORDER_THIN);  
712 - style.setBorderRight(HSSFCellStyle.BORDER_THIN);  
713 - style.setBorderTop(HSSFCellStyle.BORDER_THIN);  
714 - style.setAlignment(HSSFCellStyle.ALIGN_CENTER);  
715 - // 生成一个字体  
716 - HSSFFont font = workbook.createFont();  
717 -// font.setColor(HSSFColor.VIOLET.index);  
718 - font.setFontHeightInPoints((short) 12);  
719 - font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);  
720 - // 把字体应用到当前的样式  
721 - style.setFont(font);  
722 - HSSFCellStyle cellStyle =workbook.createCellStyle();  
723 - cellStyle.setFont(font);  
724 - cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); //水平布局:居中  
725 - cellStyle.setWrapText(true);  
726 -  
727 - //设置wordsheet名  
728 - HSSFSheet sheetYS = workbook.createSheet();//设置wordsheet名  
729 - HSSFRow row = sheetYS.createRow(0);  
730 - setCellStyleAndValue(row, style, 0, name);  
731 - CellRangeAddress callRangeAddress = new CellRangeAddress(0,0,0,list.get(0).size()-1);  
732 - sheetYS.addMergedRegion(callRangeAddress);  
733 - // 样式  
734 - setMergeCellStyle (callRangeAddress, sheetYS, workbook);  
735 -  
736 - try{  
737 - for(int i=0; i<list.size(); i++){  
738 - HSSFRow rowYSi = sheetYS.createRow(i+1);  
739 - List<String> stringList = list.get(i);  
740 - int num = 4;  
741 - if("xl".equals(type))  
742 - num = 2;  
743 - else if("cl".equals(type))  
744 - num = 5;  
745 - for(int j=0; j<stringList.size(); j++){  
746 - String str = stringList.get(j);  
747 - if(i == list.size()-1){  
748 - if(j==0) {  
749 - setCellStyleAndValue(rowYSi, style, j, str);  
750 - CellRangeAddress callRangeAddressYSi = new CellRangeAddress(i+1,i+1,0,num);  
751 - sheetYS.addMergedRegion(callRangeAddressYSi);  
752 - // 样式  
753 - setMergeCellStyle (callRangeAddressYSi, sheetYS, workbook);  
754 - }else  
755 - setCellStyleAndValue(rowYSi,style,j+num,str);  
756 - } else {  
757 - setCellStyleAndValue(rowYSi,style,j,str);  
758 - }  
759 - }  
760 - }  
761 -  
762 - // 给列设置宽度自适应  
763 - setSizeColumn1(sheetYS,1,list.get(0).size());  
764 - String path = this.getClass().getResource("/").getPath() + "static/pages/forms/export/";  
765 - String targetPath = path+name+".xls";  
766 - createFolder(targetPath);  
767 - FileOutputStream fout = new FileOutputStream(targetPath);  
768 - //5.输出  
769 - workbook.write(fout);  
770 - fout.close();  
771 - } catch (Exception e) {  
772 - e.printStackTrace();  
773 - }  
774 - }  
775 -  
776 - /**  
777 - * 自适应宽度(中文支持)  
778 - * @param sheet  
779 - * @param size  
780 - */  
781 - private static void setSizeColumn(HSSFSheet sheet, int size) {  
782 - for (int columnNum = 0; columnNum < size; columnNum++) {  
783 - int columnWidth = sheet.getColumnWidth(columnNum) / 256;  
784 - for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) {  
785 - HSSFRow currentRow;  
786 - //当前行未被使用过  
787 - if (sheet.getRow(rowNum) == null) {  
788 - currentRow = sheet.createRow(rowNum);  
789 - } else {  
790 - currentRow = sheet.getRow(rowNum);  
791 - }  
792 - if (currentRow.getCell(columnNum) != null) {  
793 - HSSFCell currentCell = currentRow.getCell(columnNum);  
794 - if (currentCell.getCellType() == XSSFCell.CELL_TYPE_STRING) {  
795 - int length = currentCell.getStringCellValue().getBytes().length;  
796 - if (columnWidth < length) {  
797 - columnWidth = length;  
798 - }  
799 - }  
800 - }  
801 - }  
802 - sheet.setColumnWidth(columnNum, columnWidth * 300);  
803 -// sheet.setColumnWidth(columnNum, columnWidth * 256);  
804 - }  
805 - }  
806 -  
807 - /**  
808 - * 自适应宽度(中文支持)  
809 - * @param sheet  
810 - * @param index 从那一行开始自适应  
811 - * @param size  
812 - */  
813 - private static void setSizeColumn1(HSSFSheet sheet,int index, int size) {  
814 - for (int columnNum = index; columnNum < size; columnNum++) {  
815 - int columnWidth = sheet.getColumnWidth(columnNum) / 256;  
816 - for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) {  
817 - HSSFRow currentRow;  
818 - //当前行未被使用过  
819 - if (sheet.getRow(rowNum) == null) {  
820 - currentRow = sheet.createRow(rowNum);  
821 - } else {  
822 - currentRow = sheet.getRow(rowNum);  
823 - }  
824 - if (currentRow.getCell(columnNum) != null) {  
825 - HSSFCell currentCell = currentRow.getCell(columnNum);  
826 - if (currentCell.getCellType() == XSSFCell.CELL_TYPE_STRING) {  
827 - int length = currentCell.getStringCellValue().getBytes().length;  
828 - if (columnWidth < length) {  
829 - columnWidth = length;  
830 - }  
831 - }  
832 - }  
833 - }  
834 - sheet.setColumnWidth(columnNum, columnWidth * 300);  
835 -// sheet.setColumnWidth(columnNum, columnWidth * 256);  
836 - }  
837 - }  
838 -  
839 - /**  
840 - * 设置单元格值和样式  
841 - * @param row  
842 - * @param style  
843 - * @param index  
844 - * @param value  
845 - */  
846 - public static void setCellStyleAndValue(HSSFRow row,HSSFCellStyle style,int index,String value){  
847 - HSSFCell cell = row.createCell(index);  
848 - cell.setCellValue(value);  
849 - cell.setCellStyle(style);  
850 - }  
851 - /**  
852 - * 设置合并单元格样式  
853 - * @param cra  
854 - * @param sheet  
855 - * @param workbook  
856 - */  
857 - public static void setMergeCellStyle (CellRangeAddress cra, HSSFSheet sheet, Workbook workbook){  
858 - // 使用RegionUtil类为合并后的单元格添加边框  
859 - RegionUtil.setBorderBottom(1, cra, sheet, workbook); // 下边框  
860 - RegionUtil.setBorderLeft(1, cra, sheet, workbook); // 左边框  
861 - RegionUtil.setBorderRight(1, cra, sheet, workbook); // 有边框  
862 - RegionUtil.setBorderTop(1, cra, sheet, workbook); // 上边框  
863 - }  
864 -} 1 +package com.bsth.util;
  2 +
  3 +import java.io.File;
  4 +import java.io.FileInputStream;
  5 +import java.io.FileOutputStream;
  6 +import java.util.ArrayList;
  7 +import java.util.HashMap;
  8 +import java.util.Iterator;
  9 +import java.util.List;
  10 +import java.util.Map;
  11 +import java.util.TreeMap;
  12 +import java.util.regex.Matcher;
  13 +import java.util.regex.Pattern;
  14 +
  15 +import com.bsth.common.ResponseCode;
  16 +import org.apache.poi.hssf.usermodel.HSSFCell;
  17 +import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  18 +import org.apache.poi.hssf.usermodel.HSSFFont;
  19 +import org.apache.poi.hssf.usermodel.HSSFRow;
  20 +import org.apache.poi.hssf.usermodel.HSSFSheet;
  21 +import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  22 +import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  23 +import org.apache.poi.ss.usermodel.Cell;
  24 +import org.apache.poi.ss.usermodel.Workbook;
  25 +import org.apache.poi.ss.util.CellRangeAddress;
  26 +
  27 +import com.bsth.entity.Line;
  28 +import com.bsth.entity.realcontrol.ScheduleRealInfo;
  29 +import org.apache.poi.ss.util.RegionUtil;
  30 +import org.apache.poi.xssf.usermodel.XSSFCell;
  31 +
  32 +public class ReportUtils {
  33 + // private final String packaegName = "com.bsth.entity.";
  34 + private final String packaegName = "com.bsth.entity.realcontrol.";
  35 +
  36 + /**
  37 + * /**
  38 + *
  39 + * @param list
  40 + * 模板中,需要重复显示的行所需的数据
  41 + * @param map
  42 + * 模板中除以上list外所有的数据
  43 + * @param index
  44 + * 需要重复的行号,该值为行号减1
  45 + * @param sourcePath
  46 + * 模板路径
  47 + * @param targetPath
  48 + * 生成路径
  49 + */
  50 + public void excelReplace(List<Iterator<?>> list, Object[] tArray,
  51 + String sourcePath, String targetPath) {
  52 + try {
  53 + // 把源文件放入流中
  54 + POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(
  55 + sourcePath));
  56 + HSSFWorkbook wb = new HSSFWorkbook(fs);
  57 + if(tArray.length != 0 && tArray[0] instanceof java.util.Map){
  58 + Map<String, Object> m = (Map<String, Object>)tArray[0];
  59 + if(m.containsKey("sheetName") && m.get("sheetName")!=null
  60 + && m.get("sheetName").toString().trim().length()!=0)
  61 + wb.setSheetName(0, m.get("sheetName").toString());
  62 + }
  63 + HSSFSheet sheet = wb.getSheetAt(0);
  64 + HSSFRow row;
  65 + HSSFCell cell = null;
  66 + String key;
  67 + // 取得总行数
  68 + int rowNum = sheet.getLastRowNum();
  69 + // 取得总列数
  70 + int cellNum = sheet.getRow(0).getLastCellNum();
  71 +
  72 + // 遍历行
  73 + for (int i = 0; i < rowNum; i++) {
  74 + row = sheet.getRow(i);
  75 + // 遍历列
  76 + for (int j = 0; j < cellNum; j++) {
  77 + if (row == null) {
  78 + continue;
  79 + }
  80 + cell = row.getCell(j);
  81 + if (cell == null) {
  82 + continue;
  83 + }
  84 + // 取得每列的内容,如果列内容是$key$格式,则替换内容
  85 + key = getCellValue(cell);
  86 + if (key != null && (key.indexOf("$") != -1 || key.indexOf("#list#") != -1)) {
  87 + // * 列中内容有#list#,则表示该行为模板行,需要以该行为模板
  88 + // * 例如:模板行格式 #list#0_0 $Car.id$
  89 + // * 第一个0表示需要在list中取iterator的索引值
  90 + // * 第二个0表示在iterator中取的第几个对象,如果不为0,则取下一个对象,而不是沿用前面获取的对象
  91 + // * $Car.id$表示所取的对象为Car的对象,并且取值为id的值
  92 + if (key.indexOf("#list#") != -1) {
  93 + key = key.replace("#list#", "").trim();
  94 + String[] lists = key.split(" ");
  95 + // 取得list中的索引值
  96 + int listIndex = Integer
  97 + .valueOf(lists[0].split("_")[0]);
  98 + Iterator<?> iterator = list.get(listIndex);
  99 + // 根据模板创建行并填弃数据,返回增加的行数
  100 + int rowCount = iteratorFillCellValue(wb, sheet,
  101 + cell, iterator, i, rowNum, key);
  102 + rowNum += rowCount;
  103 + i += rowCount;
  104 + break;
  105 + } else {
  106 + // 直接填充数据的列,从对象数组中取得值,这里的数组不传值
  107 + getValueAndSetCellValue(cell, key, tArray, new String[]{""});
  108 + }
  109 + }
  110 +
  111 + }
  112 + }
  113 + // 创建目标文件夹
  114 + createFolder(targetPath);
  115 + // 输出文件
  116 + FileOutputStream fileOut = new FileOutputStream(targetPath);
  117 + wb.write(fileOut);
  118 + fileOut.close();
  119 + } catch (Exception e) {
  120 + e.printStackTrace();
  121 + }
  122 + }
  123 +
  124 +
  125 + /**
  126 + *
  127 + * @param sheetList 多sheet模板中,需要重复显示的行所需的数据
  128 + * @param tArray
  129 + * @param sourcePath 模板路径
  130 + * @param targetPath 生成路径
  131 + */
  132 + public void excelMoreSheetReplace(List<List<Iterator<?>>> sheetList, Object[] tArray,
  133 + String sourcePath, String targetPath) {
  134 + try {
  135 + // 把源文件放入流中
  136 + POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(
  137 + sourcePath));
  138 + HSSFWorkbook wb = new HSSFWorkbook(fs);
  139 + for (int s=0; s<sheetList.size(); s++){
  140 + List<Iterator<?>> list = sheetList.get(s);
  141 + if(tArray.length != 0 && tArray[0] instanceof java.util.Map){
  142 + Map<String, Object> m = (Map<String, Object>)tArray[0];
  143 + if(m.containsKey("sheetName"+s+1) && m.get("sheetName"+s+1)!=null
  144 + && m.get("sheetName"+s+1).toString().trim().length()!=0)
  145 + wb.setSheetName(0, m.get("sheetName"+s+1).toString());
  146 + }
  147 + HSSFSheet sheet = wb.getSheetAt(s);
  148 + HSSFRow row;
  149 + HSSFCell cell = null;
  150 + String key;
  151 + // 取得总行数
  152 + int rowNum = sheet.getLastRowNum();
  153 + // 取得总列数
  154 + int cellNum = sheet.getRow(0).getLastCellNum();
  155 +
  156 + // 遍历行
  157 + for (int i = 0; i <= rowNum; i++) {
  158 + row = sheet.getRow(i);
  159 + // 遍历列
  160 + for (int j = 0; j < cellNum; j++) {
  161 + if (row == null) {
  162 + continue;
  163 + }
  164 + cell = row.getCell(j);
  165 + if (cell == null) {
  166 + continue;
  167 + }
  168 + // 取得每列的内容,如果列内容是$key$格式,则替换内容
  169 + key = getCellValue(cell);
  170 + if (key != null && (key.indexOf("$") != -1 || key.indexOf("#list#") != -1)) {
  171 + // * 列中内容有#list#,则表示该行为模板行,需要以该行为模板
  172 + // * 例如:模板行格式 #list#0_0 $Car.id$
  173 + // * 第一个0表示需要在list中取iterator的索引值
  174 + // * 第二个0表示在iterator中取的第几个对象,如果不为0,则取下一个对象,而不是沿用前面获取的对象
  175 + // * $Car.id$表示所取的对象为Car的对象,并且取值为id的值
  176 + if (key.indexOf("#list#") != -1) {
  177 + key = key.replace("#list#", "").trim();
  178 + String[] lists = key.split(" ");
  179 + // 取得list中的索引值
  180 + int listIndex = Integer
  181 + .valueOf(lists[0].split("_")[0]);
  182 + Iterator<?> iterator = list.get(listIndex);
  183 + // 根据模板创建行并填充数据,返回增加的行数
  184 + int rowCount = iteratorFillCellValue(wb, sheet,
  185 + cell, iterator, i, rowNum, key);
  186 + rowNum += rowCount;
  187 + i += rowCount;
  188 + break;
  189 + } else {
  190 + // 直接填充数据的列,从对象数组中取得值,这里的数组不传值
  191 + getValueAndSetCellValue(cell, key, tArray, new String[]{""});
  192 + }
  193 + }
  194 +
  195 + }
  196 + }
  197 + }
  198 +
  199 + // 创建目标文件夹
  200 + createFolder(targetPath);
  201 + // 输出文件
  202 + FileOutputStream fileOut = new FileOutputStream(targetPath);
  203 + wb.write(fileOut);
  204 + fileOut.close();
  205 + } catch (Exception e) {
  206 + e.printStackTrace();
  207 + }
  208 + }
  209 +
  210 + /**
  211 + * 将file1中的一页sheet复制到file2中
  212 + *
  213 + * @param file1
  214 + * 原sheet所在的excel文件
  215 + * @param file2
  216 + * 目标excel文件
  217 + * @param page
  218 + * 原excel中要被复制的sheet的位置(从0开始)
  219 + * @param rate
  220 + * 调整复制后的缩放倍率(列如:145,则为缩放145%)
  221 + */
  222 + public void copySheetByFile(File file1, File file2, int page, int rate) {
  223 + try {
  224 + // 把源文件放入流中
  225 + POIFSFileSystem fs1 = new POIFSFileSystem(new FileInputStream(file1));
  226 + HSSFWorkbook wb1 = new HSSFWorkbook(fs1);
  227 + HSSFSheet sheet = wb1.getSheetAt(page);
  228 + POIFSFileSystem fs2 = new POIFSFileSystem(new FileInputStream(file2));
  229 + HSSFWorkbook wb2 = new HSSFWorkbook(fs2);
  230 + HSSFSheet createSheet = wb2.createSheet(sheet.getSheetName());
  231 + HSSFCellStyle createCellStyle = wb2.createCellStyle();
  232 + HSSFRow row;
  233 +
  234 + createSheet.setZoom(rate, 100);
  235 + for(int i = 0; i < sheet.getRow(0).getPhysicalNumberOfCells(); i++){
  236 + createSheet.setColumnWidth(i, sheet.getColumnWidth(i));
  237 + }
  238 +
  239 + List<CellRangeAddress> mergedRegions = sheet.getMergedRegions();
  240 + for(int l = 0; l < mergedRegions.size(); l++){
  241 + //复制源表中的合并单元格
  242 + createSheet.addMergedRegion(mergedRegions.get(l));
  243 + }
  244 + int firstRow = sheet.getFirstRowNum();
  245 + int lastRow = sheet.getLastRowNum();
  246 + for(int k = firstRow; k <= lastRow; k++){
  247 + // 创建新建excel Sheet的行
  248 + HSSFRow rowCreat = createSheet.createRow(k);
  249 + // 取得源有excel Sheet的行
  250 + row = sheet.getRow(k);
  251 +// rowCreat.setHeight(row.getHeight()); //设置行高
  252 + // 单元格式样
  253 + int firstCell = row.getFirstCellNum();
  254 + int lastCell = row.getLastCellNum();
  255 + for (int j = firstCell; j < lastCell; j++) {
  256 + // 自动适应列宽 貌似不起作用
  257 +// createSheet.autoSizeColumn(j);
  258 +// System.out.println(row.getCell(j));
  259 + rowCreat.createCell(j);
  260 + String strVal = "";
  261 + if (row.getCell(j)==null) {
  262 +
  263 + } else {
  264 + strVal = row.getCell(j).getStringCellValue();
  265 + rowCreat.getCell(j).setCellValue(strVal);
  266 + copyCellStyle(wb1, row.getCell(j).getCellStyle(), createCellStyle);
  267 + createCellStyle.setBorderTop((short)1);
  268 + createCellStyle.setBorderLeft((short)1);
  269 + createCellStyle.setBorderRight((short)1);
  270 + createCellStyle.setBorderBottom((short)1);
  271 + rowCreat.getCell(j).setCellStyle(createCellStyle);
  272 + }
  273 + }
  274 + }
  275 +
  276 +// int firstRowNum = createSheet.getFirstRowNum();
  277 +// int lastRowNum = createSheet.getLastRowNum();
  278 +// int test = 0;
  279 +// for(int k = firstRowNum; k <= lastRowNum; k++){
  280 +// HSSFRow createRow = createSheet.getRow(k);
  281 +// int firstCellNum = createRow.getFirstCellNum();
  282 +// int lastCellNum = createRow.getLastCellNum();
  283 +// for(int i = firstCellNum; i < lastCellNum; i++){
  284 +// HSSFCell cell = createRow.getCell(i);
  285 +// cell.getCellStyle().setBorderTop(HSSFCellStyle.BORDER_THIN);
  286 +// cell.getCellStyle().setBorderLeft(HSSFCellStyle.BORDER_THIN);
  287 +// cell.getCellStyle().setBorderRight(HSSFCellStyle.BORDER_THIN);
  288 +// cell.getCellStyle().setBorderBottom(HSSFCellStyle.BORDER_THIN);
  289 +// test ++;
  290 +// }
  291 +// }
  292 +// System.out.println("test = " + test);
  293 +
  294 + FileOutputStream fileOut = new FileOutputStream(file2);
  295 + wb2.write(fileOut);
  296 + fileOut.close();
  297 + wb2.close();
  298 + wb1.close();
  299 + fs2.close();
  300 + fs1.close();
  301 + file1.delete();
  302 +// // 创建目标文件夹
  303 +// createFolder(targetPath);
  304 + // 输出文件
  305 + } catch (Exception e) {
  306 + e.printStackTrace();
  307 + }
  308 + }
  309 +
  310 + public void test(File file){
  311 + POIFSFileSystem fs;
  312 + try {
  313 + fs = new POIFSFileSystem(new FileInputStream(file));
  314 + HSSFWorkbook wb = new HSSFWorkbook(fs);
  315 + for(int j = 0; j < wb.getNumberOfSheets(); j++){
  316 + HSSFSheet sheet = wb.getSheetAt(j);
  317 + int firstRowNum = sheet.getFirstRowNum();
  318 + int lastRowNum = sheet.getLastRowNum();
  319 + int test = 0;
  320 + for(int k = firstRowNum; k <= lastRowNum; k++){
  321 + HSSFRow createRow = sheet.getRow(k);
  322 + int firstCellNum = createRow.getFirstCellNum();
  323 + int lastCellNum = createRow.getLastCellNum();
  324 + for(int i = firstCellNum; i < lastCellNum; i++){
  325 + HSSFCell cell = createRow.getCell(i);
  326 + HSSFCellStyle cellStyle = wb.createCellStyle();
  327 +
  328 + cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
  329 + cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
  330 + cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
  331 + cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
  332 + cell.setCellStyle(cellStyle);
  333 + test ++;
  334 + }
  335 + }
  336 + System.out.println("test = " + test);
  337 +
  338 + FileOutputStream fileOut = new FileOutputStream(file);
  339 + wb.write(fileOut);
  340 + fileOut.close();
  341 + }
  342 + } catch (Exception e) {
  343 + // TODO Auto-generated catch block
  344 + e.printStackTrace();
  345 + }
  346 +
  347 + }
  348 +
  349 + public String getCellValue(HSSFCell cell) {
  350 + int cellType = 0;
  351 + String result = "";
  352 + double d;
  353 + if(cell != null) {
  354 + // 获取列数据类型
  355 + cellType = cell.getCellType();
  356 + // 不同列数据类型,取值方法不同
  357 + switch(cellType) {
  358 + // String
  359 + case HSSFCell.CELL_TYPE_STRING:
  360 + result = cell.getStringCellValue().toString();
  361 + break;
  362 + // numeric类型,excel中,日期格式会转成数字格式存储
  363 + case HSSFCell.CELL_TYPE_NUMERIC:
  364 + result = cell.getNumericCellValue()+"";
  365 + break;
  366 + // 公式类型
  367 + case HSSFCell.CELL_TYPE_FORMULA:
  368 + result = cell.getCellFormula() + "";
  369 + break;
  370 + // boolean类型
  371 + case HSSFCell.CELL_TYPE_BOOLEAN:
  372 + result = cell.getBooleanCellValue() + "";
  373 + break;
  374 + // 空格
  375 + case HSSFCell.CELL_TYPE_BLANK:
  376 + result = null;
  377 + break;
  378 + // 错误值
  379 + case HSSFCell.CELL_TYPE_ERROR:
  380 + result = null;
  381 + break;
  382 + default :
  383 + System.out.println("其它");
  384 + break;
  385 + }
  386 + }
  387 + return result;
  388 + }
  389 +
  390 + /**
  391 + * 根据iterator,以及模板中的标识,填充模板
  392 + *
  393 + * @param wb
  394 + * @param sheet
  395 + * @param cell
  396 + * @param iterator
  397 + * iterator
  398 + * @param index
  399 + * 模板行索引
  400 + * @param rowNum
  401 + * 表格总行数
  402 + * @param key
  403 + * 表格内容
  404 + * @return
  405 + */
  406 + private int iteratorFillCellValue(HSSFWorkbook wb, HSSFSheet sheet,
  407 + HSSFCell cell, Iterator<?> iterator, int index, int rowNum,
  408 + String key) {
  409 + int rowCount = 0;
  410 + Object obj = null;
  411 + int p = 0;
  412 + int i = index;
  413 + HSSFRow newRow = null;
  414 + int tmpCellNum = 0;
  415 + int k = 0;
  416 + int listIndex = 0;
  417 + // 取得模板行
  418 + HSSFRow orgRow = sheet.getRow(index);
  419 + HSSFCellStyle style= wb.createCellStyle();
  420 + try {
  421 + while (iterator.hasNext()) {
  422 + // 取得iterator的对象
  423 + obj = iterator.next();
  424 + // 移动当前编辑行以下的所有行
  425 + if (p != 0) {
  426 + rowNum += 1;
  427 + i += 1;
  428 + rowCount += 1;// 增加的总行数
  429 + // 把当前行以下的所有行往下移动1行
  430 + sheet.shiftRows(i, rowNum, 1);
  431 + }
  432 + p = 1;
  433 + // 创建新行
  434 + newRow = sheet.createRow(index + k++);
  435 + // 把新行的内容换成和模板行一样
  436 + copyRow(wb, orgRow, newRow, true,style);
  437 + tmpCellNum = newRow.getLastCellNum();
  438 + for (int l = 0; l < tmpCellNum; l++) {
  439 + cell = newRow.getCell(l);
  440 + key = getCellValue(cell);
  441 + /**
  442 + * 如果单无格内容为#list#,表示该行是模板行 #list#0_0
  443 + * 第一个0表示需要在list中取iterator的索引值
  444 + * 第二个0表示在iterator中取的第几个对象,如果不为0,则取下一个对象,而不是沿用前面获取的对象
  445 + */
  446 + if(key == null || key.equals("")){
  447 + obj = iterator.next();
  448 + }
  449 + if (key.indexOf("#list#") != -1 && key.indexOf("_0") == -1) {
  450 + if (iterator.hasNext()) {
  451 + obj = iterator.next();
  452 + } else {
  453 + obj = null;
  454 + }
  455 + }
  456 + if (key.trim().indexOf(" ") != -1) {
  457 + key = key.split(" ")[1];
  458 + }
  459 + getValueAndSetCellValue(cell, key, obj,new String[]{listIndex+""});
  460 + }
  461 + // list的数量
  462 + listIndex ++;
  463 + }
  464 + } catch (Exception e) {
  465 + e.printStackTrace();
  466 + }
  467 + return rowCount;
  468 + }
  469 +
  470 + /**
  471 + * 取到相应的值并填入相应的列中
  472 + *
  473 + * @param cell 列
  474 + * @param key 列内容
  475 + * @param obj 数据源对象
  476 + * @param args 其他参数 数组
  477 + * 数组内容:
  478 + * 0位:在list中的第几条数据
  479 + */
  480 + private void getValueAndSetCellValue(HSSFCell cell, String key, Object obj, String[] args) {
  481 + try {
  482 + // 保有存单元格的内容
  483 + String cellValue = key = key.replace("\\n", "");
  484 + String tmpKey;
  485 + // 判断单元格内容是否是公式
  486 + if(cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA){
  487 + Pattern p=Pattern.compile("(\\d+)"); //
  488 + Matcher m=p.matcher(key);
  489 + if(m.find()){
  490 + cellValue = key.replace(m.group(1),
  491 + Integer.valueOf(m.group(1))+Integer.valueOf(args[0])+"");
  492 + cell.setCellFormula(cellValue);
  493 + return;
  494 + }
  495 + }else{//其他格式
  496 +
  497 + // 循环截取两个$中间的内容,反射出值
  498 + while (key.indexOf("$") != -1) {
  499 + key = key.substring(key.indexOf("$") + 1);
  500 + // 取两个$中间的内容
  501 + tmpKey = key.substring(0, key.indexOf("$"));
  502 + key = key.substring(key.indexOf("$") + 1);
  503 + // 如果内容是如下格式Cars.id,则从obj中值得相应的对象的值
  504 + if (tmpKey.indexOf(".") != -1) {
  505 + String className = tmpKey.substring(0, tmpKey.indexOf("."));
  506 + // 取得类的全限定名
  507 + String classWholeName = packaegName + className;
  508 + String fieldName = tmpKey.substring(tmpKey.indexOf(".") + 1);
  509 + // 如果obj是数组,循环判断哪个对象是对应的
  510 + if (obj instanceof Object[]) {
  511 + Object[] objs = (Object[]) obj;
  512 + for (int k = 0; k < objs.length; k++) {
  513 + if (objs[k].getClass().getName().equals(classWholeName)) {
  514 + cellValue = cellValue.replace("$" + tmpKey
  515 + + "$", getKeyValue(objs[k], fieldName)
  516 + + "");
  517 + } else if(objs[k].getClass().getName().equals("java.util.HashMap")){
  518 + Map<String,Object> map = (HashMap<String,Object>)objs[k];
  519 + cellValue = cellValue.replace("$" + tmpKey + "$",map.get(fieldName)+"");
  520 + }
  521 + }
  522 + } else if (obj.getClass().getName().equals(classWholeName)) {
  523 + cellValue = cellValue.replace("$" + tmpKey + "$",getKeyValue(obj, fieldName) + "");
  524 + } else if (obj instanceof Map){
  525 + Map<String,Object> map = (Map<String, Object>)obj;
  526 + cellValue = cellValue.replace("$" + tmpKey + "$",map.get(fieldName)+"");
  527 + }
  528 + }
  529 + }
  530 + }
  531 + cell.setCellValue(cellValue);
  532 + } catch (Exception e) {
  533 + e.printStackTrace();
  534 + }
  535 +
  536 + }
  537 +
  538 + /**
  539 + * 给列填充数据
  540 + *
  541 + * @param cell
  542 + * 列
  543 + * @param obj
  544 + * 数据源对象
  545 + * @param fieldName
  546 + * 需要取数据的字段
  547 + */
  548 + private Object getKeyValue(Object obj, String fieldName) {
  549 + Object value = "";
  550 + try {
  551 + if (obj != null) {
  552 + ReportRelatedUtils test = new ReportRelatedUtils();
  553 + value = test.getValue(obj, fieldName) == null ? "" : test .getValue(obj, fieldName);
  554 + }
  555 + } catch (Exception e) {
  556 + e.printStackTrace();
  557 + }
  558 + return value;
  559 + }
  560 +
  561 + public static void main(String[] args) {
  562 +
  563 + try {
  564 + ReportUtils ee = new ReportUtils();
  565 + List<Iterator<?>> list = new ArrayList<Iterator<?>>();
  566 + Line line = new Line();
  567 + line.setId(1);
  568 + line.setName("line1");
  569 +
  570 + List<Object> dataList = new ArrayList<Object>();
  571 +
  572 +
  573 + ScheduleRealInfo srr = new ScheduleRealInfo();
  574 + srr.setId((long) 111);
  575 + srr.setXlName("abc11");
  576 +
  577 + ScheduleRealInfo sr = new ScheduleRealInfo();
  578 + sr.setZdsj("06:10");
  579 + sr.setZdsjActual("06:25");
  580 + dataList.add(sr);
  581 + sr = new ScheduleRealInfo();
  582 + sr.setZdsj("06:20");
  583 + sr.setZdsjActual("");
  584 + dataList.add(sr);
  585 + list.add(dataList.iterator());
  586 +
  587 + ee.excelReplace(list, new Object[] { srr }, "D:/waybill.xls",
  588 + "D:/22.xls");
  589 + System.out.println("ok");
  590 + } catch (Exception e) {
  591 + e.printStackTrace();
  592 + }
  593 + }
  594 +
  595 + /**
  596 + * 行复制功能
  597 + *
  598 + * @param fromRow
  599 + * @param toRow
  600 + */
  601 + private void copyRow(HSSFWorkbook wb, HSSFRow fromRow, HSSFRow toRow,
  602 + boolean copyValueFlag, HSSFCellStyle style) {
  603 + for (Iterator<Cell> cellIt = fromRow.cellIterator(); cellIt.hasNext();) {
  604 + HSSFCell tmpCell = (HSSFCell) cellIt.next();
  605 + HSSFCell newCell = toRow.createCell(tmpCell.getColumnIndex(), 0);
  606 + copyCell(wb, tmpCell, newCell, copyValueFlag, tmpCell.getCellStyle());
  607 + }
  608 + }
  609 +
  610 + /**
  611 + * 复制单元格
  612 + *
  613 + * @param srcCell
  614 + * @param distCell
  615 + * @param copyValueFlag
  616 + * true则连同cell的内容一起复制
  617 + */
  618 + public void copyCell(HSSFWorkbook wb, HSSFCell srcCell, HSSFCell distCell,
  619 + boolean copyValueFlag, HSSFCellStyle newstyle) {
  620 +// HSSFCellStyle newstyle = wb.createCellStyle();
  621 + copyCellStyle(wb, srcCell.getCellStyle(), newstyle);
  622 + // 样式
  623 + distCell.setCellStyle(newstyle);
  624 + // 评论
  625 + if (srcCell.getCellComment() != null) {
  626 + distCell.setCellComment(srcCell.getCellComment());
  627 + }
  628 + // 不同数据类型处理
  629 + int srcCellType = srcCell.getCellType();
  630 + distCell.setCellType(srcCellType);
  631 + if (copyValueFlag) {
  632 + if (srcCellType == HSSFCell.CELL_TYPE_NUMERIC) {
  633 + distCell.setCellValue(srcCell.getDateCellValue());
  634 + } else if (srcCellType == HSSFCell.CELL_TYPE_STRING) {
  635 + distCell.setCellValue(srcCell.getRichStringCellValue());
  636 + } else if (srcCellType == HSSFCell.CELL_TYPE_BLANK) {
  637 +
  638 + } else if (srcCellType == HSSFCell.CELL_TYPE_BOOLEAN) {
  639 + distCell.setCellValue(srcCell.getBooleanCellValue());
  640 + } else if (srcCellType == HSSFCell.CELL_TYPE_ERROR) {
  641 + distCell.setCellErrorValue(srcCell.getErrorCellValue());
  642 + } else if (srcCellType == HSSFCell.CELL_TYPE_FORMULA) {
  643 + distCell.setCellFormula(srcCell.getCellFormula());
  644 + } else {
  645 + }
  646 + }
  647 + }
  648 +
  649 + /**
  650 + * 复制一个单元格样式到目的单元格样式
  651 + *
  652 + * @param fromStyle
  653 + * @param toStyle
  654 + */
  655 + public void copyCellStyle(HSSFWorkbook wb, HSSFCellStyle fromStyle,
  656 + HSSFCellStyle toStyle) {
  657 + toStyle.setAlignment(fromStyle.getAlignment());
  658 + // 边框和边框颜色
  659 + toStyle.setBorderBottom(fromStyle.getBorderBottom());
  660 + toStyle.setBorderLeft(fromStyle.getBorderLeft());
  661 + toStyle.setBorderRight(fromStyle.getBorderRight());
  662 + toStyle.setBorderTop(fromStyle.getBorderTop());
  663 + toStyle.setTopBorderColor(fromStyle.getTopBorderColor());
  664 + toStyle.setBottomBorderColor(fromStyle.getBottomBorderColor());
  665 + toStyle.setRightBorderColor(fromStyle.getRightBorderColor());
  666 + toStyle.setLeftBorderColor(fromStyle.getLeftBorderColor());
  667 +
  668 + // 背景和前景
  669 + toStyle.setFillBackgroundColor(fromStyle.getFillBackgroundColor());
  670 + toStyle.setFillForegroundColor(fromStyle.getFillForegroundColor());
  671 +
  672 + toStyle.setDataFormat(fromStyle.getDataFormat());
  673 + toStyle.setFillPattern(fromStyle.getFillPattern());
  674 + toStyle.setHidden(fromStyle.getHidden());
  675 + toStyle.setIndention(fromStyle.getIndention());// 首行缩进
  676 + toStyle.setLocked(fromStyle.getLocked());
  677 + toStyle.setRotation(fromStyle.getRotation());// 旋转
  678 + toStyle.setVerticalAlignment(fromStyle.getVerticalAlignment());
  679 + toStyle.setWrapText(fromStyle.getWrapText());
  680 + // 字体
  681 + toStyle.setFont(fromStyle.getFont(wb));
  682 +
  683 + }
  684 +
  685 + /**
  686 + * 创建文件夹,并删除原有文件
  687 + *
  688 + * @param path
  689 + */
  690 + private void createFolder(String path) {
  691 + File targetFile = null;
  692 + targetFile = new File(path);
  693 + if (targetFile.exists()) {// 删除原有文件
  694 + targetFile.delete();
  695 + }
  696 + // 创建目标文件夹
  697 + targetFile = new File(path.substring(0, path.lastIndexOf("/")));
  698 + if (!targetFile.exists()) {
  699 + targetFile.mkdirs();
  700 + }
  701 + }
  702 +
  703 + public void createFlie(List<List<String>> list, String name, String type){
  704 + HSSFWorkbook workbook = new HSSFWorkbook();
  705 + // 生成一个样式
  706 + HSSFCellStyle style = workbook.createCellStyle();
  707 + // 设置这些样式
  708 +// style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
  709 +// style.setFillPattern(HSSFCellStyle.BORDER_THIN);
  710 + style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
  711 + style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
  712 + style.setBorderRight(HSSFCellStyle.BORDER_THIN);
  713 + style.setBorderTop(HSSFCellStyle.BORDER_THIN);
  714 + style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
  715 + // 生成一个字体
  716 + HSSFFont font = workbook.createFont();
  717 +// font.setColor(HSSFColor.VIOLET.index);
  718 + font.setFontHeightInPoints((short) 12);
  719 + font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
  720 + // 把字体应用到当前的样式
  721 + style.setFont(font);
  722 + HSSFCellStyle cellStyle =workbook.createCellStyle();
  723 + cellStyle.setFont(font);
  724 + cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); //水平布局:居中
  725 + cellStyle.setWrapText(true);
  726 +
  727 + //设置wordsheet名
  728 + HSSFSheet sheetYS = workbook.createSheet();//设置wordsheet名
  729 + HSSFRow row = sheetYS.createRow(0);
  730 + setCellStyleAndValue(row, style, 0, name);
  731 + CellRangeAddress callRangeAddress = new CellRangeAddress(0,0,0,list.get(0).size()-1);
  732 + sheetYS.addMergedRegion(callRangeAddress);
  733 + // 样式
  734 + setMergeCellStyle (callRangeAddress, sheetYS, workbook);
  735 +
  736 + try{
  737 + for(int i=0; i<list.size(); i++){
  738 + HSSFRow rowYSi = sheetYS.createRow(i+1);
  739 + List<String> stringList = list.get(i);
  740 + int num = 4;
  741 + if("xl".equals(type))
  742 + num = 2;
  743 + else if("cl".equals(type))
  744 + num = 5;
  745 + for(int j=0; j<stringList.size(); j++){
  746 + String str = stringList.get(j);
  747 + if(i == list.size()-1){
  748 + if(j==0) {
  749 + setCellStyleAndValue(rowYSi, style, j, str);
  750 + CellRangeAddress callRangeAddressYSi = new CellRangeAddress(i+1,i+1,0,num);
  751 + sheetYS.addMergedRegion(callRangeAddressYSi);
  752 + // 样式
  753 + setMergeCellStyle (callRangeAddressYSi, sheetYS, workbook);
  754 + }else
  755 + setCellStyleAndValue(rowYSi,style,j+num,str);
  756 + } else {
  757 + setCellStyleAndValue(rowYSi,style,j,str);
  758 + }
  759 + }
  760 + }
  761 +
  762 + // 给列设置宽度自适应
  763 + setSizeColumn1(sheetYS,1,list.get(0).size());
  764 + String path = this.getClass().getResource("/").getPath() + "static/pages/forms/export/";
  765 + String targetPath = path+name+".xls";
  766 + createFolder(targetPath);
  767 + FileOutputStream fout = new FileOutputStream(targetPath);
  768 + //5.输出
  769 + workbook.write(fout);
  770 + fout.close();
  771 + } catch (Exception e) {
  772 + e.printStackTrace();
  773 + }
  774 + }
  775 +
  776 + /**
  777 + * 自适应宽度(中文支持)
  778 + * @param sheet
  779 + * @param size
  780 + */
  781 + private static void setSizeColumn(HSSFSheet sheet, int size) {
  782 + for (int columnNum = 0; columnNum < size; columnNum++) {
  783 + int columnWidth = sheet.getColumnWidth(columnNum) / 256;
  784 + for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) {
  785 + HSSFRow currentRow;
  786 + //当前行未被使用过
  787 + if (sheet.getRow(rowNum) == null) {
  788 + currentRow = sheet.createRow(rowNum);
  789 + } else {
  790 + currentRow = sheet.getRow(rowNum);
  791 + }
  792 + if (currentRow.getCell(columnNum) != null) {
  793 + HSSFCell currentCell = currentRow.getCell(columnNum);
  794 + if (currentCell.getCellType() == XSSFCell.CELL_TYPE_STRING) {
  795 + int length = currentCell.getStringCellValue().getBytes().length;
  796 + if (columnWidth < length) {
  797 + columnWidth = length;
  798 + }
  799 + }
  800 + }
  801 + }
  802 + sheet.setColumnWidth(columnNum, columnWidth * 300);
  803 +// sheet.setColumnWidth(columnNum, columnWidth * 256);
  804 + }
  805 + }
  806 +
  807 + /**
  808 + * 自适应宽度(中文支持)
  809 + * @param sheet
  810 + * @param index 从那一行开始自适应
  811 + * @param size
  812 + */
  813 + private static void setSizeColumn1(HSSFSheet sheet,int index, int size) {
  814 + for (int columnNum = index; columnNum < size; columnNum++) {
  815 + int columnWidth = sheet.getColumnWidth(columnNum) / 256;
  816 + for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) {
  817 + HSSFRow currentRow;
  818 + //当前行未被使用过
  819 + if (sheet.getRow(rowNum) == null) {
  820 + currentRow = sheet.createRow(rowNum);
  821 + } else {
  822 + currentRow = sheet.getRow(rowNum);
  823 + }
  824 + if (currentRow.getCell(columnNum) != null) {
  825 + HSSFCell currentCell = currentRow.getCell(columnNum);
  826 + if (currentCell.getCellType() == XSSFCell.CELL_TYPE_STRING) {
  827 + int length = currentCell.getStringCellValue().getBytes().length;
  828 + if (columnWidth < length) {
  829 + columnWidth = length;
  830 + }
  831 + }
  832 + }
  833 + }
  834 + sheet.setColumnWidth(columnNum, columnWidth * 300);
  835 +// sheet.setColumnWidth(columnNum, columnWidth * 256);
  836 + }
  837 + }
  838 +
  839 + /**
  840 + * 设置单元格值和样式
  841 + * @param row
  842 + * @param style
  843 + * @param index
  844 + * @param value
  845 + */
  846 + public static void setCellStyleAndValue(HSSFRow row,HSSFCellStyle style,int index,String value){
  847 + HSSFCell cell = row.createCell(index);
  848 + cell.setCellValue(value);
  849 + cell.setCellStyle(style);
  850 + }
  851 + /**
  852 + * 设置合并单元格样式
  853 + * @param cra
  854 + * @param sheet
  855 + * @param workbook
  856 + */
  857 + public static void setMergeCellStyle (CellRangeAddress cra, HSSFSheet sheet, Workbook workbook){
  858 + // 使用RegionUtil类为合并后的单元格添加边框
  859 + RegionUtil.setBorderBottom(1, cra, sheet, workbook); // 下边框
  860 + RegionUtil.setBorderLeft(1, cra, sheet, workbook); // 左边框
  861 + RegionUtil.setBorderRight(1, cra, sheet, workbook); // 有边框
  862 + RegionUtil.setBorderTop(1, cra, sheet, workbook); // 上边框
  863 + }
  864 +}
src/main/resources/static/pages/base/line/list.html
@@ -98,7 +98,7 @@ @@ -98,7 +98,7 @@
98 </td> 98 </td>
99 <td> 99 <td>
100 <!-- 这里没使用字典表,暂时写在页面上 --> 100 <!-- 这里没使用字典表,暂时写在页面上 -->
101 - <select name="nature_like" class="form-control" id="natureSelect"> 101 + <select name="nature_eq" class="form-control" id="natureSelect">
102 <option value="">请选择...</option> 102 <option value="">请选择...</option>
103 <option value="lj">路救</option> 103 <option value="lj">路救</option>
104 <option value="bc">备车</option> 104 <option value="bc">备车</option>
src/main/resources/static/pages/forms/mould/passenger-statistic-station.xls 0 → 100644
No preview for this file type
src/main/resources/static/pages/forms/mould/passenger-statistic-vehicle.xls 0 → 100644
No preview for this file type
src/main/resources/static/pages/report/passenger-statistic.html 0 → 100644
  1 +<style type="text/css">
  2 + .table-bordered {
  3 + border: 1px solid; }
  4 + .table-bordered > thead > tr > th,
  5 + .table-bordered > thead > tr > td,
  6 + .table-bordered > tbody > tr > th,
  7 + .table-bordered > tbody > tr > td,
  8 + .table-bordered > tfoot > tr > th,
  9 + .table-bordered > tfoot > tr > td {
  10 + border: 1px solid; }
  11 + .table-bordered > thead > tr > th,
  12 + .table-bordered > thead > tr > td {
  13 + border-bottom-width: 2px; }
  14 +
  15 + .table > tbody + tbody {
  16 + border-top: 1px solid; }
  17 +</style>
  18 +
  19 +<div class="page-head">
  20 + <div class="page-title">
  21 + <h1>刷卡客流统计</h1>
  22 + </div>
  23 +</div>
  24 +
  25 +<div class="row">
  26 + <div class="col-md-12">
  27 + <div class="portlet light porttlet-fit bordered">
  28 + <div class="portlet-title">
  29 + <form class="form-inline" action="">
  30 + <div>
  31 + <div style="display: inline-block; margin-left: 10px;">
  32 + 统计:
  33 + <input type="radio" checked name="statistic" value="0"/> 按站点
  34 + <input type="radio" name="statistic" value="1"/> 按车辆
  35 + </div>
  36 + <div style="display: inline-block; margin-left: 10px;" id="gsdmDiv">
  37 + <span class="item-label" style="width: 80px;">公司: </span>
  38 + <select class="form-control" name="company" id="gsdm" style="width: 180px;"></select>
  39 + </div>
  40 + <div style="display: inline-block; margin-left: 10px;" id="fgsdmDiv">
  41 + <span class="item-label" style="width: 80px;">分公司: </span>
  42 + <select class="form-control" name="subCompany" id="fgsdm" style="width: 180px;"></select>
  43 + </div>
  44 + <div style="display: inline-block;margin-left: 10px;">
  45 + <span class="item-label" style="width: 80px;">线路: </span>
  46 + <select class="form-control station-change" name="line" id="line" style="width: 180px;"></select>
  47 + </div>
  48 + <div style="display: inline-block;margin-left: 10px;">
  49 + <span class="item-label" style="width: 80px;">时间: </span>
  50 + <input class="form-control station-change" type="text" id="rqBegin" name="rqBegin" style="width: 180px;"/>&nbsp;至&nbsp;
  51 + <input class="form-control station-change" type="text" id="rqEnd" name="rqEnd" style="width: 180px;"/>
  52 + </div>
  53 + </div>
  54 + <br/>
  55 + <div>
  56 + <div class="group" style="display: inline-block;">
  57 + <div style="display: inline-block;margin-left: 10px;">
  58 + <span class="item-label" style="width: 80px;">上下行: </span>
  59 + <select class="form-control station-change" name="direction" style="width: 180px;">
  60 + <option value="0">上行</option>
  61 + <option value="1">下行</option>
  62 + </select>
  63 + </div>
  64 + <div style="display: inline-block;margin-left: 10px;">
  65 + <span class="item-label" style="width: 80px;">站点: </span>
  66 + <select class="form-control" id="stationCode" name="stationCode" style="width: 180px;"></select>
  67 + </div>
  68 + </div>
  69 + <div class="group" style="display: none;">
  70 + <div style="display: inline-block;margin-left: 10px;">
  71 + <span class="item-label" style="width: 80px;">车辆: </span>
  72 + <select class="form-control" id="insideCode" style="width: 180px;"></select>
  73 + </div>
  74 + </div>
  75 + <div class="form-group" style="display: inline-block;margin-left: 15px;">
  76 + <input class="btn btn-default" type="button" id="query" value="查询"/>
  77 + <input class="btn btn-default" type="button" id="export" value="导出"/>
  78 + </div>
  79 + </div>
  80 + </form>
  81 + </div>
  82 + <div class="portlet-body">
  83 + <div class="row">
  84 + <div class="table-container" style="margin-top: 10px;overflow:auto;min-width: 906px;height: 100%">
  85 + <table class="table table-bordered table-checkable" id="forms0">
  86 + <thead>
  87 + <tr>
  88 + <td width="8%">序号</td>
  89 + <td width="23%">线路</td>
  90 + <td width="23%">上下行</td>
  91 + <td width="23%">站点名称</td>
  92 + <td width="23%">刷卡次数</td>
  93 + </tr>
  94 + </thead>
  95 + <tbody class="passenger_statistic_tbody">
  96 +
  97 + </tbody>
  98 + </table>
  99 + </div>
  100 + </div>
  101 + </div>
  102 + <div class="portlet-body" style="display: none;">
  103 + <div class="row">
  104 + <div class="table-container" style="margin-top: 10px;overflow:auto;min-width: 906px;height: 100%">
  105 + <table class="table table-bordered table-checkable" id="forms1">
  106 + <thead>
  107 + <tr>
  108 + <td width="10%"></td>
  109 + <td width="30%">线路</td>
  110 + <td width="30%">车辆编码</td>
  111 + <td width="30%">刷卡次数</td>
  112 + </tr>
  113 + </thead>
  114 + <tbody class="passenger_statistic_tbody">
  115 +
  116 + </tbody>
  117 + </table>
  118 + </div>
  119 + </div>
  120 + </div>
  121 + </div>
  122 + </div>
  123 +</div>
  124 +
  125 +<script>
  126 + $(function(){
  127 + $('input[name="statistic"]').on('change', function() {
  128 + $('.group').css('display', 'none').eq($(this).val()).css('display', 'inline-block');
  129 + $('.portlet-body').css('display', 'none').eq($(this).val()).css('display', '');
  130 + })
  131 + // 关闭左侧栏
  132 + if (!$('body').hasClass('page-sidebar-closed'))
  133 + $('.menu-toggler.sidebar-toggler').click();
  134 +
  135 + $("#rqBegin").datetimepicker({
  136 + format : 'YYYYMMDDHH',
  137 + locale : 'zh-cn'
  138 + });
  139 +
  140 + $("#rqEnd").datetimepicker({
  141 + format : 'YYYYMMDDHH',
  142 + locale : 'zh-cn'
  143 + });
  144 +
  145 + var dt = moment().format('YYYYMMDD');
  146 + $('#rqBegin').val(dt + '00');
  147 + $('#rqEnd').val(dt + '23');
  148 +
  149 + var obj = [];
  150 + var xlList;
  151 + $.get('/report/lineList',function(result){
  152 + xlList=result;
  153 +
  154 + $.get('/user/companyData', function(result){
  155 + obj = result;
  156 + var options = '';
  157 + for(var i = 0; i < obj.length; i++){
  158 + options += '<option value="'+obj[i].companyCode+'">'+obj[i].companyName+'</option>';
  159 + }
  160 +
  161 + if(obj.length ==0){
  162 + $("#gsdmDiv").css('display','none');
  163 + }else if(obj.length ==1){
  164 + $("#gsdmDiv").css('display','none');
  165 + if(obj[0].children.length == 1 || obj[0].children.length ==0)
  166 + $('#fgsdmDiv').css('display','none');
  167 + }
  168 + $('#gsdm').html(options);
  169 +
  170 + updateCompany();
  171 + });
  172 + });
  173 +
  174 + $("#gsdm").on("change",updateCompany);
  175 + function updateCompany(){
  176 + var company = $('#gsdm').val();
  177 + var options = '';
  178 + for(var i = 0; i < obj.length; i++){
  179 + if(obj[i].companyCode == company){
  180 + var children = obj[i].children;
  181 + for(var j = 0; j < children.length; j++){
  182 + options += '<option value="'+children[j].code+'">'+children[j].name+'</option>';
  183 + }
  184 + }
  185 + }
  186 + $('#fgsdm').html(options);
  187 + initCl();
  188 + }
  189 +
  190 +
  191 + var tempData = {};
  192 + $.get('/report/lineList',function(xlList){
  193 + var data = [];
  194 + $.get('/user/companyData', function(result){
  195 + for(var i = 0; i < result.length; i++){
  196 + var companyCode = result[i].companyCode;
  197 + var children = result[i].children;
  198 + for(var j = 0; j < children.length; j++){
  199 + var code = children[j].code;
  200 + for(var k=0;k < xlList.length;k++ ){
  201 + if(xlList[k]["fgsbm"]==code && xlList[k]["gsbm"]==companyCode){
  202 + data.push({id: xlList[k]["xlbm"], text: xlList[k]["xlname"]});
  203 + tempData[xlList[k]["xlbm"]] = companyCode+":"+code;
  204 + }
  205 + }
  206 + }
  207 + }
  208 + initPinYinSelect2('#line',data,'');
  209 + });
  210 + });
  211 +
  212 + $("#line").on("change", function(){
  213 + if($("#line").val() == " "){
  214 + $("#gsdm").attr("disabled", false);
  215 + $("#fgsdm").attr("disabled", false);
  216 + } else {
  217 + var temp = (tempData[$("#line").val()] ? tempData[$("#line").val()] : " : ").split(":");
  218 + $("#gsdm").val(temp[0]);
  219 + updateCompany();
  220 + $("#fgsdm").val(temp[1]);
  221 + $("#gsdm").attr("disabled", true);
  222 + $("#fgsdm").attr("disabled", true);
  223 + }
  224 + });
  225 +
  226 +
  227 + $("#query").on("click",function(){
  228 + var param = $('.form-inline').serializeJSON();
  229 + if (param.stationCode === '-1') {
  230 + delete param.stationCode;
  231 + }
  232 + if (!param.line) {
  233 + layer.msg("请选择线路");
  234 + } else if (!param.rqBegin || !param.rqEnd){
  235 + layer.msg("请选择起止时间");
  236 + } else {
  237 + $get('/api/passenger-statistic' + (param.statistic === '0' ? '/groupByStation' : '/groupByVehicle'), param, function(result){
  238 + var html = template('passenger_statistic_template' + param.statistic,{list:result});
  239 + $('#forms' + param.statistic + ' .passenger_statistic_tbody').html(html);
  240 + });
  241 + }
  242 + });
  243 +
  244 + $("#line").on("change",initCl);
  245 + function initCl(){
  246 + $('#insideCode').select2({
  247 + placeholder: '搜索车辆...',
  248 + allowClear: true,
  249 + ajax: {
  250 + url: '/report/carListByHistory',
  251 + dataType: 'json',
  252 + delay: 150,
  253 + data: function(params){
  254 + return{nbbm: params.term,
  255 + gsbm:$('#gsdm').val(),
  256 + fgsbm:$('#fgsdm').val(),
  257 + xlbm:$('#line').val()};
  258 + },
  259 + processResults: function (data) {
  260 + return {
  261 + results: data
  262 + };
  263 + },
  264 + cache: true
  265 + },
  266 + templateResult: function(repo){
  267 + if (repo.loading) return repo.text;
  268 + var h = '<span>'+repo.text+'</span>';
  269 + h += (repo.lineName?'&nbsp;<span class="select2-desc">'+repo.lineName+'</span>':'');
  270 + return h;
  271 + },
  272 + escapeMarkup: function (markup) { return markup; },
  273 + minimumInputLength: 1,
  274 + templateSelection: function(repo){
  275 + return repo.text;
  276 + },
  277 + language: {
  278 + noResults: function(){
  279 + return '<span style="color:red;font-size: 12px;">没有搜索到车辆!</span>';
  280 + },
  281 + inputTooShort : function(e) {
  282 + return '<span style="color:gray;font-size: 12px;"><i class="fa fa-search"></i> 输入自编号搜索车辆</span>';
  283 + },
  284 + searching : function() {
  285 + return '<span style="color:gray;font-size: 12px;"> 正在搜索车辆...</span>';
  286 + }
  287 + }
  288 + });
  289 + };
  290 +
  291 +
  292 + $(".station-change").on("change",initZd);
  293 + $("#rqBegin,#rqEnd").on("blur",initZd);
  294 + var status=false;
  295 +
  296 + function initZd(){
  297 + var param = $('.form-inline').serializeJSON();
  298 + if (!param.line || !param.direction || !param.rqBegin || !param.rqEnd) {
  299 + } else {
  300 + $.get('/api/lsstationroute/findByLineDirectionDate', param, function(result){
  301 + var zdList=result.data;
  302 + if(status){
  303 + $("#stationCode").select2("destroy").html('');
  304 + }
  305 + var datas=[];
  306 + datas.push({id:"-1",text:"请选择..."});
  307 + for(var i=0;i<zdList.length;i++){
  308 + datas.push({id: zdList[i]["stationCode"], text: zdList[i]["stationName"] + '(v' + zdList[i]['versions'] + ')'});
  309 + }
  310 + initPinYinSelect2('#stationCode',datas);
  311 + status=true;
  312 + })
  313 + }
  314 + }
  315 +
  316 + $("#export").on("click",function(){
  317 + var param = $('.form-inline').serializeJSON();
  318 + if (param.stationCode === '-1') {
  319 + delete param.stationCode;
  320 + }
  321 + if (!param.line) {
  322 + layer.msg("请选择线路");
  323 + } else if (!param.rqBegin || !param.rqEnd){
  324 + layer.msg("请选择起止时间");
  325 + } else {
  326 + $get('/api/passenger-statistic' + (param.statistic === '0' ? '/groupByStation/export' : '/groupByVehicle/export'), param, function(result){
  327 + window.open('/downloadFile/download?fileName=' + param.rqBegin + '至' + param.rqEnd + '-刷卡客流统计(' + (param.statistic === '0' ? '站点' : '车辆') + ')');
  328 + });
  329 + }
  330 + });
  331 + });
  332 +
  333 +</script>
  334 +<script type="text/html" id="passenger_statistic_template0">
  335 + {{each list as obj i}}
  336 + <tr>
  337 + <td>{{i+1}}</td>
  338 + <td>{{obj.lineName}}</td>
  339 + <td>
  340 + {{if obj.direction==0}}
  341 + 上行
  342 + {{else}}
  343 + 下行
  344 + {{/if}}
  345 + </td>
  346 + <td>{{obj.stationName}}</td>
  347 + <td>{{obj.number}}</td>
  348 + </tr>
  349 + {{/each}}
  350 + {{if list.length == 0}}
  351 + <tr>
  352 + <td colspan="5"><h6 class="muted">没有找到相关数据</h6></td>
  353 + </tr>
  354 + {{/if}}
  355 +</script>
  356 +<script type="text/html" id="passenger_statistic_template1">
  357 + {{each list as obj i}}
  358 + <tr>
  359 + <td>{{i+1}}</td>
  360 + <td>{{obj.lineName}}</td>
  361 + <td>{{obj.insideCode}}</td>
  362 + <td>{{obj.number}}</td>
  363 + </tr>
  364 + {{/each}}
  365 + {{if list.length == 0}}
  366 + <tr>
  367 + <td colspan="4"><h6 class="muted">没有找到相关数据</h6></td>
  368 + </tr>
  369 + {{/if}}
  370 +</script>