Commit a823da6d7cbf576850930a6612a913a188d0755c
1 parent
096cd356
1.一站一码扫码统计
2.minio访问加入签名验证
Showing
20 changed files
with
1661 additions
and
1125 deletions
src/main/java/com/bsth/DataSourceConfig.java
0 → 100644
| 1 | +package com.bsth; | |
| 2 | + | |
| 3 | +import org.springframework.beans.factory.annotation.Qualifier; | |
| 4 | +import org.springframework.boot.context.properties.ConfigurationProperties; | |
| 5 | +import org.springframework.boot.jdbc.DataSourceBuilder; | |
| 6 | +import org.springframework.context.annotation.Bean; | |
| 7 | +import org.springframework.context.annotation.Configuration; | |
| 8 | +import org.springframework.context.annotation.Primary; | |
| 9 | +import org.springframework.jdbc.core.JdbcTemplate; | |
| 10 | + | |
| 11 | +import javax.sql.DataSource; | |
| 12 | + | |
| 13 | +/** | |
| 14 | + * @author Hill | |
| 15 | + */ | |
| 16 | +@Configuration | |
| 17 | +public class DataSourceConfig { | |
| 18 | + | |
| 19 | + @Bean(name = "dataSource") | |
| 20 | + @Qualifier("dataSource") | |
| 21 | + @Primary | |
| 22 | + @ConfigurationProperties(prefix = "spring.datasource.control") | |
| 23 | + public DataSource dataSource(){ | |
| 24 | + return DataSourceBuilder.create().build(); | |
| 25 | + } | |
| 26 | + | |
| 27 | + @Bean(name = "infoPublishDataSource") | |
| 28 | + @Qualifier("infoPublishDataSource") | |
| 29 | + @ConfigurationProperties(prefix = "spring.datasource.info-publish") | |
| 30 | + public DataSource infoPublishDataSource(){ | |
| 31 | + return DataSourceBuilder.create().build(); | |
| 32 | + } | |
| 33 | + | |
| 34 | + @Bean(name = "jdbcTemplate") | |
| 35 | + @Primary | |
| 36 | + public JdbcTemplate jdbcTemplate(@Qualifier("dataSource")DataSource dataSource){ | |
| 37 | + return new JdbcTemplate(dataSource); | |
| 38 | + } | |
| 39 | + | |
| 40 | + @Bean(name = "infoPublishJdbcTemplate") | |
| 41 | + public JdbcTemplate infoPublishJdbcTemplate(@Qualifier("infoPublishDataSource")DataSource dataSource){ | |
| 42 | + return new JdbcTemplate(dataSource); | |
| 43 | + } | |
| 44 | +} | ... | ... |
src/main/java/com/bsth/controller/alaram/MinioController.java
0 → 100644
| 1 | +package com.bsth.controller.alaram; | |
| 2 | + | |
| 3 | +import com.bsth.common.ResponseCode; | |
| 4 | +import io.minio.MinioClient; | |
| 5 | +import io.minio.GetPresignedObjectUrlArgs; | |
| 6 | +import io.minio.http.Method; | |
| 7 | +import org.slf4j.Logger; | |
| 8 | +import org.slf4j.LoggerFactory; | |
| 9 | +import org.springframework.beans.factory.annotation.Autowired; | |
| 10 | +import org.springframework.web.bind.annotation.RequestMapping; | |
| 11 | +import org.springframework.web.bind.annotation.RequestParam; | |
| 12 | +import org.springframework.web.bind.annotation.RestController; | |
| 13 | + | |
| 14 | +import java.util.ArrayList; | |
| 15 | +import java.util.HashMap; | |
| 16 | +import java.util.List; | |
| 17 | +import java.util.Map; | |
| 18 | +import java.util.concurrent.TimeUnit; | |
| 19 | + | |
| 20 | +@RestController | |
| 21 | +@RequestMapping("api/minio") | |
| 22 | +public class MinioController { | |
| 23 | + | |
| 24 | + private final static Logger log = LoggerFactory.getLogger(MinioController.class); | |
| 25 | + | |
| 26 | + @Autowired | |
| 27 | + private MinioClient minioClient; | |
| 28 | + | |
| 29 | + @RequestMapping(value = "/preSignedUrl") | |
| 30 | + public Map<String, Object> preSignedUrl(@RequestParam(defaultValue = "dsm") String bucket, @RequestParam(name = "objects[]") String[] objects) { | |
| 31 | + Map<String, Object> result = new HashMap<>(); | |
| 32 | + try { | |
| 33 | + List<String> urls = new ArrayList<>(); | |
| 34 | + for (String object : objects) { | |
| 35 | + String url = minioClient.getPresignedObjectUrl( | |
| 36 | + GetPresignedObjectUrlArgs.builder() | |
| 37 | + .bucket(bucket) | |
| 38 | + .object(object) | |
| 39 | + .method(Method.GET) | |
| 40 | + .expiry(5, TimeUnit.MINUTES) | |
| 41 | + .build() | |
| 42 | + ); | |
| 43 | + urls.add(url); | |
| 44 | + } | |
| 45 | + | |
| 46 | + result.put("urls", urls); | |
| 47 | + result.put("status", ResponseCode.SUCCESS); | |
| 48 | + } catch (Exception e) { | |
| 49 | + log.error("minio预签名异常", e); | |
| 50 | + result.put("msg", "minio预签名异常"); | |
| 51 | + result.put("status", ResponseCode.ERROR); | |
| 52 | + } | |
| 53 | + | |
| 54 | + return result; | |
| 55 | + } | |
| 56 | +} | ... | ... |
src/main/java/com/bsth/controller/publish/InfoPublishController.java
0 → 100644
| 1 | +package com.bsth.controller.publish; | |
| 2 | + | |
| 3 | +import com.bsth.common.ResponseCode; | |
| 4 | +import com.bsth.entity.publish.ScanStatictic; | |
| 5 | +import com.bsth.message.entity.AlarmADASVo; | |
| 6 | +import com.bsth.service.alarm.AlarmAdasService; | |
| 7 | +import com.bsth.service.publish.InfoPublishService; | |
| 8 | +import com.bsth.util.ReportUtils; | |
| 9 | +import org.slf4j.Logger; | |
| 10 | +import org.slf4j.LoggerFactory; | |
| 11 | +import org.springframework.beans.factory.annotation.Autowired; | |
| 12 | +import org.springframework.web.bind.annotation.RequestMapping; | |
| 13 | +import org.springframework.web.bind.annotation.RequestParam; | |
| 14 | +import org.springframework.web.bind.annotation.RestController; | |
| 15 | + | |
| 16 | +import java.util.*; | |
| 17 | + | |
| 18 | +@RestController | |
| 19 | +@RequestMapping("api/info_publish") | |
| 20 | +public class InfoPublishController { | |
| 21 | + | |
| 22 | + private final static Logger log = LoggerFactory.getLogger(InfoPublishController.class); | |
| 23 | + | |
| 24 | + @Autowired | |
| 25 | + private InfoPublishService infoPublishService; | |
| 26 | + | |
| 27 | + @RequestMapping(value = "/scan_statistic/list") | |
| 28 | + public Map<String, Object> list(@RequestParam Map<String, String> param, | |
| 29 | + @RequestParam(defaultValue = "0") int page, | |
| 30 | + @RequestParam(defaultValue = "15") int size, | |
| 31 | + @RequestParam(defaultValue = "timestamp") String order, | |
| 32 | + @RequestParam(defaultValue = "0") int direction) { | |
| 33 | + Map<String, Object> result = new HashMap<>(); | |
| 34 | + try { | |
| 35 | + Map<String, Object> results = infoPublishService.list(param, page, size, order, direction); | |
| 36 | + | |
| 37 | + result.putAll(results); | |
| 38 | + result.put("page", page); | |
| 39 | + result.put("status", ResponseCode.SUCCESS); | |
| 40 | + } catch (Exception e) { | |
| 41 | + result.put("status", ResponseCode.ERROR); | |
| 42 | + log.error(e.getMessage(), e); | |
| 43 | + } | |
| 44 | + | |
| 45 | + return result; | |
| 46 | + } | |
| 47 | + | |
| 48 | + @RequestMapping(value = "/scan_statistic/list_export") | |
| 49 | + public Map<String, Object> list(@RequestParam Map<String, String> param) { | |
| 50 | + Map<String, Object> result = new HashMap<>(); | |
| 51 | + try { | |
| 52 | + Map<String, Object> results = infoPublishService.export(param); | |
| 53 | + ReportUtils util = new ReportUtils(); | |
| 54 | + String path = this.getClass().getResource("/").getPath() + "static/pages/forms", rq = param.get("rq"); | |
| 55 | + util.excelReplace((List<Iterator<?>>) results.get("list"), new Object[]{}, String.format("%s/mould/scanStatistic.xls", path), String.format("%s/export/一站一码扫码情况%s.xls", path, rq)); | |
| 56 | + | |
| 57 | + result.put("status", ResponseCode.SUCCESS); | |
| 58 | + } catch (Exception e) { | |
| 59 | + result.put("status", ResponseCode.ERROR); | |
| 60 | + log.error(e.getMessage(), e); | |
| 61 | + } | |
| 62 | + | |
| 63 | + return result; | |
| 64 | + } | |
| 65 | +} | ... | ... |
src/main/java/com/bsth/controller/realcontrol/DataManagerController.java
| 1 | -package com.bsth.controller.realcontrol; | |
| 2 | - | |
| 3 | -import com.bsth.service.realcontrol.DataManagerService; | |
| 4 | -import org.apache.commons.lang3.StringEscapeUtils; | |
| 5 | -import org.springframework.beans.factory.annotation.Autowired; | |
| 6 | -import org.springframework.web.bind.annotation.RequestMapping; | |
| 7 | -import org.springframework.web.bind.annotation.RequestParam; | |
| 8 | -import org.springframework.web.bind.annotation.RestController; | |
| 9 | - | |
| 10 | -import java.util.Map; | |
| 11 | - | |
| 12 | -/** | |
| 13 | - * 数据管理,包括从老系统的数据迁移。新系统的数据校验等 | |
| 14 | - * Created by panzhao on 2017/4/17. | |
| 15 | - */ | |
| 16 | -@RestController | |
| 17 | -@RequestMapping("dataManager") | |
| 18 | -public class DataManagerController { | |
| 19 | - | |
| 20 | - @Autowired | |
| 21 | - DataManagerService dataManagerService; | |
| 22 | - | |
| 23 | - @RequestMapping("cars/old_now") | |
| 24 | - public Map<String, Object> carInfos(@RequestParam Integer lineId){ | |
| 25 | - return dataManagerService.carInfos(lineId); | |
| 26 | - } | |
| 27 | - | |
| 28 | - @RequestMapping("car/updateDevices") | |
| 29 | - public Map<String, Object> updateDevices(@RequestParam String jsonStr){ | |
| 30 | - jsonStr = StringEscapeUtils.unescapeHtml4(jsonStr); | |
| 31 | - return dataManagerService.updateDevices(jsonStr); | |
| 32 | - } | |
| 33 | -} | |
| 1 | +package com.bsth.controller.realcontrol; | |
| 2 | + | |
| 3 | +import com.bsth.service.realcontrol.DataManagerService; | |
| 4 | +import org.apache.commons.lang3.StringEscapeUtils; | |
| 5 | +import org.springframework.beans.factory.annotation.Autowired; | |
| 6 | +import org.springframework.web.bind.annotation.RequestMapping; | |
| 7 | +import org.springframework.web.bind.annotation.RequestParam; | |
| 8 | +import org.springframework.web.bind.annotation.RestController; | |
| 9 | + | |
| 10 | +import java.util.Map; | |
| 11 | + | |
| 12 | +/** | |
| 13 | + * 数据管理,包括从老系统的数据迁移。新系统的数据校验等 | |
| 14 | + * Created by panzhao on 2017/4/17. | |
| 15 | + */ | |
| 16 | +//@RestController | |
| 17 | +@RequestMapping("dataManager") | |
| 18 | +public class DataManagerController { | |
| 19 | + | |
| 20 | + @Autowired | |
| 21 | + DataManagerService dataManagerService; | |
| 22 | + | |
| 23 | + @RequestMapping("cars/old_now") | |
| 24 | + public Map<String, Object> carInfos(@RequestParam Integer lineId){ | |
| 25 | + return dataManagerService.carInfos(lineId); | |
| 26 | + } | |
| 27 | + | |
| 28 | + @RequestMapping("car/updateDevices") | |
| 29 | + public Map<String, Object> updateDevices(@RequestParam String jsonStr){ | |
| 30 | + jsonStr = StringEscapeUtils.unescapeHtml4(jsonStr); | |
| 31 | + return dataManagerService.updateDevices(jsonStr); | |
| 32 | + } | |
| 33 | +} | ... | ... |
src/main/java/com/bsth/entity/publish/ScanStatictic.java
0 → 100644
| 1 | +package com.bsth.entity.publish; | |
| 2 | + | |
| 3 | +public class ScanStatictic { | |
| 4 | + | |
| 5 | + private String rq; | |
| 6 | + | |
| 7 | + private String physicalCode; | |
| 8 | + | |
| 9 | + private String stationName; | |
| 10 | + | |
| 11 | + private int count; | |
| 12 | + | |
| 13 | + public String getRq() { | |
| 14 | + return rq; | |
| 15 | + } | |
| 16 | + | |
| 17 | + public void setRq(String rq) { | |
| 18 | + this.rq = rq; | |
| 19 | + } | |
| 20 | + | |
| 21 | + public String getPhysicalCode() { | |
| 22 | + return physicalCode; | |
| 23 | + } | |
| 24 | + | |
| 25 | + public void setPhysicalCode(String physicalCode) { | |
| 26 | + this.physicalCode = physicalCode; | |
| 27 | + } | |
| 28 | + | |
| 29 | + public String getStationName() { | |
| 30 | + return stationName; | |
| 31 | + } | |
| 32 | + | |
| 33 | + public void setStationName(String stationName) { | |
| 34 | + this.stationName = stationName; | |
| 35 | + } | |
| 36 | + | |
| 37 | + public int getCount() { | |
| 38 | + return count; | |
| 39 | + } | |
| 40 | + | |
| 41 | + public void setCount(int count) { | |
| 42 | + this.count = count; | |
| 43 | + } | |
| 44 | +} | ... | ... |
src/main/java/com/bsth/service/publish/InfoPublishService.java
0 → 100644
src/main/java/com/bsth/service/publish/impl/InfoPublishServiceImpl.java
0 → 100644
| 1 | +package com.bsth.service.publish.impl; | |
| 2 | + | |
| 3 | +import com.bsth.entity.publish.ScanStatictic; | |
| 4 | +import com.bsth.service.publish.InfoPublishService; | |
| 5 | +import org.springframework.beans.factory.annotation.Autowired; | |
| 6 | +import org.springframework.beans.factory.annotation.Qualifier; | |
| 7 | +import org.springframework.jdbc.core.BeanPropertyRowMapper; | |
| 8 | +import org.springframework.jdbc.core.JdbcTemplate; | |
| 9 | +import org.springframework.stereotype.Service; | |
| 10 | +import org.springframework.util.StringUtils; | |
| 11 | + | |
| 12 | +import java.util.*; | |
| 13 | + | |
| 14 | +@Service | |
| 15 | +public class InfoPublishServiceImpl implements InfoPublishService { | |
| 16 | + | |
| 17 | + @Autowired | |
| 18 | + @Qualifier("infoPublishJdbcTemplate") | |
| 19 | + private JdbcTemplate infoPublishJdbcTemplate; | |
| 20 | + | |
| 21 | + @Override | |
| 22 | + public Map<String, Object> list(Map<String, String> map, int page, int size, String order, int direction) { | |
| 23 | + Map<String, Object> result = new HashMap<>(); | |
| 24 | + StringBuilder query = new StringBuilder("SELECT ? rq, physicalCode physical_code, stationName station_name,count(1) `count` FROM bsth_h_scan where 1 = 1"); | |
| 25 | + List<Object> params = new ArrayList<>(); | |
| 26 | + params.add(map.get("rq")); | |
| 27 | + for (Map.Entry<String, String> entry : map.entrySet()) { | |
| 28 | + String key = entry.getKey(), value = entry.getValue(); | |
| 29 | + if (StringUtils.isEmpty(key) || ",page,size,order,direction,".indexOf(String.format(",%s,", key)) > -1) { | |
| 30 | + continue; | |
| 31 | + } | |
| 32 | + if ("rq".equals(key)) { | |
| 33 | + query.append(" and rq between ? and ?"); | |
| 34 | + String[] rqs = value.split("-"); | |
| 35 | + if (rqs.length != 2) { | |
| 36 | + throw new RuntimeException("异常的起止日期"); | |
| 37 | + } | |
| 38 | + for (String rq : rqs) { | |
| 39 | + params.add(rq); | |
| 40 | + } | |
| 41 | + } else { | |
| 42 | + query.append(" and ").append(key).append(" like concat(?, '%')"); | |
| 43 | + params.add(value); | |
| 44 | + } | |
| 45 | + } | |
| 46 | + query.append(" GROUP BY physicalCode, stationName"); | |
| 47 | + StringBuilder count = new StringBuilder("SELECT COUNT(*) FROM (").append(query).append(") t"); | |
| 48 | + Integer total = infoPublishJdbcTemplate.queryForObject(count.toString(), params.toArray(), Integer.class); | |
| 49 | + query.append(" ORDER BY physicalCode LIMIT ").append(page * size).append(",").append(size); | |
| 50 | + List<ScanStatictic> data = infoPublishJdbcTemplate.query(query.toString(), params.toArray(), BeanPropertyRowMapper.newInstance(ScanStatictic.class)); | |
| 51 | + result.put("list", data); | |
| 52 | + result.put("totalPages", total % size == 0 ? total / size - 1 : total / size); | |
| 53 | + | |
| 54 | + return result; | |
| 55 | + } | |
| 56 | + | |
| 57 | + @Override | |
| 58 | + public Map<String, Object> export(Map<String, String> map) { | |
| 59 | + Map<String, Object> result = new HashMap<>(); | |
| 60 | + StringBuilder query = new StringBuilder("SELECT ? rq, physicalCode, stationName,count(1) `count` FROM bsth_h_scan where 1 = 1"); | |
| 61 | + List<Object> params = new ArrayList<>(); | |
| 62 | + params.add(map.get("rq")); | |
| 63 | + for (Map.Entry<String, String> entry : map.entrySet()) { | |
| 64 | + String key = entry.getKey(), value = entry.getValue(); | |
| 65 | + if ("rq".equals(key)) { | |
| 66 | + query.append(" and rq between ? and ?"); | |
| 67 | + String[] rqs = value.split("-"); | |
| 68 | + if (rqs.length != 2) { | |
| 69 | + throw new RuntimeException("异常的起止日期"); | |
| 70 | + } | |
| 71 | + for (String rq : rqs) { | |
| 72 | + params.add(rq); | |
| 73 | + } | |
| 74 | + } else { | |
| 75 | + query.append(" and ").append(key).append(" like concat(?, '%')"); | |
| 76 | + params.add(value); | |
| 77 | + } | |
| 78 | + } | |
| 79 | + query.append(" GROUP BY physicalCode, stationName"); | |
| 80 | + query.append(" ORDER BY physicalCode"); | |
| 81 | + List<Iterator<?>> iterators = new ArrayList<>(); | |
| 82 | + iterators.add(infoPublishJdbcTemplate.queryForList(query.toString(), params.toArray()).iterator()); | |
| 83 | + result.put("list", iterators); | |
| 84 | + | |
| 85 | + return result; | |
| 86 | + } | |
| 87 | +} | ... | ... |
src/main/java/com/bsth/service/realcontrol/impl/DataManagerServiceImpl.java
| 1 | -package com.bsth.service.realcontrol.impl; | |
| 2 | - | |
| 3 | -import com.alibaba.fastjson.JSONArray; | |
| 4 | -import com.alibaba.fastjson.JSONObject; | |
| 5 | -import com.bsth.common.ResponseCode; | |
| 6 | -import com.bsth.data.BasicData; | |
| 7 | -import com.bsth.entity.Cars; | |
| 8 | -import com.bsth.repository.CarsRepository; | |
| 9 | -import com.bsth.service.realcontrol.DataManagerService; | |
| 10 | -import com.bsth.util.db.DBUtils_oldSystem; | |
| 11 | -import org.slf4j.Logger; | |
| 12 | -import org.slf4j.LoggerFactory; | |
| 13 | -import org.springframework.beans.factory.annotation.Autowired; | |
| 14 | -import org.springframework.jdbc.core.JdbcTemplate; | |
| 15 | -import org.springframework.stereotype.Service; | |
| 16 | - | |
| 17 | -import java.util.ArrayList; | |
| 18 | -import java.util.HashMap; | |
| 19 | -import java.util.List; | |
| 20 | -import java.util.Map; | |
| 21 | - | |
| 22 | -/** | |
| 23 | - * Created by panzhao on 2017/4/18. | |
| 24 | - */ | |
| 25 | -@Service | |
| 26 | -public class DataManagerServiceImpl implements DataManagerService{ | |
| 27 | - | |
| 28 | - @Autowired | |
| 29 | - CarsRepository carsRepository; | |
| 30 | - | |
| 31 | - @Autowired | |
| 32 | - JdbcTemplate controlJdbcTemp; | |
| 33 | - | |
| 34 | - @Autowired | |
| 35 | - BasicData.BasicDataLoader dataLoader; | |
| 36 | - | |
| 37 | - Logger logger = LoggerFactory.getLogger(this.getClass()); | |
| 38 | - | |
| 39 | - @Override | |
| 40 | - public Map<String, Object> carInfos(Integer lineId) { | |
| 41 | - | |
| 42 | - Map<String, Object> rs = new HashMap<>(); | |
| 43 | - try { | |
| 44 | - List<Map<String, String>> nowData = new ArrayList<>(); | |
| 45 | - List<Map<String, String>> oldData = new ArrayList<>(); | |
| 46 | - Map<String, String> map; | |
| 47 | - | |
| 48 | - //查询新系统车辆信息 | |
| 49 | - List<Cars> list = carsRepository.findCarsByLineId(lineId); | |
| 50 | - for(Cars c : list){ | |
| 51 | - map = new HashMap<>(); | |
| 52 | - map.put("nbbm", c.getInsideCode()); | |
| 53 | - map.put("device", c.getEquipmentCode()); | |
| 54 | - nowData.add(map); | |
| 55 | - } | |
| 56 | - | |
| 57 | - //获取老系统数据 | |
| 58 | - JdbcTemplate jdbcTemplate = new JdbcTemplate(DBUtils_oldSystem.getDataSource()); | |
| 59 | - List<Map<String, Object>> oyList = jdbcTemplate.queryForList("select NBBM,SBBH from JJWGPS_T_CLXXB t where xlbm=?", BasicData.lineId2CodeMap.get(lineId)); | |
| 60 | - for(Map<String, Object> tempMap : oyList){ | |
| 61 | - map = new HashMap<>(); | |
| 62 | - map.put("nbbm", tempMap.get("NBBM").toString()); | |
| 63 | - map.put("device", tempMap.get("SBBH").toString()); | |
| 64 | - oldData.add(map); | |
| 65 | - } | |
| 66 | - | |
| 67 | - rs.put("status", ResponseCode.SUCCESS); | |
| 68 | - rs.put("nows", nowData); | |
| 69 | - rs.put("olds", oldData); | |
| 70 | - }catch (Exception e){ | |
| 71 | - logger.error("", e); | |
| 72 | - rs.put("status", ResponseCode.ERROR); | |
| 73 | - rs.put("msg", e.getMessage()); | |
| 74 | - } | |
| 75 | - | |
| 76 | - return rs; | |
| 77 | - } | |
| 78 | - | |
| 79 | - /** | |
| 80 | - * 更新设备号 | |
| 81 | - * @param jsonStr | |
| 82 | - * @return | |
| 83 | - */ | |
| 84 | - @Override | |
| 85 | - public Map<String, Object> updateDevices(String jsonStr) { | |
| 86 | - Map<String, Object> rs = new HashMap<>(); | |
| 87 | - try { | |
| 88 | - int count=0; | |
| 89 | - JSONArray array = JSONArray.parseArray(jsonStr); | |
| 90 | - JSONObject jObj; | |
| 91 | - for(int i = 0; i < array.size(); i ++){ | |
| 92 | - jObj = array.getJSONObject(i); | |
| 93 | - count += controlJdbcTemp.update("update bsth_c_cars set equipment_code=? where inside_code=?" | |
| 94 | - , jObj.getString("device"), jObj.getString("nbbm")); | |
| 95 | - } | |
| 96 | - | |
| 97 | - //刷新缓存 | |
| 98 | - dataLoader.loadDeviceInfo(); | |
| 99 | - rs.put("status", ResponseCode.SUCCESS); | |
| 100 | - rs.put("count", count); | |
| 101 | - }catch (Exception e){ | |
| 102 | - logger.error("", e); | |
| 103 | - rs.put("status", ResponseCode.ERROR); | |
| 104 | - rs.put("msg", e.getMessage()); | |
| 105 | - } | |
| 106 | - return rs; | |
| 107 | - } | |
| 108 | -} | |
| 1 | +package com.bsth.service.realcontrol.impl; | |
| 2 | + | |
| 3 | +import com.alibaba.fastjson.JSONArray; | |
| 4 | +import com.alibaba.fastjson.JSONObject; | |
| 5 | +import com.bsth.common.ResponseCode; | |
| 6 | +import com.bsth.data.BasicData; | |
| 7 | +import com.bsth.entity.Cars; | |
| 8 | +import com.bsth.repository.CarsRepository; | |
| 9 | +import com.bsth.service.realcontrol.DataManagerService; | |
| 10 | +import com.bsth.util.db.DBUtils_oldSystem; | |
| 11 | +import org.slf4j.Logger; | |
| 12 | +import org.slf4j.LoggerFactory; | |
| 13 | +import org.springframework.beans.factory.annotation.Autowired; | |
| 14 | +import org.springframework.jdbc.core.JdbcTemplate; | |
| 15 | +import org.springframework.stereotype.Service; | |
| 16 | + | |
| 17 | +import java.util.ArrayList; | |
| 18 | +import java.util.HashMap; | |
| 19 | +import java.util.List; | |
| 20 | +import java.util.Map; | |
| 21 | + | |
| 22 | +/** | |
| 23 | + * Created by panzhao on 2017/4/18. | |
| 24 | + */ | |
| 25 | +//@Service | |
| 26 | +public class DataManagerServiceImpl implements DataManagerService{ | |
| 27 | + | |
| 28 | + @Autowired | |
| 29 | + CarsRepository carsRepository; | |
| 30 | + | |
| 31 | + @Autowired | |
| 32 | + JdbcTemplate controlJdbcTemp; | |
| 33 | + | |
| 34 | + @Autowired | |
| 35 | + BasicData.BasicDataLoader dataLoader; | |
| 36 | + | |
| 37 | + Logger logger = LoggerFactory.getLogger(this.getClass()); | |
| 38 | + | |
| 39 | + @Override | |
| 40 | + public Map<String, Object> carInfos(Integer lineId) { | |
| 41 | + | |
| 42 | + Map<String, Object> rs = new HashMap<>(); | |
| 43 | + try { | |
| 44 | + List<Map<String, String>> nowData = new ArrayList<>(); | |
| 45 | + List<Map<String, String>> oldData = new ArrayList<>(); | |
| 46 | + Map<String, String> map; | |
| 47 | + | |
| 48 | + //查询新系统车辆信息 | |
| 49 | + List<Cars> list = carsRepository.findCarsByLineId(lineId); | |
| 50 | + for(Cars c : list){ | |
| 51 | + map = new HashMap<>(); | |
| 52 | + map.put("nbbm", c.getInsideCode()); | |
| 53 | + map.put("device", c.getEquipmentCode()); | |
| 54 | + nowData.add(map); | |
| 55 | + } | |
| 56 | + | |
| 57 | + //获取老系统数据 | |
| 58 | + JdbcTemplate jdbcTemplate = new JdbcTemplate(DBUtils_oldSystem.getDataSource()); | |
| 59 | + List<Map<String, Object>> oyList = jdbcTemplate.queryForList("select NBBM,SBBH from JJWGPS_T_CLXXB t where xlbm=?", BasicData.lineId2CodeMap.get(lineId)); | |
| 60 | + for(Map<String, Object> tempMap : oyList){ | |
| 61 | + map = new HashMap<>(); | |
| 62 | + map.put("nbbm", tempMap.get("NBBM").toString()); | |
| 63 | + map.put("device", tempMap.get("SBBH").toString()); | |
| 64 | + oldData.add(map); | |
| 65 | + } | |
| 66 | + | |
| 67 | + rs.put("status", ResponseCode.SUCCESS); | |
| 68 | + rs.put("nows", nowData); | |
| 69 | + rs.put("olds", oldData); | |
| 70 | + }catch (Exception e){ | |
| 71 | + logger.error("", e); | |
| 72 | + rs.put("status", ResponseCode.ERROR); | |
| 73 | + rs.put("msg", e.getMessage()); | |
| 74 | + } | |
| 75 | + | |
| 76 | + return rs; | |
| 77 | + } | |
| 78 | + | |
| 79 | + /** | |
| 80 | + * 更新设备号 | |
| 81 | + * @param jsonStr | |
| 82 | + * @return | |
| 83 | + */ | |
| 84 | + @Override | |
| 85 | + public Map<String, Object> updateDevices(String jsonStr) { | |
| 86 | + Map<String, Object> rs = new HashMap<>(); | |
| 87 | + try { | |
| 88 | + int count=0; | |
| 89 | + JSONArray array = JSONArray.parseArray(jsonStr); | |
| 90 | + JSONObject jObj; | |
| 91 | + for(int i = 0; i < array.size(); i ++){ | |
| 92 | + jObj = array.getJSONObject(i); | |
| 93 | + count += controlJdbcTemp.update("update bsth_c_cars set equipment_code=? where inside_code=?" | |
| 94 | + , jObj.getString("device"), jObj.getString("nbbm")); | |
| 95 | + } | |
| 96 | + | |
| 97 | + //刷新缓存 | |
| 98 | + dataLoader.loadDeviceInfo(); | |
| 99 | + rs.put("status", ResponseCode.SUCCESS); | |
| 100 | + rs.put("count", count); | |
| 101 | + }catch (Exception e){ | |
| 102 | + logger.error("", e); | |
| 103 | + rs.put("status", ResponseCode.ERROR); | |
| 104 | + rs.put("msg", e.getMessage()); | |
| 105 | + } | |
| 106 | + return rs; | |
| 107 | + } | |
| 108 | +} | ... | ... |
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.*; | |
| 7 | -import java.util.regex.Matcher; | |
| 8 | -import java.util.regex.Pattern; | |
| 9 | - | |
| 10 | -import com.bsth.common.ResponseCode; | |
| 11 | -import org.apache.commons.lang3.StringUtils; | |
| 12 | -import org.apache.poi.hssf.usermodel.HSSFCell; | |
| 13 | -import org.apache.poi.hssf.usermodel.HSSFCellStyle; | |
| 14 | -import org.apache.poi.hssf.usermodel.HSSFFont; | |
| 15 | -import org.apache.poi.hssf.usermodel.HSSFRow; | |
| 16 | -import org.apache.poi.hssf.usermodel.HSSFSheet; | |
| 17 | -import org.apache.poi.hssf.usermodel.HSSFWorkbook; | |
| 18 | -import org.apache.poi.poifs.filesystem.POIFSFileSystem; | |
| 19 | -import org.apache.poi.ss.usermodel.Cell; | |
| 20 | -import org.apache.poi.ss.usermodel.Workbook; | |
| 21 | -import org.apache.poi.ss.util.CellRangeAddress; | |
| 22 | - | |
| 23 | -import com.bsth.entity.Line; | |
| 24 | -import com.bsth.entity.realcontrol.ScheduleRealInfo; | |
| 25 | -import org.apache.poi.ss.util.RegionUtil; | |
| 26 | -import org.apache.poi.xssf.usermodel.XSSFCell; | |
| 27 | - | |
| 28 | -public class ReportUtils { | |
| 29 | - // private final String packaegName = "com.bsth.entity."; | |
| 30 | - private final String packaegName = "com.bsth.entity.realcontrol."; | |
| 31 | - | |
| 32 | - private final Pattern pattern = Pattern.compile("^\\d+(\\.\\d*)?$"); | |
| 33 | - | |
| 34 | - | |
| 35 | - /** | |
| 36 | - * /** | |
| 37 | - * | |
| 38 | - * @param list | |
| 39 | - * 模板中,需要重复显示的行所需的数据 | |
| 40 | - * @param map | |
| 41 | - * 模板中除以上list外所有的数据 | |
| 42 | - * @param index | |
| 43 | - * 需要重复的行号,该值为行号减1 | |
| 44 | - * @param sourcePath | |
| 45 | - * 模板路径 | |
| 46 | - * @param targetPath | |
| 47 | - * 生成路径 | |
| 48 | - */ | |
| 49 | - public void excelReplace(List<Iterator<?>> list, Object[] tArray, | |
| 50 | - String sourcePath, String targetPath,String...NaN) { | |
| 51 | - try { | |
| 52 | - // 把源文件放入流中 | |
| 53 | - POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream( | |
| 54 | - sourcePath)); | |
| 55 | - HSSFWorkbook wb = new HSSFWorkbook(fs); | |
| 56 | - if(tArray.length != 0 && tArray[0] instanceof java.util.Map){ | |
| 57 | - Map<String, Object> m = (Map<String, Object>)tArray[0]; | |
| 58 | - if(m.containsKey("sheetName") && m.get("sheetName")!=null | |
| 59 | - && m.get("sheetName").toString().trim().length()!=0) | |
| 60 | - wb.setSheetName(0, m.get("sheetName").toString()); | |
| 61 | - } | |
| 62 | - HSSFSheet sheet = wb.getSheetAt(0); | |
| 63 | - HSSFRow row; | |
| 64 | - HSSFCell cell = null; | |
| 65 | - String key; | |
| 66 | - // 取得总行数 | |
| 67 | - int rowNum = sheet.getLastRowNum(); | |
| 68 | - // 取得总列数 | |
| 69 | - int cellNum = sheet.getRow(0).getLastCellNum(); | |
| 70 | - | |
| 71 | - // 遍历行 | |
| 72 | - for (int i = 0; i < rowNum; i++) { | |
| 73 | - row = sheet.getRow(i); | |
| 74 | - // 遍历列 | |
| 75 | - for (int j = 0; j < cellNum; j++) { | |
| 76 | - if (row == null) { | |
| 77 | - continue; | |
| 78 | - } | |
| 79 | - cell = row.getCell(j); | |
| 80 | - if (cell == null) { | |
| 81 | - continue; | |
| 82 | - } | |
| 83 | - // 取得每列的内容,如果列内容是$key$格式,则替换内容 | |
| 84 | - key = getCellValue(cell); | |
| 85 | - if (key != null && (key.indexOf("$") != -1 || key.indexOf("#list#") != -1)) { | |
| 86 | - // * 列中内容有#list#,则表示该行为模板行,需要以该行为模板 | |
| 87 | - // * 例如:模板行格式 #list#0_0 $Car.id$ | |
| 88 | - // * 第一个0表示需要在list中取iterator的索引值 | |
| 89 | - // * 第二个0表示在iterator中取的第几个对象,如果不为0,则取下一个对象,而不是沿用前面获取的对象 | |
| 90 | - // * $Car.id$表示所取的对象为Car的对象,并且取值为id的值 | |
| 91 | - if (key.indexOf("#list#") != -1) { | |
| 92 | - key = key.replace("#list#", "").trim(); | |
| 93 | - String[] lists = key.split(" "); | |
| 94 | - // 取得list中的索引值 | |
| 95 | - int listIndex = Integer | |
| 96 | - .valueOf(lists[0].split("_")[0]); | |
| 97 | - Iterator<?> iterator = list.get(listIndex); | |
| 98 | - // 根据模板创建行并填弃数据,返回增加的行数 | |
| 99 | - int rowCount = iteratorFillCellValue(wb, sheet, | |
| 100 | - cell, iterator, i, rowNum, key,NaN); | |
| 101 | - rowNum += rowCount; | |
| 102 | - i += rowCount; | |
| 103 | - break; | |
| 104 | - } else { | |
| 105 | - // 直接填充数据的列,从对象数组中取得值,这里的数组不传值 | |
| 106 | - getValueAndSetCellValue(cell, key, tArray, new String[]{""},NaN); | |
| 107 | - } | |
| 108 | - } | |
| 109 | - | |
| 110 | - } | |
| 111 | - } | |
| 112 | - // 创建目标文件夹 | |
| 113 | - createFolder(targetPath); | |
| 114 | - // 输出文件 | |
| 115 | - FileOutputStream fileOut = new FileOutputStream(targetPath); | |
| 116 | - wb.write(fileOut); | |
| 117 | - fileOut.close(); | |
| 118 | - } catch (Exception e) { | |
| 119 | - e.printStackTrace(); | |
| 120 | - } | |
| 121 | - } | |
| 122 | - | |
| 123 | - | |
| 124 | - /** | |
| 125 | - * | |
| 126 | - * @param sheetList 多sheet模板中,需要重复显示的行所需的数据 | |
| 127 | - * @param tArray | |
| 128 | - * @param sourcePath 模板路径 | |
| 129 | - * @param targetPath 生成路径 | |
| 130 | - * @param NaN 非数字参数(String[] 传入的key不做数字处理) | |
| 131 | - */ | |
| 132 | - public void excelMoreSheetReplace(List<List<Iterator<?>>> sheetList, Object[] tArray, | |
| 133 | - String sourcePath, String targetPath,String...NaN) { | |
| 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[]{""},NaN); | |
| 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 = getCellValue(row.getCell(j)); | |
| 265 | - switch(row.getCell(j).getCellType()) { | |
| 266 | - // String | |
| 267 | - case HSSFCell.CELL_TYPE_STRING: | |
| 268 | - rowCreat.getCell(j).setCellValue(row.getCell(j).getStringCellValue().toString()); | |
| 269 | - break; | |
| 270 | - // numeric类型,excel中,日期格式会转成数字格式存储 | |
| 271 | - case HSSFCell.CELL_TYPE_NUMERIC: | |
| 272 | - rowCreat.getCell(j).setCellValue(row.getCell(j).getNumericCellValue()); | |
| 273 | - break; | |
| 274 | - // 公式类型 | |
| 275 | - case HSSFCell.CELL_TYPE_FORMULA: | |
| 276 | - rowCreat.getCell(j).setCellValue(row.getCell(j).getCellFormula()); | |
| 277 | - break; | |
| 278 | - // boolean类型 | |
| 279 | - case HSSFCell.CELL_TYPE_BOOLEAN: | |
| 280 | - rowCreat.getCell(j).setCellValue(row.getCell(j).getBooleanCellValue()); | |
| 281 | - break; | |
| 282 | - // 空格 | |
| 283 | - case HSSFCell.CELL_TYPE_BLANK: | |
| 284 | - rowCreat.getCell(j).setCellValue(""); | |
| 285 | - break; | |
| 286 | - // 错误值 | |
| 287 | - case HSSFCell.CELL_TYPE_ERROR: | |
| 288 | - rowCreat.getCell(j).setCellValue(""); | |
| 289 | - break; | |
| 290 | - default : | |
| 291 | - rowCreat.getCell(j).setCellValue(""); | |
| 292 | - break; | |
| 293 | - } | |
| 294 | -// strVal = row.getCell(j).getStringCellValue(); | |
| 295 | -// rowCreat.getCell(j).setCellValue(strVal); | |
| 296 | - copyCellStyle(wb1, row.getCell(j).getCellStyle(), createCellStyle); | |
| 297 | - createCellStyle.setBorderTop((short)1); | |
| 298 | - createCellStyle.setBorderLeft((short)1); | |
| 299 | - createCellStyle.setBorderRight((short)1); | |
| 300 | - createCellStyle.setBorderBottom((short)1); | |
| 301 | - rowCreat.getCell(j).setCellStyle(createCellStyle); | |
| 302 | - } | |
| 303 | - } | |
| 304 | - } | |
| 305 | - | |
| 306 | -// int firstRowNum = createSheet.getFirstRowNum(); | |
| 307 | -// int lastRowNum = createSheet.getLastRowNum(); | |
| 308 | -// int test = 0; | |
| 309 | -// for(int k = firstRowNum; k <= lastRowNum; k++){ | |
| 310 | -// HSSFRow createRow = createSheet.getRow(k); | |
| 311 | -// int firstCellNum = createRow.getFirstCellNum(); | |
| 312 | -// int lastCellNum = createRow.getLastCellNum(); | |
| 313 | -// for(int i = firstCellNum; i < lastCellNum; i++){ | |
| 314 | -// HSSFCell cell = createRow.getCell(i); | |
| 315 | -// cell.getCellStyle().setBorderTop(HSSFCellStyle.BORDER_THIN); | |
| 316 | -// cell.getCellStyle().setBorderLeft(HSSFCellStyle.BORDER_THIN); | |
| 317 | -// cell.getCellStyle().setBorderRight(HSSFCellStyle.BORDER_THIN); | |
| 318 | -// cell.getCellStyle().setBorderBottom(HSSFCellStyle.BORDER_THIN); | |
| 319 | -// test ++; | |
| 320 | -// } | |
| 321 | -// } | |
| 322 | -// System.out.println("test = " + test); | |
| 323 | - | |
| 324 | - FileOutputStream fileOut = new FileOutputStream(file2); | |
| 325 | - wb2.write(fileOut); | |
| 326 | - fileOut.close(); | |
| 327 | - wb2.close(); | |
| 328 | - wb1.close(); | |
| 329 | - fs2.close(); | |
| 330 | - fs1.close(); | |
| 331 | - file1.delete(); | |
| 332 | -// // 创建目标文件夹 | |
| 333 | -// createFolder(targetPath); | |
| 334 | - // 输出文件 | |
| 335 | - } catch (Exception e) { | |
| 336 | - e.printStackTrace(); | |
| 337 | - } | |
| 338 | - } | |
| 339 | - | |
| 340 | - public void test(File file){ | |
| 341 | - POIFSFileSystem fs; | |
| 342 | - try { | |
| 343 | - fs = new POIFSFileSystem(new FileInputStream(file)); | |
| 344 | - HSSFWorkbook wb = new HSSFWorkbook(fs); | |
| 345 | - for(int j = 0; j < wb.getNumberOfSheets(); j++){ | |
| 346 | - HSSFSheet sheet = wb.getSheetAt(j); | |
| 347 | - int firstRowNum = sheet.getFirstRowNum(); | |
| 348 | - int lastRowNum = sheet.getLastRowNum(); | |
| 349 | - int test = 0; | |
| 350 | - for(int k = firstRowNum; k <= lastRowNum; k++){ | |
| 351 | - HSSFRow createRow = sheet.getRow(k); | |
| 352 | - int firstCellNum = createRow.getFirstCellNum(); | |
| 353 | - int lastCellNum = createRow.getLastCellNum(); | |
| 354 | - for(int i = firstCellNum; i < lastCellNum; i++){ | |
| 355 | - HSSFCell cell = createRow.getCell(i); | |
| 356 | - HSSFCellStyle cellStyle = wb.createCellStyle(); | |
| 357 | - | |
| 358 | - cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); | |
| 359 | - cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); | |
| 360 | - cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); | |
| 361 | - cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); | |
| 362 | - cell.setCellStyle(cellStyle); | |
| 363 | - test ++; | |
| 364 | - } | |
| 365 | - } | |
| 366 | - System.out.println("test = " + test); | |
| 367 | - | |
| 368 | - FileOutputStream fileOut = new FileOutputStream(file); | |
| 369 | - wb.write(fileOut); | |
| 370 | - fileOut.close(); | |
| 371 | - } | |
| 372 | - } catch (Exception e) { | |
| 373 | - // TODO Auto-generated catch block | |
| 374 | - e.printStackTrace(); | |
| 375 | - } | |
| 376 | - | |
| 377 | - } | |
| 378 | - | |
| 379 | - public String getCellValue(HSSFCell cell) { | |
| 380 | - int cellType = 0; | |
| 381 | - String result = ""; | |
| 382 | - double d; | |
| 383 | - if(cell != null) { | |
| 384 | - // 获取列数据类型 | |
| 385 | - cellType = cell.getCellType(); | |
| 386 | - // 不同列数据类型,取值方法不同 | |
| 387 | - switch(cellType) { | |
| 388 | - // String | |
| 389 | - case HSSFCell.CELL_TYPE_STRING: | |
| 390 | - result = cell.getStringCellValue().toString(); | |
| 391 | - break; | |
| 392 | - // numeric类型,excel中,日期格式会转成数字格式存储 | |
| 393 | - case HSSFCell.CELL_TYPE_NUMERIC: | |
| 394 | - result = cell.getNumericCellValue()+""; | |
| 395 | - break; | |
| 396 | - // 公式类型 | |
| 397 | - case HSSFCell.CELL_TYPE_FORMULA: | |
| 398 | - result = cell.getCellFormula() + ""; | |
| 399 | - break; | |
| 400 | - // boolean类型 | |
| 401 | - case HSSFCell.CELL_TYPE_BOOLEAN: | |
| 402 | - result = cell.getBooleanCellValue() + ""; | |
| 403 | - break; | |
| 404 | - // 空格 | |
| 405 | - case HSSFCell.CELL_TYPE_BLANK: | |
| 406 | - result = null; | |
| 407 | - break; | |
| 408 | - // 错误值 | |
| 409 | - case HSSFCell.CELL_TYPE_ERROR: | |
| 410 | - result = null; | |
| 411 | - break; | |
| 412 | - default : | |
| 413 | - System.out.println("其它"); | |
| 414 | - break; | |
| 415 | - } | |
| 416 | - } | |
| 417 | - return result; | |
| 418 | - } | |
| 419 | - | |
| 420 | - /** | |
| 421 | - * 根据iterator,以及模板中的标识,填充模板 | |
| 422 | - * | |
| 423 | - * @param wb | |
| 424 | - * @param sheet | |
| 425 | - * @param cell | |
| 426 | - * @param iterator | |
| 427 | - * iterator | |
| 428 | - * @param index | |
| 429 | - * 模板行索引 | |
| 430 | - * @param rowNum | |
| 431 | - * 表格总行数 | |
| 432 | - * @param key | |
| 433 | - * 表格内容 | |
| 434 | - * @param NaN 非数字参数(String[] 传入的key不做数字处理) | |
| 435 | - * @return | |
| 436 | - */ | |
| 437 | - private int iteratorFillCellValue(HSSFWorkbook wb, HSSFSheet sheet, | |
| 438 | - HSSFCell cell, Iterator<?> iterator, int index, int rowNum, | |
| 439 | - String key,String...NaN) { | |
| 440 | - int rowCount = 0; | |
| 441 | - Object obj = null; | |
| 442 | - int p = 0; | |
| 443 | - int i = index; | |
| 444 | - HSSFRow newRow = null; | |
| 445 | - int tmpCellNum = 0; | |
| 446 | - int k = 0; | |
| 447 | - int listIndex = 0; | |
| 448 | - // 取得模板行 | |
| 449 | - HSSFRow orgRow = sheet.getRow(index); | |
| 450 | - HSSFCellStyle style= wb.createCellStyle(); | |
| 451 | - try { | |
| 452 | - while (iterator.hasNext()) { | |
| 453 | - // 取得iterator的对象 | |
| 454 | - obj = iterator.next(); | |
| 455 | - // 移动当前编辑行以下的所有行 | |
| 456 | - if (p != 0) { | |
| 457 | - rowNum += 1; | |
| 458 | - i += 1; | |
| 459 | - rowCount += 1;// 增加的总行数 | |
| 460 | - // 把当前行以下的所有行往下移动1行 | |
| 461 | - sheet.shiftRows(i, rowNum, 1); | |
| 462 | - } | |
| 463 | - p = 1; | |
| 464 | - // 创建新行 | |
| 465 | - newRow = sheet.createRow(index + k++); | |
| 466 | - // 把新行的内容换成和模板行一样 | |
| 467 | - copyRow(wb, orgRow, newRow, true,style); | |
| 468 | - tmpCellNum = newRow.getLastCellNum(); | |
| 469 | - for (int l = 0; l < tmpCellNum; l++) { | |
| 470 | - cell = newRow.getCell(l); | |
| 471 | - key = getCellValue(cell); | |
| 472 | - /** | |
| 473 | - * 如果单无格内容为#list#,表示该行是模板行 #list#0_0 | |
| 474 | - * 第一个0表示需要在list中取iterator的索引值 | |
| 475 | - * 第二个0表示在iterator中取的第几个对象,如果不为0,则取下一个对象,而不是沿用前面获取的对象 | |
| 476 | - */ | |
| 477 | - if(key == null || key.equals("")){ | |
| 478 | - obj = iterator.next(); | |
| 479 | - } | |
| 480 | - if (key.indexOf("#list#") != -1 && key.indexOf("_0") == -1) { | |
| 481 | - if (iterator.hasNext()) { | |
| 482 | - obj = iterator.next(); | |
| 483 | - } else { | |
| 484 | - obj = null; | |
| 485 | - } | |
| 486 | - } | |
| 487 | - if (key.trim().indexOf(" ") != -1) { | |
| 488 | - key = key.split(" ")[1]; | |
| 489 | - } | |
| 490 | - getValueAndSetCellValue(cell, key, obj,new String[]{listIndex+""},NaN); | |
| 491 | - } | |
| 492 | - // list的数量 | |
| 493 | - listIndex ++; | |
| 494 | - } | |
| 495 | - } catch (Exception e) { | |
| 496 | - e.printStackTrace(); | |
| 497 | - } | |
| 498 | - return rowCount; | |
| 499 | - } | |
| 500 | - | |
| 501 | - /** | |
| 502 | - * 取到相应的值并填入相应的列中 | |
| 503 | - * | |
| 504 | - * @param cell 列 | |
| 505 | - * @param key 列内容 | |
| 506 | - * @param obj 数据源对象 | |
| 507 | - * @param args 其他参数 数组 | |
| 508 | - * @param NaN 非数字参数(String[] 传入的key不做数字处理) | |
| 509 | - * 数组内容: | |
| 510 | - * 0位:在list中的第几条数据 | |
| 511 | - */ | |
| 512 | - private void getValueAndSetCellValue(HSSFCell cell, String key, Object obj, String[] args,String...NaN) { | |
| 513 | - try { | |
| 514 | - // 保有存单元格的内容 | |
| 515 | - String cellValue = key = key.replace("\\n", ""); | |
| 516 | - String tmpKey; | |
| 517 | - String fieldName = null; | |
| 518 | - // 判断单元格内容是否是公式 | |
| 519 | - if(cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA){ | |
| 520 | - Pattern p=Pattern.compile("(\\d+)"); // | |
| 521 | - Matcher m=p.matcher(key); | |
| 522 | - if(m.find()){ | |
| 523 | - cellValue = key.replace(m.group(1), | |
| 524 | - Integer.valueOf(m.group(1))+Integer.valueOf(args[0])+""); | |
| 525 | - cell.setCellFormula(cellValue); | |
| 526 | - return; | |
| 527 | - } | |
| 528 | - }else{//其他格式 | |
| 529 | - | |
| 530 | - // 循环截取两个$中间的内容,反射出值 | |
| 531 | - while (key.indexOf("$") != -1) { | |
| 532 | - key = key.substring(key.indexOf("$") + 1); | |
| 533 | - // 取两个$中间的内容 | |
| 534 | - tmpKey = key.substring(0, key.indexOf("$")); | |
| 535 | - key = key.substring(key.indexOf("$") + 1); | |
| 536 | - // 如果内容是如下格式Cars.id,则从obj中值得相应的对象的值 | |
| 537 | - if (tmpKey.indexOf(".") != -1) { | |
| 538 | - String className = tmpKey.substring(0, tmpKey.indexOf(".")); | |
| 539 | - // 取得类的全限定名 | |
| 540 | - String classWholeName = packaegName + className; | |
| 541 | - fieldName = tmpKey.substring(tmpKey.indexOf(".") + 1); | |
| 542 | - // 如果obj是数组,循环判断哪个对象是对应的 | |
| 543 | - if (obj instanceof Object[]) { | |
| 544 | - Object[] objs = (Object[]) obj; | |
| 545 | - for (int k = 0; k < objs.length; k++) { | |
| 546 | - if (objs[k].getClass().getName().equals(classWholeName)) { | |
| 547 | - cellValue = cellValue.replace("$" + tmpKey | |
| 548 | - + "$", getKeyValue(objs[k], fieldName) | |
| 549 | - + ""); | |
| 550 | - } else if(objs[k].getClass().getName().equals("java.util.HashMap")){ | |
| 551 | - Map<String,Object> map = (HashMap<String,Object>)objs[k]; | |
| 552 | - cellValue = cellValue.replace("$" + tmpKey + "$",map.get(fieldName)+""); | |
| 553 | - } | |
| 554 | - } | |
| 555 | - } else if (obj.getClass().getName().equals(classWholeName)) { | |
| 556 | - cellValue = cellValue.replace("$" + tmpKey + "$",getKeyValue(obj, fieldName) + ""); | |
| 557 | - } else if (obj.getClass().getName().equals("java.util.HashMap")){ | |
| 558 | - Map<String,Object> map = (HashMap<String,Object>)obj; | |
| 559 | - cellValue = cellValue.replace("$" + tmpKey + "$",map.get(fieldName)+""); | |
| 560 | - } | |
| 561 | - } | |
| 562 | - } | |
| 563 | - } | |
| 564 | - cell.setCellValue(cellValue); | |
| 565 | - Matcher matcher = pattern.matcher(cellValue); | |
| 566 | - if (matcher.matches() && !Arrays.asList(NaN).contains(fieldName)) { | |
| 567 | - if (cellValue.indexOf(".") > -1) { | |
| 568 | - cell.setCellValue(Double.parseDouble(cellValue)); | |
| 569 | - } else { | |
| 570 | - cell.setCellValue(Integer.parseInt(cellValue)); | |
| 571 | - } | |
| 572 | - } | |
| 573 | - } catch (Exception e) { | |
| 574 | - e.printStackTrace(); | |
| 575 | - } | |
| 576 | - | |
| 577 | - } | |
| 578 | - | |
| 579 | - /** | |
| 580 | - * 给列填充数据 | |
| 581 | - * | |
| 582 | - * @param cell | |
| 583 | - * 列 | |
| 584 | - * @param obj | |
| 585 | - * 数据源对象 | |
| 586 | - * @param fieldName | |
| 587 | - * 需要取数据的字段 | |
| 588 | - */ | |
| 589 | - private Object getKeyValue(Object obj, String fieldName) { | |
| 590 | - Object value = ""; | |
| 591 | - try { | |
| 592 | - if (obj != null) { | |
| 593 | - ReportRelatedUtils test = new ReportRelatedUtils(); | |
| 594 | - value = test.getValue(obj, fieldName) == null ? "" : test .getValue(obj, fieldName); | |
| 595 | - } | |
| 596 | - } catch (Exception e) { | |
| 597 | - e.printStackTrace(); | |
| 598 | - } | |
| 599 | - return value; | |
| 600 | - } | |
| 601 | - | |
| 602 | - public static void main(String[] args) { | |
| 603 | - | |
| 604 | - try { | |
| 605 | - ReportUtils ee = new ReportUtils(); | |
| 606 | - List<Iterator<?>> list = new ArrayList<Iterator<?>>(); | |
| 607 | - Line line = new Line(); | |
| 608 | - line.setId(1); | |
| 609 | - line.setName("line1"); | |
| 610 | - | |
| 611 | - List<Object> dataList = new ArrayList<Object>(); | |
| 612 | - | |
| 613 | - | |
| 614 | - ScheduleRealInfo srr = new ScheduleRealInfo(); | |
| 615 | - srr.setId((long) 111); | |
| 616 | - srr.setXlName("abc11"); | |
| 617 | - | |
| 618 | - ScheduleRealInfo sr = new ScheduleRealInfo(); | |
| 619 | - sr.setZdsj("06:10"); | |
| 620 | - sr.setZdsjActual("06:25"); | |
| 621 | - dataList.add(sr); | |
| 622 | - sr = new ScheduleRealInfo(); | |
| 623 | - sr.setZdsj("06:20"); | |
| 624 | - sr.setZdsjActual(""); | |
| 625 | - dataList.add(sr); | |
| 626 | - list.add(dataList.iterator()); | |
| 627 | - | |
| 628 | - ee.excelReplace(list, new Object[] { srr }, "D:/waybill.xls", | |
| 629 | - "D:/22.xls"); | |
| 630 | - System.out.println("ok"); | |
| 631 | - } catch (Exception e) { | |
| 632 | - e.printStackTrace(); | |
| 633 | - } | |
| 634 | - } | |
| 635 | - | |
| 636 | - /** | |
| 637 | - * 行复制功能 | |
| 638 | - * | |
| 639 | - * @param fromRow | |
| 640 | - * @param toRow | |
| 641 | - */ | |
| 642 | - private void copyRow(HSSFWorkbook wb, HSSFRow fromRow, HSSFRow toRow, | |
| 643 | - boolean copyValueFlag, HSSFCellStyle style) { | |
| 644 | - for (Iterator<Cell> cellIt = fromRow.cellIterator(); cellIt.hasNext();) { | |
| 645 | - HSSFCell tmpCell = (HSSFCell) cellIt.next(); | |
| 646 | - HSSFCell newCell = toRow.createCell(tmpCell.getColumnIndex(), 0); | |
| 647 | - copyCell(wb, tmpCell, newCell, copyValueFlag, tmpCell.getCellStyle()); | |
| 648 | - } | |
| 649 | - } | |
| 650 | - | |
| 651 | - /** | |
| 652 | - * 复制单元格 | |
| 653 | - * | |
| 654 | - * @param srcCell | |
| 655 | - * @param distCell | |
| 656 | - * @param copyValueFlag | |
| 657 | - * true则连同cell的内容一起复制 | |
| 658 | - */ | |
| 659 | - public void copyCell(HSSFWorkbook wb, HSSFCell srcCell, HSSFCell distCell, | |
| 660 | - boolean copyValueFlag, HSSFCellStyle newstyle) { | |
| 661 | -// HSSFCellStyle newstyle = wb.createCellStyle(); | |
| 662 | - copyCellStyle(wb, srcCell.getCellStyle(), newstyle); | |
| 663 | - // 样式 | |
| 664 | - distCell.setCellStyle(newstyle); | |
| 665 | - // 评论 | |
| 666 | - if (srcCell.getCellComment() != null) { | |
| 667 | - distCell.setCellComment(srcCell.getCellComment()); | |
| 668 | - } | |
| 669 | - // 不同数据类型处理 | |
| 670 | - int srcCellType = srcCell.getCellType(); | |
| 671 | - distCell.setCellType(srcCellType); | |
| 672 | - if (copyValueFlag) { | |
| 673 | - if (srcCellType == HSSFCell.CELL_TYPE_NUMERIC) { | |
| 674 | - distCell.setCellValue(srcCell.getDateCellValue()); | |
| 675 | - } else if (srcCellType == HSSFCell.CELL_TYPE_STRING) { | |
| 676 | - distCell.setCellValue(srcCell.getRichStringCellValue()); | |
| 677 | - } else if (srcCellType == HSSFCell.CELL_TYPE_BLANK) { | |
| 678 | - | |
| 679 | - } else if (srcCellType == HSSFCell.CELL_TYPE_BOOLEAN) { | |
| 680 | - distCell.setCellValue(srcCell.getBooleanCellValue()); | |
| 681 | - } else if (srcCellType == HSSFCell.CELL_TYPE_ERROR) { | |
| 682 | - distCell.setCellErrorValue(srcCell.getErrorCellValue()); | |
| 683 | - } else if (srcCellType == HSSFCell.CELL_TYPE_FORMULA) { | |
| 684 | - distCell.setCellFormula(srcCell.getCellFormula()); | |
| 685 | - } else { | |
| 686 | - } | |
| 687 | - } | |
| 688 | - } | |
| 689 | - | |
| 690 | - /** | |
| 691 | - * 复制一个单元格样式到目的单元格样式 | |
| 692 | - * | |
| 693 | - * @param fromStyle | |
| 694 | - * @param toStyle | |
| 695 | - */ | |
| 696 | - public void copyCellStyle(HSSFWorkbook wb, HSSFCellStyle fromStyle, | |
| 697 | - HSSFCellStyle toStyle) { | |
| 698 | - toStyle.setAlignment(fromStyle.getAlignment()); | |
| 699 | - // 边框和边框颜色 | |
| 700 | - toStyle.setBorderBottom(fromStyle.getBorderBottom()); | |
| 701 | - toStyle.setBorderLeft(fromStyle.getBorderLeft()); | |
| 702 | - toStyle.setBorderRight(fromStyle.getBorderRight()); | |
| 703 | - toStyle.setBorderTop(fromStyle.getBorderTop()); | |
| 704 | - toStyle.setTopBorderColor(fromStyle.getTopBorderColor()); | |
| 705 | - toStyle.setBottomBorderColor(fromStyle.getBottomBorderColor()); | |
| 706 | - toStyle.setRightBorderColor(fromStyle.getRightBorderColor()); | |
| 707 | - toStyle.setLeftBorderColor(fromStyle.getLeftBorderColor()); | |
| 708 | - | |
| 709 | - // 背景和前景 | |
| 710 | - toStyle.setFillBackgroundColor(fromStyle.getFillBackgroundColor()); | |
| 711 | - toStyle.setFillForegroundColor(fromStyle.getFillForegroundColor()); | |
| 712 | - | |
| 713 | - toStyle.setDataFormat(fromStyle.getDataFormat()); | |
| 714 | - toStyle.setFillPattern(fromStyle.getFillPattern()); | |
| 715 | - toStyle.setHidden(fromStyle.getHidden()); | |
| 716 | - toStyle.setIndention(fromStyle.getIndention());// 首行缩进 | |
| 717 | - toStyle.setLocked(fromStyle.getLocked()); | |
| 718 | - toStyle.setRotation(fromStyle.getRotation());// 旋转 | |
| 719 | - toStyle.setVerticalAlignment(fromStyle.getVerticalAlignment()); | |
| 720 | - toStyle.setWrapText(fromStyle.getWrapText()); | |
| 721 | - // 字体 | |
| 722 | - toStyle.setFont(fromStyle.getFont(wb)); | |
| 723 | - | |
| 724 | - } | |
| 725 | - | |
| 726 | - /** | |
| 727 | - * 创建文件夹,并删除原有文件 | |
| 728 | - * | |
| 729 | - * @param path | |
| 730 | - */ | |
| 731 | - private void createFolder(String path) { | |
| 732 | - File targetFile = null; | |
| 733 | - targetFile = new File(path); | |
| 734 | - if (targetFile.exists()) {// 删除原有文件 | |
| 735 | - targetFile.delete(); | |
| 736 | - } | |
| 737 | - // 创建目标文件夹 | |
| 738 | - targetFile = new File(path.substring(0, path.lastIndexOf("/"))); | |
| 739 | - if (!targetFile.exists()) { | |
| 740 | - targetFile.mkdirs(); | |
| 741 | - } | |
| 742 | - } | |
| 743 | - | |
| 744 | - public void createFlie(List<List<String>> list, String name, String type){ | |
| 745 | - HSSFWorkbook workbook = new HSSFWorkbook(); | |
| 746 | - // 生成一个样式 | |
| 747 | - HSSFCellStyle style = workbook.createCellStyle(); | |
| 748 | - // 设置这些样式 | |
| 749 | -// style.setFillForegroundColor(HSSFColor.SKY_BLUE.index); | |
| 750 | -// style.setFillPattern(HSSFCellStyle.BORDER_THIN); | |
| 751 | - style.setBorderBottom(HSSFCellStyle.BORDER_THIN); | |
| 752 | - style.setBorderLeft(HSSFCellStyle.BORDER_THIN); | |
| 753 | - style.setBorderRight(HSSFCellStyle.BORDER_THIN); | |
| 754 | - style.setBorderTop(HSSFCellStyle.BORDER_THIN); | |
| 755 | - style.setAlignment(HSSFCellStyle.ALIGN_CENTER); | |
| 756 | - // 生成一个字体 | |
| 757 | - HSSFFont font = workbook.createFont(); | |
| 758 | -// font.setColor(HSSFColor.VIOLET.index); | |
| 759 | - font.setFontHeightInPoints((short) 12); | |
| 760 | - font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); | |
| 761 | - // 把字体应用到当前的样式 | |
| 762 | - style.setFont(font); | |
| 763 | - HSSFCellStyle cellStyle =workbook.createCellStyle(); | |
| 764 | - cellStyle.setFont(font); | |
| 765 | - cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); //水平布局:居中 | |
| 766 | - cellStyle.setWrapText(true); | |
| 767 | - | |
| 768 | - //设置wordsheet名 | |
| 769 | - HSSFSheet sheetYS = workbook.createSheet();//设置wordsheet名 | |
| 770 | - HSSFRow row = sheetYS.createRow(0); | |
| 771 | - setCellStyleAndValue(row, style, 0, name); | |
| 772 | - CellRangeAddress callRangeAddress = new CellRangeAddress(0,0,0,list.get(0).size()-1); | |
| 773 | - sheetYS.addMergedRegion(callRangeAddress); | |
| 774 | - // 样式 | |
| 775 | - setMergeCellStyle (callRangeAddress, sheetYS, workbook); | |
| 776 | - | |
| 777 | - try{ | |
| 778 | - for(int i=0; i<list.size(); i++){ | |
| 779 | - HSSFRow rowYSi = sheetYS.createRow(i+1); | |
| 780 | - List<String> stringList = list.get(i); | |
| 781 | - int num = 4; | |
| 782 | - if("xl".equals(type)) | |
| 783 | - num = 2; | |
| 784 | - else if("cl".equals(type)) | |
| 785 | - num = 5; | |
| 786 | - for(int j=0; j<stringList.size(); j++){ | |
| 787 | - String str = stringList.get(j); | |
| 788 | - if(i == list.size()-1){ | |
| 789 | - if(j==0) { | |
| 790 | - setCellStyleAndValue(rowYSi, style, j, str); | |
| 791 | - CellRangeAddress callRangeAddressYSi = new CellRangeAddress(i+1,i+1,0,num); | |
| 792 | - sheetYS.addMergedRegion(callRangeAddressYSi); | |
| 793 | - // 样式 | |
| 794 | - setMergeCellStyle (callRangeAddressYSi, sheetYS, workbook); | |
| 795 | - }else | |
| 796 | - setCellStyleAndValue(rowYSi,style,j+num,str); | |
| 797 | - } else { | |
| 798 | - setCellStyleAndValue(rowYSi,style,j,str); | |
| 799 | - } | |
| 800 | - } | |
| 801 | - } | |
| 802 | - | |
| 803 | - // 给列设置宽度自适应 | |
| 804 | - setSizeColumn1(sheetYS,1,list.get(0).size()); | |
| 805 | - String path = this.getClass().getResource("/").getPath() + "static/pages/forms/export/"; | |
| 806 | - String targetPath = path+name+".xls"; | |
| 807 | - createFolder(targetPath); | |
| 808 | - FileOutputStream fout = new FileOutputStream(targetPath); | |
| 809 | - //5.输出 | |
| 810 | - workbook.write(fout); | |
| 811 | - fout.close(); | |
| 812 | - } catch (Exception e) { | |
| 813 | - e.printStackTrace(); | |
| 814 | - } | |
| 815 | - } | |
| 816 | - | |
| 817 | - /** | |
| 818 | - * 自适应宽度(中文支持) | |
| 819 | - * @param sheet | |
| 820 | - * @param size | |
| 821 | - */ | |
| 822 | - private static void setSizeColumn(HSSFSheet sheet, int size) { | |
| 823 | - for (int columnNum = 0; columnNum < size; columnNum++) { | |
| 824 | - int columnWidth = sheet.getColumnWidth(columnNum) / 256; | |
| 825 | - for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) { | |
| 826 | - HSSFRow currentRow; | |
| 827 | - //当前行未被使用过 | |
| 828 | - if (sheet.getRow(rowNum) == null) { | |
| 829 | - currentRow = sheet.createRow(rowNum); | |
| 830 | - } else { | |
| 831 | - currentRow = sheet.getRow(rowNum); | |
| 832 | - } | |
| 833 | - if (currentRow.getCell(columnNum) != null) { | |
| 834 | - HSSFCell currentCell = currentRow.getCell(columnNum); | |
| 835 | - if (currentCell.getCellType() == XSSFCell.CELL_TYPE_STRING) { | |
| 836 | - int length = currentCell.getStringCellValue().getBytes().length; | |
| 837 | - if (columnWidth < length) { | |
| 838 | - columnWidth = length; | |
| 839 | - } | |
| 840 | - } | |
| 841 | - } | |
| 842 | - } | |
| 843 | - sheet.setColumnWidth(columnNum, columnWidth * 300); | |
| 844 | -// sheet.setColumnWidth(columnNum, columnWidth * 256); | |
| 845 | - } | |
| 846 | - } | |
| 847 | - | |
| 848 | - /** | |
| 849 | - * 自适应宽度(中文支持) | |
| 850 | - * @param sheet | |
| 851 | - * @param index 从那一行开始自适应 | |
| 852 | - * @param size | |
| 853 | - */ | |
| 854 | - private static void setSizeColumn1(HSSFSheet sheet,int index, int size) { | |
| 855 | - for (int columnNum = index; columnNum < size; columnNum++) { | |
| 856 | - int columnWidth = sheet.getColumnWidth(columnNum) / 256; | |
| 857 | - for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) { | |
| 858 | - HSSFRow currentRow; | |
| 859 | - //当前行未被使用过 | |
| 860 | - if (sheet.getRow(rowNum) == null) { | |
| 861 | - currentRow = sheet.createRow(rowNum); | |
| 862 | - } else { | |
| 863 | - currentRow = sheet.getRow(rowNum); | |
| 864 | - } | |
| 865 | - if (currentRow.getCell(columnNum) != null) { | |
| 866 | - HSSFCell currentCell = currentRow.getCell(columnNum); | |
| 867 | - if (currentCell.getCellType() == XSSFCell.CELL_TYPE_STRING) { | |
| 868 | - int length = currentCell.getStringCellValue().getBytes().length; | |
| 869 | - if (columnWidth < length) { | |
| 870 | - columnWidth = length; | |
| 871 | - } | |
| 872 | - } | |
| 873 | - } | |
| 874 | - } | |
| 875 | - sheet.setColumnWidth(columnNum, columnWidth * 300); | |
| 876 | -// sheet.setColumnWidth(columnNum, columnWidth * 256); | |
| 877 | - } | |
| 878 | - } | |
| 879 | - | |
| 880 | - /** | |
| 881 | - * 设置单元格值和样式 | |
| 882 | - * @param row | |
| 883 | - * @param style | |
| 884 | - * @param index | |
| 885 | - * @param value | |
| 886 | - */ | |
| 887 | - public static void setCellStyleAndValue(HSSFRow row,HSSFCellStyle style,int index,String value){ | |
| 888 | - HSSFCell cell = row.createCell(index); | |
| 889 | - cell.setCellValue(value); | |
| 890 | - cell.setCellStyle(style); | |
| 891 | - } | |
| 892 | - /** | |
| 893 | - * 设置合并单元格样式 | |
| 894 | - * @param cra | |
| 895 | - * @param sheet | |
| 896 | - * @param workbook | |
| 897 | - */ | |
| 898 | - public static void setMergeCellStyle (CellRangeAddress cra, HSSFSheet sheet, Workbook workbook){ | |
| 899 | - // 使用RegionUtil类为合并后的单元格添加边框 | |
| 900 | - RegionUtil.setBorderBottom(1, cra, sheet, workbook); // 下边框 | |
| 901 | - RegionUtil.setBorderLeft(1, cra, sheet, workbook); // 左边框 | |
| 902 | - RegionUtil.setBorderRight(1, cra, sheet, workbook); // 有边框 | |
| 903 | - RegionUtil.setBorderTop(1, cra, sheet, workbook); // 上边框 | |
| 904 | - } | |
| 905 | -} | |
| 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.*; | |
| 7 | +import java.util.regex.Matcher; | |
| 8 | +import java.util.regex.Pattern; | |
| 9 | + | |
| 10 | +import com.bsth.common.ResponseCode; | |
| 11 | +import org.apache.commons.lang3.StringUtils; | |
| 12 | +import org.apache.poi.hssf.usermodel.HSSFCell; | |
| 13 | +import org.apache.poi.hssf.usermodel.HSSFCellStyle; | |
| 14 | +import org.apache.poi.hssf.usermodel.HSSFFont; | |
| 15 | +import org.apache.poi.hssf.usermodel.HSSFRow; | |
| 16 | +import org.apache.poi.hssf.usermodel.HSSFSheet; | |
| 17 | +import org.apache.poi.hssf.usermodel.HSSFWorkbook; | |
| 18 | +import org.apache.poi.poifs.filesystem.POIFSFileSystem; | |
| 19 | +import org.apache.poi.ss.usermodel.Cell; | |
| 20 | +import org.apache.poi.ss.usermodel.Workbook; | |
| 21 | +import org.apache.poi.ss.util.CellRangeAddress; | |
| 22 | + | |
| 23 | +import com.bsth.entity.Line; | |
| 24 | +import com.bsth.entity.realcontrol.ScheduleRealInfo; | |
| 25 | +import org.apache.poi.ss.util.RegionUtil; | |
| 26 | +import org.apache.poi.xssf.usermodel.XSSFCell; | |
| 27 | + | |
| 28 | +public class ReportUtils { | |
| 29 | + // private final String packaegName = "com.bsth.entity."; | |
| 30 | + private final String packaegName = "com.bsth.entity.realcontrol."; | |
| 31 | + | |
| 32 | + private final Pattern pattern = Pattern.compile("^\\d+(\\.\\d*)?$"); | |
| 33 | + | |
| 34 | + | |
| 35 | + /** | |
| 36 | + * /** | |
| 37 | + * | |
| 38 | + * @param list | |
| 39 | + * 模板中,需要重复显示的行所需的数据 | |
| 40 | + * @param map | |
| 41 | + * 模板中除以上list外所有的数据 | |
| 42 | + * @param index | |
| 43 | + * 需要重复的行号,该值为行号减1 | |
| 44 | + * @param sourcePath | |
| 45 | + * 模板路径 | |
| 46 | + * @param targetPath | |
| 47 | + * 生成路径 | |
| 48 | + */ | |
| 49 | + public void excelReplace(List<Iterator<?>> list, Object[] tArray, | |
| 50 | + String sourcePath, String targetPath,String...NaN) { | |
| 51 | + try { | |
| 52 | + // 把源文件放入流中 | |
| 53 | + POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream( | |
| 54 | + sourcePath)); | |
| 55 | + HSSFWorkbook wb = new HSSFWorkbook(fs); | |
| 56 | + if(tArray.length != 0 && tArray[0] instanceof java.util.Map){ | |
| 57 | + Map<String, Object> m = (Map<String, Object>)tArray[0]; | |
| 58 | + if(m.containsKey("sheetName") && m.get("sheetName")!=null | |
| 59 | + && m.get("sheetName").toString().trim().length()!=0) | |
| 60 | + wb.setSheetName(0, m.get("sheetName").toString()); | |
| 61 | + } | |
| 62 | + HSSFSheet sheet = wb.getSheetAt(0); | |
| 63 | + HSSFRow row; | |
| 64 | + HSSFCell cell = null; | |
| 65 | + String key; | |
| 66 | + // 取得总行数 | |
| 67 | + int rowNum = sheet.getLastRowNum(); | |
| 68 | + // 取得总列数 | |
| 69 | + int cellNum = sheet.getRow(0).getLastCellNum(); | |
| 70 | + | |
| 71 | + // 遍历行 | |
| 72 | + for (int i = 0; i < rowNum; i++) { | |
| 73 | + row = sheet.getRow(i); | |
| 74 | + // 遍历列 | |
| 75 | + for (int j = 0; j < cellNum; j++) { | |
| 76 | + if (row == null) { | |
| 77 | + continue; | |
| 78 | + } | |
| 79 | + cell = row.getCell(j); | |
| 80 | + if (cell == null) { | |
| 81 | + continue; | |
| 82 | + } | |
| 83 | + // 取得每列的内容,如果列内容是$key$格式,则替换内容 | |
| 84 | + key = getCellValue(cell); | |
| 85 | + if (key != null && (key.indexOf("$") != -1 || key.indexOf("#list#") != -1)) { | |
| 86 | + // * 列中内容有#list#,则表示该行为模板行,需要以该行为模板 | |
| 87 | + // * 例如:模板行格式 #list#0_0 $Car.id$ | |
| 88 | + // * 第一个0表示需要在list中取iterator的索引值 | |
| 89 | + // * 第二个0表示在iterator中取的第几个对象,如果不为0,则取下一个对象,而不是沿用前面获取的对象 | |
| 90 | + // * $Car.id$表示所取的对象为Car的对象,并且取值为id的值 | |
| 91 | + if (key.indexOf("#list#") != -1) { | |
| 92 | + key = key.replace("#list#", "").trim(); | |
| 93 | + String[] lists = key.split(" "); | |
| 94 | + // 取得list中的索引值 | |
| 95 | + int listIndex = Integer | |
| 96 | + .valueOf(lists[0].split("_")[0]); | |
| 97 | + Iterator<?> iterator = list.get(listIndex); | |
| 98 | + // 根据模板创建行并填弃数据,返回增加的行数 | |
| 99 | + int rowCount = iteratorFillCellValue(wb, sheet, | |
| 100 | + cell, iterator, i, rowNum, key,NaN); | |
| 101 | + rowNum += rowCount; | |
| 102 | + i += rowCount; | |
| 103 | + break; | |
| 104 | + } else { | |
| 105 | + // 直接填充数据的列,从对象数组中取得值,这里的数组不传值 | |
| 106 | + getValueAndSetCellValue(cell, key, tArray, new String[]{""},NaN); | |
| 107 | + } | |
| 108 | + } | |
| 109 | + | |
| 110 | + } | |
| 111 | + } | |
| 112 | + // 创建目标文件夹 | |
| 113 | + createFolder(targetPath); | |
| 114 | + // 输出文件 | |
| 115 | + FileOutputStream fileOut = new FileOutputStream(targetPath); | |
| 116 | + wb.write(fileOut); | |
| 117 | + fileOut.close(); | |
| 118 | + } catch (Exception e) { | |
| 119 | + e.printStackTrace(); | |
| 120 | + } | |
| 121 | + } | |
| 122 | + | |
| 123 | + | |
| 124 | + /** | |
| 125 | + * | |
| 126 | + * @param sheetList 多sheet模板中,需要重复显示的行所需的数据 | |
| 127 | + * @param tArray | |
| 128 | + * @param sourcePath 模板路径 | |
| 129 | + * @param targetPath 生成路径 | |
| 130 | + * @param NaN 非数字参数(String[] 传入的key不做数字处理) | |
| 131 | + */ | |
| 132 | + public void excelMoreSheetReplace(List<List<Iterator<?>>> sheetList, Object[] tArray, | |
| 133 | + String sourcePath, String targetPath,String...NaN) { | |
| 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[]{""},NaN); | |
| 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 = getCellValue(row.getCell(j)); | |
| 265 | + switch(row.getCell(j).getCellType()) { | |
| 266 | + // String | |
| 267 | + case HSSFCell.CELL_TYPE_STRING: | |
| 268 | + rowCreat.getCell(j).setCellValue(row.getCell(j).getStringCellValue().toString()); | |
| 269 | + break; | |
| 270 | + // numeric类型,excel中,日期格式会转成数字格式存储 | |
| 271 | + case HSSFCell.CELL_TYPE_NUMERIC: | |
| 272 | + rowCreat.getCell(j).setCellValue(row.getCell(j).getNumericCellValue()); | |
| 273 | + break; | |
| 274 | + // 公式类型 | |
| 275 | + case HSSFCell.CELL_TYPE_FORMULA: | |
| 276 | + rowCreat.getCell(j).setCellValue(row.getCell(j).getCellFormula()); | |
| 277 | + break; | |
| 278 | + // boolean类型 | |
| 279 | + case HSSFCell.CELL_TYPE_BOOLEAN: | |
| 280 | + rowCreat.getCell(j).setCellValue(row.getCell(j).getBooleanCellValue()); | |
| 281 | + break; | |
| 282 | + // 空格 | |
| 283 | + case HSSFCell.CELL_TYPE_BLANK: | |
| 284 | + rowCreat.getCell(j).setCellValue(""); | |
| 285 | + break; | |
| 286 | + // 错误值 | |
| 287 | + case HSSFCell.CELL_TYPE_ERROR: | |
| 288 | + rowCreat.getCell(j).setCellValue(""); | |
| 289 | + break; | |
| 290 | + default : | |
| 291 | + rowCreat.getCell(j).setCellValue(""); | |
| 292 | + break; | |
| 293 | + } | |
| 294 | +// strVal = row.getCell(j).getStringCellValue(); | |
| 295 | +// rowCreat.getCell(j).setCellValue(strVal); | |
| 296 | + copyCellStyle(wb1, row.getCell(j).getCellStyle(), createCellStyle); | |
| 297 | + createCellStyle.setBorderTop((short)1); | |
| 298 | + createCellStyle.setBorderLeft((short)1); | |
| 299 | + createCellStyle.setBorderRight((short)1); | |
| 300 | + createCellStyle.setBorderBottom((short)1); | |
| 301 | + rowCreat.getCell(j).setCellStyle(createCellStyle); | |
| 302 | + } | |
| 303 | + } | |
| 304 | + } | |
| 305 | + | |
| 306 | +// int firstRowNum = createSheet.getFirstRowNum(); | |
| 307 | +// int lastRowNum = createSheet.getLastRowNum(); | |
| 308 | +// int test = 0; | |
| 309 | +// for(int k = firstRowNum; k <= lastRowNum; k++){ | |
| 310 | +// HSSFRow createRow = createSheet.getRow(k); | |
| 311 | +// int firstCellNum = createRow.getFirstCellNum(); | |
| 312 | +// int lastCellNum = createRow.getLastCellNum(); | |
| 313 | +// for(int i = firstCellNum; i < lastCellNum; i++){ | |
| 314 | +// HSSFCell cell = createRow.getCell(i); | |
| 315 | +// cell.getCellStyle().setBorderTop(HSSFCellStyle.BORDER_THIN); | |
| 316 | +// cell.getCellStyle().setBorderLeft(HSSFCellStyle.BORDER_THIN); | |
| 317 | +// cell.getCellStyle().setBorderRight(HSSFCellStyle.BORDER_THIN); | |
| 318 | +// cell.getCellStyle().setBorderBottom(HSSFCellStyle.BORDER_THIN); | |
| 319 | +// test ++; | |
| 320 | +// } | |
| 321 | +// } | |
| 322 | +// System.out.println("test = " + test); | |
| 323 | + | |
| 324 | + FileOutputStream fileOut = new FileOutputStream(file2); | |
| 325 | + wb2.write(fileOut); | |
| 326 | + fileOut.close(); | |
| 327 | + wb2.close(); | |
| 328 | + wb1.close(); | |
| 329 | + fs2.close(); | |
| 330 | + fs1.close(); | |
| 331 | + file1.delete(); | |
| 332 | +// // 创建目标文件夹 | |
| 333 | +// createFolder(targetPath); | |
| 334 | + // 输出文件 | |
| 335 | + } catch (Exception e) { | |
| 336 | + e.printStackTrace(); | |
| 337 | + } | |
| 338 | + } | |
| 339 | + | |
| 340 | + public void test(File file){ | |
| 341 | + POIFSFileSystem fs; | |
| 342 | + try { | |
| 343 | + fs = new POIFSFileSystem(new FileInputStream(file)); | |
| 344 | + HSSFWorkbook wb = new HSSFWorkbook(fs); | |
| 345 | + for(int j = 0; j < wb.getNumberOfSheets(); j++){ | |
| 346 | + HSSFSheet sheet = wb.getSheetAt(j); | |
| 347 | + int firstRowNum = sheet.getFirstRowNum(); | |
| 348 | + int lastRowNum = sheet.getLastRowNum(); | |
| 349 | + int test = 0; | |
| 350 | + for(int k = firstRowNum; k <= lastRowNum; k++){ | |
| 351 | + HSSFRow createRow = sheet.getRow(k); | |
| 352 | + int firstCellNum = createRow.getFirstCellNum(); | |
| 353 | + int lastCellNum = createRow.getLastCellNum(); | |
| 354 | + for(int i = firstCellNum; i < lastCellNum; i++){ | |
| 355 | + HSSFCell cell = createRow.getCell(i); | |
| 356 | + HSSFCellStyle cellStyle = wb.createCellStyle(); | |
| 357 | + | |
| 358 | + cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); | |
| 359 | + cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); | |
| 360 | + cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); | |
| 361 | + cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); | |
| 362 | + cell.setCellStyle(cellStyle); | |
| 363 | + test ++; | |
| 364 | + } | |
| 365 | + } | |
| 366 | + System.out.println("test = " + test); | |
| 367 | + | |
| 368 | + FileOutputStream fileOut = new FileOutputStream(file); | |
| 369 | + wb.write(fileOut); | |
| 370 | + fileOut.close(); | |
| 371 | + } | |
| 372 | + } catch (Exception e) { | |
| 373 | + // TODO Auto-generated catch block | |
| 374 | + e.printStackTrace(); | |
| 375 | + } | |
| 376 | + | |
| 377 | + } | |
| 378 | + | |
| 379 | + public String getCellValue(HSSFCell cell) { | |
| 380 | + int cellType = 0; | |
| 381 | + String result = ""; | |
| 382 | + double d; | |
| 383 | + if(cell != null) { | |
| 384 | + // 获取列数据类型 | |
| 385 | + cellType = cell.getCellType(); | |
| 386 | + // 不同列数据类型,取值方法不同 | |
| 387 | + switch(cellType) { | |
| 388 | + // String | |
| 389 | + case HSSFCell.CELL_TYPE_STRING: | |
| 390 | + result = cell.getStringCellValue().toString(); | |
| 391 | + break; | |
| 392 | + // numeric类型,excel中,日期格式会转成数字格式存储 | |
| 393 | + case HSSFCell.CELL_TYPE_NUMERIC: | |
| 394 | + result = cell.getNumericCellValue()+""; | |
| 395 | + break; | |
| 396 | + // 公式类型 | |
| 397 | + case HSSFCell.CELL_TYPE_FORMULA: | |
| 398 | + result = cell.getCellFormula() + ""; | |
| 399 | + break; | |
| 400 | + // boolean类型 | |
| 401 | + case HSSFCell.CELL_TYPE_BOOLEAN: | |
| 402 | + result = cell.getBooleanCellValue() + ""; | |
| 403 | + break; | |
| 404 | + // 空格 | |
| 405 | + case HSSFCell.CELL_TYPE_BLANK: | |
| 406 | + result = null; | |
| 407 | + break; | |
| 408 | + // 错误值 | |
| 409 | + case HSSFCell.CELL_TYPE_ERROR: | |
| 410 | + result = null; | |
| 411 | + break; | |
| 412 | + default : | |
| 413 | + System.out.println("其它"); | |
| 414 | + break; | |
| 415 | + } | |
| 416 | + } | |
| 417 | + return result; | |
| 418 | + } | |
| 419 | + | |
| 420 | + /** | |
| 421 | + * 根据iterator,以及模板中的标识,填充模板 | |
| 422 | + * | |
| 423 | + * @param wb | |
| 424 | + * @param sheet | |
| 425 | + * @param cell | |
| 426 | + * @param iterator | |
| 427 | + * iterator | |
| 428 | + * @param index | |
| 429 | + * 模板行索引 | |
| 430 | + * @param rowNum | |
| 431 | + * 表格总行数 | |
| 432 | + * @param key | |
| 433 | + * 表格内容 | |
| 434 | + * @param NaN 非数字参数(String[] 传入的key不做数字处理) | |
| 435 | + * @return | |
| 436 | + */ | |
| 437 | + private int iteratorFillCellValue(HSSFWorkbook wb, HSSFSheet sheet, | |
| 438 | + HSSFCell cell, Iterator<?> iterator, int index, int rowNum, | |
| 439 | + String key,String...NaN) { | |
| 440 | + int rowCount = 0; | |
| 441 | + Object obj = null; | |
| 442 | + int p = 0; | |
| 443 | + int i = index; | |
| 444 | + HSSFRow newRow = null; | |
| 445 | + int tmpCellNum = 0; | |
| 446 | + int k = 0; | |
| 447 | + int listIndex = 0; | |
| 448 | + // 取得模板行 | |
| 449 | + HSSFRow orgRow = sheet.getRow(index); | |
| 450 | + HSSFCellStyle style= wb.createCellStyle(); | |
| 451 | + try { | |
| 452 | + while (iterator.hasNext()) { | |
| 453 | + // 取得iterator的对象 | |
| 454 | + obj = iterator.next(); | |
| 455 | + // 移动当前编辑行以下的所有行 | |
| 456 | + if (p != 0) { | |
| 457 | + rowNum += 1; | |
| 458 | + i += 1; | |
| 459 | + rowCount += 1;// 增加的总行数 | |
| 460 | + // 把当前行以下的所有行往下移动1行 | |
| 461 | + sheet.shiftRows(i, rowNum, 1); | |
| 462 | + } | |
| 463 | + p = 1; | |
| 464 | + // 创建新行 | |
| 465 | + newRow = sheet.createRow(index + k++); | |
| 466 | + // 把新行的内容换成和模板行一样 | |
| 467 | + copyRow(wb, orgRow, newRow, true,style); | |
| 468 | + tmpCellNum = newRow.getLastCellNum(); | |
| 469 | + for (int l = 0; l < tmpCellNum; l++) { | |
| 470 | + cell = newRow.getCell(l); | |
| 471 | + key = getCellValue(cell); | |
| 472 | + /** | |
| 473 | + * 如果单无格内容为#list#,表示该行是模板行 #list#0_0 | |
| 474 | + * 第一个0表示需要在list中取iterator的索引值 | |
| 475 | + * 第二个0表示在iterator中取的第几个对象,如果不为0,则取下一个对象,而不是沿用前面获取的对象 | |
| 476 | + */ | |
| 477 | + if(key == null || key.equals("")){ | |
| 478 | + obj = iterator.next(); | |
| 479 | + } | |
| 480 | + if (key.indexOf("#list#") != -1 && key.indexOf("_0") == -1) { | |
| 481 | + if (iterator.hasNext()) { | |
| 482 | + obj = iterator.next(); | |
| 483 | + } else { | |
| 484 | + obj = null; | |
| 485 | + } | |
| 486 | + } | |
| 487 | + if (key.trim().indexOf(" ") != -1) { | |
| 488 | + key = key.split(" ")[1]; | |
| 489 | + } | |
| 490 | + getValueAndSetCellValue(cell, key, obj,new String[]{listIndex+""},NaN); | |
| 491 | + } | |
| 492 | + // list的数量 | |
| 493 | + listIndex ++; | |
| 494 | + } | |
| 495 | + } catch (Exception e) { | |
| 496 | + e.printStackTrace(); | |
| 497 | + } | |
| 498 | + return rowCount; | |
| 499 | + } | |
| 500 | + | |
| 501 | + /** | |
| 502 | + * 取到相应的值并填入相应的列中 | |
| 503 | + * | |
| 504 | + * @param cell 列 | |
| 505 | + * @param key 列内容 | |
| 506 | + * @param obj 数据源对象 | |
| 507 | + * @param args 其他参数 数组 | |
| 508 | + * @param NaN 非数字参数(String[] 传入的key不做数字处理) | |
| 509 | + * 数组内容: | |
| 510 | + * 0位:在list中的第几条数据 | |
| 511 | + */ | |
| 512 | + private void getValueAndSetCellValue(HSSFCell cell, String key, Object obj, String[] args,String...NaN) { | |
| 513 | + try { | |
| 514 | + // 保有存单元格的内容 | |
| 515 | + String cellValue = key = key.replace("\\n", ""); | |
| 516 | + String tmpKey; | |
| 517 | + String fieldName = null; | |
| 518 | + // 判断单元格内容是否是公式 | |
| 519 | + if(cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA){ | |
| 520 | + Pattern p=Pattern.compile("(\\d+)"); // | |
| 521 | + Matcher m=p.matcher(key); | |
| 522 | + if(m.find()){ | |
| 523 | + cellValue = key.replace(m.group(1), | |
| 524 | + Integer.valueOf(m.group(1))+Integer.valueOf(args[0])+""); | |
| 525 | + cell.setCellFormula(cellValue); | |
| 526 | + return; | |
| 527 | + } | |
| 528 | + }else{//其他格式 | |
| 529 | + | |
| 530 | + // 循环截取两个$中间的内容,反射出值 | |
| 531 | + while (key.indexOf("$") != -1) { | |
| 532 | + key = key.substring(key.indexOf("$") + 1); | |
| 533 | + // 取两个$中间的内容 | |
| 534 | + tmpKey = key.substring(0, key.indexOf("$")); | |
| 535 | + key = key.substring(key.indexOf("$") + 1); | |
| 536 | + // 如果内容是如下格式Cars.id,则从obj中值得相应的对象的值 | |
| 537 | + if (tmpKey.indexOf(".") != -1) { | |
| 538 | + String className = tmpKey.substring(0, tmpKey.indexOf(".")); | |
| 539 | + // 取得类的全限定名 | |
| 540 | + String classWholeName = packaegName + className; | |
| 541 | + fieldName = tmpKey.substring(tmpKey.indexOf(".") + 1); | |
| 542 | + // 如果obj是数组,循环判断哪个对象是对应的 | |
| 543 | + if (obj instanceof Object[]) { | |
| 544 | + Object[] objs = (Object[]) obj; | |
| 545 | + for (int k = 0; k < objs.length; k++) { | |
| 546 | + if (objs[k].getClass().getName().equals(classWholeName)) { | |
| 547 | + cellValue = cellValue.replace("$" + tmpKey | |
| 548 | + + "$", getKeyValue(objs[k], fieldName) | |
| 549 | + + ""); | |
| 550 | + } else if(objs[k].getClass().getName().equals("java.util.HashMap")){ | |
| 551 | + Map<String,Object> map = (HashMap<String,Object>)objs[k]; | |
| 552 | + cellValue = cellValue.replace("$" + tmpKey + "$",map.get(fieldName)+""); | |
| 553 | + } | |
| 554 | + } | |
| 555 | + } else if (obj.getClass().getName().equals(classWholeName)) { | |
| 556 | + cellValue = cellValue.replace("$" + tmpKey + "$",getKeyValue(obj, fieldName) + ""); | |
| 557 | + } else if (obj instanceof Map){ | |
| 558 | + Map<String,Object> map = (Map<String, Object>) obj; | |
| 559 | + cellValue = cellValue.replace("$" + tmpKey + "$",map.get(fieldName)+""); | |
| 560 | + } | |
| 561 | + } | |
| 562 | + } | |
| 563 | + } | |
| 564 | + cell.setCellValue(cellValue); | |
| 565 | + Matcher matcher = pattern.matcher(cellValue); | |
| 566 | + if (matcher.matches() && !Arrays.asList(NaN).contains(fieldName)) { | |
| 567 | + if (cellValue.indexOf(".") > -1) { | |
| 568 | + cell.setCellValue(Double.parseDouble(cellValue)); | |
| 569 | + } else { | |
| 570 | + cell.setCellValue(Integer.parseInt(cellValue)); | |
| 571 | + } | |
| 572 | + } | |
| 573 | + } catch (Exception e) { | |
| 574 | + e.printStackTrace(); | |
| 575 | + } | |
| 576 | + | |
| 577 | + } | |
| 578 | + | |
| 579 | + /** | |
| 580 | + * 给列填充数据 | |
| 581 | + * | |
| 582 | + * @param cell | |
| 583 | + * 列 | |
| 584 | + * @param obj | |
| 585 | + * 数据源对象 | |
| 586 | + * @param fieldName | |
| 587 | + * 需要取数据的字段 | |
| 588 | + */ | |
| 589 | + private Object getKeyValue(Object obj, String fieldName) { | |
| 590 | + Object value = ""; | |
| 591 | + try { | |
| 592 | + if (obj != null) { | |
| 593 | + ReportRelatedUtils test = new ReportRelatedUtils(); | |
| 594 | + value = test.getValue(obj, fieldName) == null ? "" : test .getValue(obj, fieldName); | |
| 595 | + } | |
| 596 | + } catch (Exception e) { | |
| 597 | + e.printStackTrace(); | |
| 598 | + } | |
| 599 | + return value; | |
| 600 | + } | |
| 601 | + | |
| 602 | + public static void main(String[] args) { | |
| 603 | + | |
| 604 | + try { | |
| 605 | + ReportUtils ee = new ReportUtils(); | |
| 606 | + List<Iterator<?>> list = new ArrayList<Iterator<?>>(); | |
| 607 | + Line line = new Line(); | |
| 608 | + line.setId(1); | |
| 609 | + line.setName("line1"); | |
| 610 | + | |
| 611 | + List<Object> dataList = new ArrayList<Object>(); | |
| 612 | + | |
| 613 | + | |
| 614 | + ScheduleRealInfo srr = new ScheduleRealInfo(); | |
| 615 | + srr.setId((long) 111); | |
| 616 | + srr.setXlName("abc11"); | |
| 617 | + | |
| 618 | + ScheduleRealInfo sr = new ScheduleRealInfo(); | |
| 619 | + sr.setZdsj("06:10"); | |
| 620 | + sr.setZdsjActual("06:25"); | |
| 621 | + dataList.add(sr); | |
| 622 | + sr = new ScheduleRealInfo(); | |
| 623 | + sr.setZdsj("06:20"); | |
| 624 | + sr.setZdsjActual(""); | |
| 625 | + dataList.add(sr); | |
| 626 | + list.add(dataList.iterator()); | |
| 627 | + | |
| 628 | + ee.excelReplace(list, new Object[] { srr }, "D:/waybill.xls", | |
| 629 | + "D:/22.xls"); | |
| 630 | + System.out.println("ok"); | |
| 631 | + } catch (Exception e) { | |
| 632 | + e.printStackTrace(); | |
| 633 | + } | |
| 634 | + } | |
| 635 | + | |
| 636 | + /** | |
| 637 | + * 行复制功能 | |
| 638 | + * | |
| 639 | + * @param fromRow | |
| 640 | + * @param toRow | |
| 641 | + */ | |
| 642 | + private void copyRow(HSSFWorkbook wb, HSSFRow fromRow, HSSFRow toRow, | |
| 643 | + boolean copyValueFlag, HSSFCellStyle style) { | |
| 644 | + for (Iterator<Cell> cellIt = fromRow.cellIterator(); cellIt.hasNext();) { | |
| 645 | + HSSFCell tmpCell = (HSSFCell) cellIt.next(); | |
| 646 | + HSSFCell newCell = toRow.createCell(tmpCell.getColumnIndex(), 0); | |
| 647 | + copyCell(wb, tmpCell, newCell, copyValueFlag, tmpCell.getCellStyle()); | |
| 648 | + } | |
| 649 | + } | |
| 650 | + | |
| 651 | + /** | |
| 652 | + * 复制单元格 | |
| 653 | + * | |
| 654 | + * @param srcCell | |
| 655 | + * @param distCell | |
| 656 | + * @param copyValueFlag | |
| 657 | + * true则连同cell的内容一起复制 | |
| 658 | + */ | |
| 659 | + public void copyCell(HSSFWorkbook wb, HSSFCell srcCell, HSSFCell distCell, | |
| 660 | + boolean copyValueFlag, HSSFCellStyle newstyle) { | |
| 661 | +// HSSFCellStyle newstyle = wb.createCellStyle(); | |
| 662 | + copyCellStyle(wb, srcCell.getCellStyle(), newstyle); | |
| 663 | + // 样式 | |
| 664 | + distCell.setCellStyle(newstyle); | |
| 665 | + // 评论 | |
| 666 | + if (srcCell.getCellComment() != null) { | |
| 667 | + distCell.setCellComment(srcCell.getCellComment()); | |
| 668 | + } | |
| 669 | + // 不同数据类型处理 | |
| 670 | + int srcCellType = srcCell.getCellType(); | |
| 671 | + distCell.setCellType(srcCellType); | |
| 672 | + if (copyValueFlag) { | |
| 673 | + if (srcCellType == HSSFCell.CELL_TYPE_NUMERIC) { | |
| 674 | + distCell.setCellValue(srcCell.getDateCellValue()); | |
| 675 | + } else if (srcCellType == HSSFCell.CELL_TYPE_STRING) { | |
| 676 | + distCell.setCellValue(srcCell.getRichStringCellValue()); | |
| 677 | + } else if (srcCellType == HSSFCell.CELL_TYPE_BLANK) { | |
| 678 | + | |
| 679 | + } else if (srcCellType == HSSFCell.CELL_TYPE_BOOLEAN) { | |
| 680 | + distCell.setCellValue(srcCell.getBooleanCellValue()); | |
| 681 | + } else if (srcCellType == HSSFCell.CELL_TYPE_ERROR) { | |
| 682 | + distCell.setCellErrorValue(srcCell.getErrorCellValue()); | |
| 683 | + } else if (srcCellType == HSSFCell.CELL_TYPE_FORMULA) { | |
| 684 | + distCell.setCellFormula(srcCell.getCellFormula()); | |
| 685 | + } else { | |
| 686 | + } | |
| 687 | + } | |
| 688 | + } | |
| 689 | + | |
| 690 | + /** | |
| 691 | + * 复制一个单元格样式到目的单元格样式 | |
| 692 | + * | |
| 693 | + * @param fromStyle | |
| 694 | + * @param toStyle | |
| 695 | + */ | |
| 696 | + public void copyCellStyle(HSSFWorkbook wb, HSSFCellStyle fromStyle, | |
| 697 | + HSSFCellStyle toStyle) { | |
| 698 | + toStyle.setAlignment(fromStyle.getAlignment()); | |
| 699 | + // 边框和边框颜色 | |
| 700 | + toStyle.setBorderBottom(fromStyle.getBorderBottom()); | |
| 701 | + toStyle.setBorderLeft(fromStyle.getBorderLeft()); | |
| 702 | + toStyle.setBorderRight(fromStyle.getBorderRight()); | |
| 703 | + toStyle.setBorderTop(fromStyle.getBorderTop()); | |
| 704 | + toStyle.setTopBorderColor(fromStyle.getTopBorderColor()); | |
| 705 | + toStyle.setBottomBorderColor(fromStyle.getBottomBorderColor()); | |
| 706 | + toStyle.setRightBorderColor(fromStyle.getRightBorderColor()); | |
| 707 | + toStyle.setLeftBorderColor(fromStyle.getLeftBorderColor()); | |
| 708 | + | |
| 709 | + // 背景和前景 | |
| 710 | + toStyle.setFillBackgroundColor(fromStyle.getFillBackgroundColor()); | |
| 711 | + toStyle.setFillForegroundColor(fromStyle.getFillForegroundColor()); | |
| 712 | + | |
| 713 | + toStyle.setDataFormat(fromStyle.getDataFormat()); | |
| 714 | + toStyle.setFillPattern(fromStyle.getFillPattern()); | |
| 715 | + toStyle.setHidden(fromStyle.getHidden()); | |
| 716 | + toStyle.setIndention(fromStyle.getIndention());// 首行缩进 | |
| 717 | + toStyle.setLocked(fromStyle.getLocked()); | |
| 718 | + toStyle.setRotation(fromStyle.getRotation());// 旋转 | |
| 719 | + toStyle.setVerticalAlignment(fromStyle.getVerticalAlignment()); | |
| 720 | + toStyle.setWrapText(fromStyle.getWrapText()); | |
| 721 | + // 字体 | |
| 722 | + toStyle.setFont(fromStyle.getFont(wb)); | |
| 723 | + | |
| 724 | + } | |
| 725 | + | |
| 726 | + /** | |
| 727 | + * 创建文件夹,并删除原有文件 | |
| 728 | + * | |
| 729 | + * @param path | |
| 730 | + */ | |
| 731 | + private void createFolder(String path) { | |
| 732 | + File targetFile = null; | |
| 733 | + targetFile = new File(path); | |
| 734 | + if (targetFile.exists()) {// 删除原有文件 | |
| 735 | + targetFile.delete(); | |
| 736 | + } | |
| 737 | + // 创建目标文件夹 | |
| 738 | + targetFile = new File(path.substring(0, path.lastIndexOf("/"))); | |
| 739 | + if (!targetFile.exists()) { | |
| 740 | + targetFile.mkdirs(); | |
| 741 | + } | |
| 742 | + } | |
| 743 | + | |
| 744 | + public void createFlie(List<List<String>> list, String name, String type){ | |
| 745 | + HSSFWorkbook workbook = new HSSFWorkbook(); | |
| 746 | + // 生成一个样式 | |
| 747 | + HSSFCellStyle style = workbook.createCellStyle(); | |
| 748 | + // 设置这些样式 | |
| 749 | +// style.setFillForegroundColor(HSSFColor.SKY_BLUE.index); | |
| 750 | +// style.setFillPattern(HSSFCellStyle.BORDER_THIN); | |
| 751 | + style.setBorderBottom(HSSFCellStyle.BORDER_THIN); | |
| 752 | + style.setBorderLeft(HSSFCellStyle.BORDER_THIN); | |
| 753 | + style.setBorderRight(HSSFCellStyle.BORDER_THIN); | |
| 754 | + style.setBorderTop(HSSFCellStyle.BORDER_THIN); | |
| 755 | + style.setAlignment(HSSFCellStyle.ALIGN_CENTER); | |
| 756 | + // 生成一个字体 | |
| 757 | + HSSFFont font = workbook.createFont(); | |
| 758 | +// font.setColor(HSSFColor.VIOLET.index); | |
| 759 | + font.setFontHeightInPoints((short) 12); | |
| 760 | + font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); | |
| 761 | + // 把字体应用到当前的样式 | |
| 762 | + style.setFont(font); | |
| 763 | + HSSFCellStyle cellStyle =workbook.createCellStyle(); | |
| 764 | + cellStyle.setFont(font); | |
| 765 | + cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); //水平布局:居中 | |
| 766 | + cellStyle.setWrapText(true); | |
| 767 | + | |
| 768 | + //设置wordsheet名 | |
| 769 | + HSSFSheet sheetYS = workbook.createSheet();//设置wordsheet名 | |
| 770 | + HSSFRow row = sheetYS.createRow(0); | |
| 771 | + setCellStyleAndValue(row, style, 0, name); | |
| 772 | + CellRangeAddress callRangeAddress = new CellRangeAddress(0,0,0,list.get(0).size()-1); | |
| 773 | + sheetYS.addMergedRegion(callRangeAddress); | |
| 774 | + // 样式 | |
| 775 | + setMergeCellStyle (callRangeAddress, sheetYS, workbook); | |
| 776 | + | |
| 777 | + try{ | |
| 778 | + for(int i=0; i<list.size(); i++){ | |
| 779 | + HSSFRow rowYSi = sheetYS.createRow(i+1); | |
| 780 | + List<String> stringList = list.get(i); | |
| 781 | + int num = 4; | |
| 782 | + if("xl".equals(type)) | |
| 783 | + num = 2; | |
| 784 | + else if("cl".equals(type)) | |
| 785 | + num = 5; | |
| 786 | + for(int j=0; j<stringList.size(); j++){ | |
| 787 | + String str = stringList.get(j); | |
| 788 | + if(i == list.size()-1){ | |
| 789 | + if(j==0) { | |
| 790 | + setCellStyleAndValue(rowYSi, style, j, str); | |
| 791 | + CellRangeAddress callRangeAddressYSi = new CellRangeAddress(i+1,i+1,0,num); | |
| 792 | + sheetYS.addMergedRegion(callRangeAddressYSi); | |
| 793 | + // 样式 | |
| 794 | + setMergeCellStyle (callRangeAddressYSi, sheetYS, workbook); | |
| 795 | + }else | |
| 796 | + setCellStyleAndValue(rowYSi,style,j+num,str); | |
| 797 | + } else { | |
| 798 | + setCellStyleAndValue(rowYSi,style,j,str); | |
| 799 | + } | |
| 800 | + } | |
| 801 | + } | |
| 802 | + | |
| 803 | + // 给列设置宽度自适应 | |
| 804 | + setSizeColumn1(sheetYS,1,list.get(0).size()); | |
| 805 | + String path = this.getClass().getResource("/").getPath() + "static/pages/forms/export/"; | |
| 806 | + String targetPath = path+name+".xls"; | |
| 807 | + createFolder(targetPath); | |
| 808 | + FileOutputStream fout = new FileOutputStream(targetPath); | |
| 809 | + //5.输出 | |
| 810 | + workbook.write(fout); | |
| 811 | + fout.close(); | |
| 812 | + } catch (Exception e) { | |
| 813 | + e.printStackTrace(); | |
| 814 | + } | |
| 815 | + } | |
| 816 | + | |
| 817 | + /** | |
| 818 | + * 自适应宽度(中文支持) | |
| 819 | + * @param sheet | |
| 820 | + * @param size | |
| 821 | + */ | |
| 822 | + private static void setSizeColumn(HSSFSheet sheet, int size) { | |
| 823 | + for (int columnNum = 0; columnNum < size; columnNum++) { | |
| 824 | + int columnWidth = sheet.getColumnWidth(columnNum) / 256; | |
| 825 | + for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) { | |
| 826 | + HSSFRow currentRow; | |
| 827 | + //当前行未被使用过 | |
| 828 | + if (sheet.getRow(rowNum) == null) { | |
| 829 | + currentRow = sheet.createRow(rowNum); | |
| 830 | + } else { | |
| 831 | + currentRow = sheet.getRow(rowNum); | |
| 832 | + } | |
| 833 | + if (currentRow.getCell(columnNum) != null) { | |
| 834 | + HSSFCell currentCell = currentRow.getCell(columnNum); | |
| 835 | + if (currentCell.getCellType() == XSSFCell.CELL_TYPE_STRING) { | |
| 836 | + int length = currentCell.getStringCellValue().getBytes().length; | |
| 837 | + if (columnWidth < length) { | |
| 838 | + columnWidth = length; | |
| 839 | + } | |
| 840 | + } | |
| 841 | + } | |
| 842 | + } | |
| 843 | + sheet.setColumnWidth(columnNum, columnWidth * 300); | |
| 844 | +// sheet.setColumnWidth(columnNum, columnWidth * 256); | |
| 845 | + } | |
| 846 | + } | |
| 847 | + | |
| 848 | + /** | |
| 849 | + * 自适应宽度(中文支持) | |
| 850 | + * @param sheet | |
| 851 | + * @param index 从那一行开始自适应 | |
| 852 | + * @param size | |
| 853 | + */ | |
| 854 | + private static void setSizeColumn1(HSSFSheet sheet,int index, int size) { | |
| 855 | + for (int columnNum = index; columnNum < size; columnNum++) { | |
| 856 | + int columnWidth = sheet.getColumnWidth(columnNum) / 256; | |
| 857 | + for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) { | |
| 858 | + HSSFRow currentRow; | |
| 859 | + //当前行未被使用过 | |
| 860 | + if (sheet.getRow(rowNum) == null) { | |
| 861 | + currentRow = sheet.createRow(rowNum); | |
| 862 | + } else { | |
| 863 | + currentRow = sheet.getRow(rowNum); | |
| 864 | + } | |
| 865 | + if (currentRow.getCell(columnNum) != null) { | |
| 866 | + HSSFCell currentCell = currentRow.getCell(columnNum); | |
| 867 | + if (currentCell.getCellType() == XSSFCell.CELL_TYPE_STRING) { | |
| 868 | + int length = currentCell.getStringCellValue().getBytes().length; | |
| 869 | + if (columnWidth < length) { | |
| 870 | + columnWidth = length; | |
| 871 | + } | |
| 872 | + } | |
| 873 | + } | |
| 874 | + } | |
| 875 | + sheet.setColumnWidth(columnNum, columnWidth * 300); | |
| 876 | +// sheet.setColumnWidth(columnNum, columnWidth * 256); | |
| 877 | + } | |
| 878 | + } | |
| 879 | + | |
| 880 | + /** | |
| 881 | + * 设置单元格值和样式 | |
| 882 | + * @param row | |
| 883 | + * @param style | |
| 884 | + * @param index | |
| 885 | + * @param value | |
| 886 | + */ | |
| 887 | + public static void setCellStyleAndValue(HSSFRow row,HSSFCellStyle style,int index,String value){ | |
| 888 | + HSSFCell cell = row.createCell(index); | |
| 889 | + cell.setCellValue(value); | |
| 890 | + cell.setCellStyle(style); | |
| 891 | + } | |
| 892 | + /** | |
| 893 | + * 设置合并单元格样式 | |
| 894 | + * @param cra | |
| 895 | + * @param sheet | |
| 896 | + * @param workbook | |
| 897 | + */ | |
| 898 | + public static void setMergeCellStyle (CellRangeAddress cra, HSSFSheet sheet, Workbook workbook){ | |
| 899 | + // 使用RegionUtil类为合并后的单元格添加边框 | |
| 900 | + RegionUtil.setBorderBottom(1, cra, sheet, workbook); // 下边框 | |
| 901 | + RegionUtil.setBorderLeft(1, cra, sheet, workbook); // 左边框 | |
| 902 | + RegionUtil.setBorderRight(1, cra, sheet, workbook); // 有边框 | |
| 903 | + RegionUtil.setBorderTop(1, cra, sheet, workbook); // 上边框 | |
| 904 | + } | |
| 905 | +} | ... | ... |
src/main/resources/application-cloud.properties
| ... | ... | @@ -12,24 +12,41 @@ spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true |
| 12 | 12 | spring.jpa.show-sql= false |
| 13 | 13 | spring.jpa.properties.hibernate.dialect= org.hibernate.spatial.dialect.mysql.MySQL56InnoDBSpatialDialect |
| 14 | 14 | |
| 15 | -#DATABASE | |
| 16 | -spring.datasource.driver-class-name= com.mysql.jdbc.Driver | |
| 17 | -spring.datasource.url= jdbc:mysql://10.10.2.20/control_dy?useUnicode=true&characterEncoding=utf-8&useSSL=false | |
| 18 | -spring.datasource.username= root | |
| 19 | -spring.datasource.password= root2jsp | |
| 20 | -spring.datasource.type= com.zaxxer.hikari.HikariDataSource | |
| 21 | - | |
| 22 | -#DATASOURCE SETTING | |
| 23 | -spring.datasource.hikari.minimum-idle= 8 | |
| 24 | -spring.datasource.hikari.maximum-pool-size= 100 | |
| 25 | -#spring.datasource.hikari.auto-commit= true | |
| 26 | -spring.datasource.hikari.idle-timeout= 60000 | |
| 27 | -#spring.datasource.hikari.pool-name= HikariPool | |
| 28 | -spring.datasource.hikari.max-lifetime= 1800000 | |
| 29 | -spring.datasource.hikari.connection-timeout= 3000 | |
| 30 | -spring.datasource.hikari.connection-test-query= SELECT 1 | |
| 31 | -spring.datasource.hikari.validation-timeout= 3000 | |
| 32 | -spring.datasource.hikari.register-mbeans=true | |
| 15 | +#DATABASE control | |
| 16 | +spring.datasource.control.driver-class-name= com.mysql.jdbc.Driver | |
| 17 | +spring.datasource.control.jdbc-url= jdbc:mysql://10.10.2.20/control_dy?useUnicode=true&characterEncoding=utf-8&useSSL=false | |
| 18 | +spring.datasource.control.username= root | |
| 19 | +spring.datasource.control.password= root2jsp | |
| 20 | +spring.datasource.control.type= com.zaxxer.hikari.HikariDataSource | |
| 21 | +#DATASOURCE | |
| 22 | +spring.datasource.control.hikari.minimum-idle= 8 | |
| 23 | +spring.datasource.control.hikari.maximum-pool-size= 100 | |
| 24 | +#spring.datasource.control.hikari.auto-commit= true | |
| 25 | +spring.datasource.control.hikari.idle-timeout= 60000 | |
| 26 | +#spring.datasource.control.hikari.pool-name= HikariPool | |
| 27 | +spring.datasource.control.hikari.max-lifetime= 1800000 | |
| 28 | +spring.datasource.control.hikari.connection-timeout= 3000 | |
| 29 | +spring.datasource.control.hikari.connection-test-query= SELECT 1 | |
| 30 | +spring.datasource.control.hikari.validation-timeout= 3000 | |
| 31 | +spring.datasource.control.hikari.register-mbeans=true | |
| 32 | + | |
| 33 | +#DATABASE info-publish | |
| 34 | +spring.datasource.info-publish.driver-class-name= com.mysql.jdbc.Driver | |
| 35 | +spring.datasource.info-publish.jdbc-url= jdbc:mysql://10.10.2.20/info_publish?rewriteBatchedStatements=true&useUnicode=true&characterEncoding=utf-8&useSSL=false | |
| 36 | +spring.datasource.info-publish.username= root | |
| 37 | +spring.datasource.info-publish.password= root2jsp | |
| 38 | +spring.datasource.info-publish.type= com.zaxxer.hikari.HikariDataSource | |
| 39 | +#DATASOURCE | |
| 40 | +spring.datasource.info-publish.hikari.minimum-idle= 8 | |
| 41 | +spring.datasource.info-publish.hikari.maximum-pool-size= 100 | |
| 42 | +#spring.datasource.info-publish.hikari.auto-commit= true | |
| 43 | +spring.datasource.info-publish.hikari.idle-timeout= 60000 | |
| 44 | +#spring.datasource.info-publish.hikari.pool-name= HikariPool | |
| 45 | +spring.datasource.info-publish.hikari.max-lifetime= 1800000 | |
| 46 | +spring.datasource.info-publish.hikari.connection-timeout= 3000 | |
| 47 | +spring.datasource.info-publish.hikari.connection-test-query= SELECT 1 | |
| 48 | +spring.datasource.info-publish.hikari.validation-timeout= 3000 | |
| 49 | +spring.datasource.info-publish.hikari.register-mbeans=true | |
| 33 | 50 | |
| 34 | 51 | kafka.use= false |
| 35 | 52 | spring.kafka.consumer.bootstrap-servers= 127.0.0.1:9092 |
| ... | ... | @@ -47,7 +64,7 @@ spring.rabbitmq.username= bsth |
| 47 | 64 | spring.rabbitmq.password= bsth001 |
| 48 | 65 | spring.rabbitmq.virtual-host= /dsm |
| 49 | 66 | |
| 50 | -minio.url= http://10.10.2.21:9008 | |
| 67 | +minio.url= http://118.113.164.50:9008 | |
| 51 | 68 | minio.accessKey= umupRhEzO9EiVhLnJ7b9 |
| 52 | 69 | minio.secretKey= VzjQUTN6c8p70HyX8AryRrB6ZjEsF2K28ndctHQh |
| 53 | 70 | minio.bucket= dsm | ... | ... |
src/main/resources/application-dev.properties
| ... | ... | @@ -12,31 +12,41 @@ spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true |
| 12 | 12 | spring.jpa.show-sql= true |
| 13 | 13 | spring.jpa.properties.hibernate.dialect= org.hibernate.spatial.dialect.mysql.MySQL56InnoDBSpatialDialect |
| 14 | 14 | |
| 15 | -#DATABASE | |
| 16 | -spring.datasource.driver-class-name= com.mysql.jdbc.Driver | |
| 17 | -#spring.datasource.url= jdbc:mysql://127.0.0.1/control?useUnicode=true&characterEncoding=utf-8&useSSL=false | |
| 18 | -##spring.datasource.url= jdbc:mysql://192.168.168.222/control?useUnicode=true&characterEncoding=utf-8&useSSL=false | |
| 19 | -#spring.datasource.username= root | |
| 20 | -#spring.datasource.password= | |
| 21 | -spring.datasource.url= jdbc:mysql://192.168.168.152/control_dy?useUnicode=true&characterEncoding=utf-8&useSSL=false | |
| 22 | -spring.datasource.username= root | |
| 23 | -spring.datasource.password= root2jsp | |
| 24 | -#spring.datasource.url= jdbc:mysql://192.168.168.241/control?useUnicode=true&characterEncoding=utf-8&useSSL=false | |
| 25 | -#spring.datasource.username= root | |
| 26 | -#spring.datasource.password= root2jsp | |
| 27 | -spring.datasource.type= com.zaxxer.hikari.HikariDataSource | |
| 15 | +#DATABASE control | |
| 16 | +spring.datasource.control.driver-class-name= com.mysql.jdbc.Driver | |
| 17 | +spring.datasource.control.jdbc-url= jdbc:mysql://192.168.168.152/control_dy?useUnicode=true&characterEncoding=utf-8&useSSL=false | |
| 18 | +spring.datasource.control.username= root | |
| 19 | +spring.datasource.control.password= root2jsp | |
| 20 | +spring.datasource.control.type= com.zaxxer.hikari.HikariDataSource | |
| 21 | +#DATASOURCE | |
| 22 | +spring.datasource.control.hikari.minimum-idle= 8 | |
| 23 | +spring.datasource.control.hikari.maximum-pool-size= 100 | |
| 24 | +#spring.datasource.control.hikari.auto-commit= true | |
| 25 | +spring.datasource.control.hikari.idle-timeout= 60000 | |
| 26 | +#spring.datasource.control.hikari.pool-name= HikariPool | |
| 27 | +spring.datasource.control.hikari.max-lifetime= 1800000 | |
| 28 | +spring.datasource.control.hikari.connection-timeout= 3000 | |
| 29 | +spring.datasource.control.hikari.connection-test-query= SELECT 1 | |
| 30 | +spring.datasource.control.hikari.validation-timeout= 3000 | |
| 31 | +spring.datasource.control.hikari.register-mbeans=true | |
| 28 | 32 | |
| 29 | -#DATASOURCE SETTING | |
| 30 | -spring.datasource.hikari.minimum-idle= 8 | |
| 31 | -spring.datasource.hikari.maximum-pool-size= 100 | |
| 32 | -#spring.datasource.hikari.auto-commit= true | |
| 33 | -spring.datasource.hikari.idle-timeout= 60000 | |
| 34 | -#spring.datasource.hikari.pool-name= HikariPool | |
| 35 | -spring.datasource.hikari.max-lifetime= 1800000 | |
| 36 | -spring.datasource.hikari.connection-timeout= 3000 | |
| 37 | -spring.datasource.hikari.connection-test-query= SELECT 1 | |
| 38 | -spring.datasource.hikari.validation-timeout= 3000 | |
| 39 | -spring.datasource.hikari.register-mbeans=true | |
| 33 | +#DATABASE info-publish | |
| 34 | +spring.datasource.info-publish.driver-class-name= com.mysql.jdbc.Driver | |
| 35 | +spring.datasource.info-publish.jdbc-url= jdbc:mysql://10.10.2.20/info_publish?rewriteBatchedStatements=true&useUnicode=true&characterEncoding=utf-8&useSSL=false | |
| 36 | +spring.datasource.info-publish.username= root | |
| 37 | +spring.datasource.info-publish.password= root2jsp | |
| 38 | +spring.datasource.info-publish.type= com.zaxxer.hikari.HikariDataSource | |
| 39 | +#DATASOURCE | |
| 40 | +spring.datasource.info-publish.hikari.minimum-idle= 8 | |
| 41 | +spring.datasource.info-publish.hikari.maximum-pool-size= 100 | |
| 42 | +#spring.datasource.info-publish.hikari.auto-commit= true | |
| 43 | +spring.datasource.info-publish.hikari.idle-timeout= 60000 | |
| 44 | +#spring.datasource.info-publish.hikari.pool-name= HikariPool | |
| 45 | +spring.datasource.info-publish.hikari.max-lifetime= 1800000 | |
| 46 | +spring.datasource.info-publish.hikari.connection-timeout= 3000 | |
| 47 | +spring.datasource.info-publish.hikari.connection-test-query= SELECT 1 | |
| 48 | +spring.datasource.info-publish.hikari.validation-timeout= 3000 | |
| 49 | +spring.datasource.info-publish.hikari.register-mbeans=true | |
| 40 | 50 | |
| 41 | 51 | kafka.use= false |
| 42 | 52 | spring.kafka.consumer.bootstrap-servers= localhost:9092 | ... | ... |
src/main/resources/application-test.properties
| ... | ... | @@ -12,27 +12,44 @@ spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true |
| 12 | 12 | spring.jpa.show-sql= false |
| 13 | 13 | spring.jpa.properties.hibernate.dialect= org.hibernate.spatial.dialect.mysql.MySQL56InnoDBSpatialDialect |
| 14 | 14 | |
| 15 | -#DATABASE | |
| 16 | -spring.datasource.driver-class-name= com.mysql.jdbc.Driver | |
| 17 | -spring.datasource.url= jdbc:mysql://192.168.168.152/control_dy?useUnicode=true&characterEncoding=utf-8&useSSL=false | |
| 18 | -spring.datasource.username= root | |
| 19 | -spring.datasource.password= root2jsp | |
| 20 | -spring.datasource.type= com.zaxxer.hikari.HikariDataSource | |
| 21 | - | |
| 22 | -#DATASOURCE SETTING | |
| 23 | -spring.datasource.hikari.minimum-idle= 8 | |
| 24 | -spring.datasource.hikari.maximum-pool-size= 100 | |
| 25 | -#spring.datasource.hikari.auto-commit= true | |
| 26 | -spring.datasource.hikari.idle-timeout= 60000 | |
| 27 | -#spring.datasource.hikari.pool-name= HikariPool | |
| 28 | -spring.datasource.hikari.max-lifetime= 1800000 | |
| 29 | -spring.datasource.hikari.connection-timeout= 3000 | |
| 30 | -spring.datasource.hikari.connection-test-query= SELECT 1 | |
| 31 | -spring.datasource.hikari.validation-timeout= 3000 | |
| 32 | -spring.datasource.hikari.register-mbeans=true | |
| 15 | +#DATABASE control | |
| 16 | +spring.datasource.control.driver-class-name= com.mysql.jdbc.Driver | |
| 17 | +spring.datasource.control.jdbc-url= jdbc:mysql://10.10.2.200/control_dy?useUnicode=true&characterEncoding=utf-8&useSSL=false | |
| 18 | +spring.datasource.control.username= root | |
| 19 | +spring.datasource.control.password= root2jsp | |
| 20 | +spring.datasource.control.type= com.zaxxer.hikari.HikariDataSource | |
| 21 | +#DATASOURCE | |
| 22 | +spring.datasource.control.hikari.minimum-idle= 8 | |
| 23 | +spring.datasource.control.hikari.maximum-pool-size= 100 | |
| 24 | +#spring.datasource.control.hikari.auto-commit= true | |
| 25 | +spring.datasource.control.hikari.idle-timeout= 60000 | |
| 26 | +#spring.datasource.control.hikari.pool-name= HikariPool | |
| 27 | +spring.datasource.control.hikari.max-lifetime= 1800000 | |
| 28 | +spring.datasource.control.hikari.connection-timeout= 3000 | |
| 29 | +spring.datasource.control.hikari.connection-test-query= SELECT 1 | |
| 30 | +spring.datasource.control.hikari.validation-timeout= 3000 | |
| 31 | +spring.datasource.control.hikari.register-mbeans=true | |
| 32 | + | |
| 33 | +#DATABASE info-publish | |
| 34 | +spring.datasource.info-publish.driver-class-name= com.mysql.jdbc.Driver | |
| 35 | +spring.datasource.info-publish.jdbc-url= jdbc:mysql://10.10.2.200/info_publish?rewriteBatchedStatements=true&useUnicode=true&characterEncoding=utf-8&useSSL=false | |
| 36 | +spring.datasource.info-publish.username= root | |
| 37 | +spring.datasource.info-publish.password= root2jsp | |
| 38 | +spring.datasource.info-publish.type= com.zaxxer.hikari.HikariDataSource | |
| 39 | +#DATASOURCE | |
| 40 | +spring.datasource.info-publish.hikari.minimum-idle= 8 | |
| 41 | +spring.datasource.info-publish.hikari.maximum-pool-size= 100 | |
| 42 | +#spring.datasource.info-publish.hikari.auto-commit= true | |
| 43 | +spring.datasource.info-publish.hikari.idle-timeout= 60000 | |
| 44 | +#spring.datasource.info-publish.hikari.pool-name= HikariPool | |
| 45 | +spring.datasource.info-publish.hikari.max-lifetime= 1800000 | |
| 46 | +spring.datasource.info-publish.hikari.connection-timeout= 3000 | |
| 47 | +spring.datasource.info-publish.hikari.connection-test-query= SELECT 1 | |
| 48 | +spring.datasource.info-publish.hikari.validation-timeout= 3000 | |
| 49 | +spring.datasource.info-publish.hikari.register-mbeans=true | |
| 33 | 50 | |
| 34 | 51 | kafka.use= false |
| 35 | -spring.kafka.consumer.bootstrap-servers= 192.170.100.114:9092,192.170.100.114:9093,192.170.100.114:9094 | |
| 52 | +spring.kafka.consumer.bootstrap-servers= 127.0.0.1:9092 | |
| 36 | 53 | spring.kafka.consumer.group-id= schedule-system-test |
| 37 | 54 | spring.kafka.consumer.auto-offset-reset= latest |
| 38 | 55 | spring.kafka.consumer.key-deserializer= org.apache.kafka.common.serialization.StringDeserializer | ... | ... |
src/main/resources/static/pages/forms/mould/scanStatistic.xls
0 → 100644
No preview for this file type
src/main/resources/static/pages/permission/authorize_all/user_auth.html
| ... | ... | @@ -56,6 +56,7 @@ |
| 56 | 56 | <li><label><input class="uk-checkbox" type="checkbox" data-event="form_message"> 调度消息分析</label></li> |
| 57 | 57 | <li><label><input class="uk-checkbox" type="checkbox" data-event="form_changetochange"> 换人换车情况统计表</label></li> |
| 58 | 58 | <li><label><input class="uk-checkbox" type="checkbox" data-event="form_repairReport"> 维修上报记录</label></li> |
| 59 | + <li><label><input class="uk-checkbox" type="checkbox" data-event="form_scanStatistic"> 一站一码</label></li> | |
| 59 | 60 | </ul> |
| 60 | 61 | </div> |
| 61 | 62 | ... | ... |
src/main/resources/static/real_control_v2/fragments/north/nav/alarm/adas.html
| ... | ... | @@ -122,13 +122,27 @@ |
| 122 | 122 | if (resetPagination) |
| 123 | 123 | pagination(rs.totalPages + 1, rs.page); |
| 124 | 124 | $('.image_link').on('click', function() { |
| 125 | - var urls = $(this).data('url').split(','), items = []; | |
| 125 | + var urls = $(this).data('url').split(','), items = [], file, type; | |
| 126 | 126 | urls.forEach(function (url, idx) { |
| 127 | - if (url.endsWith('.bin')) return; | |
| 128 | - items.push({title: '附件' + (idx + 1), source: 'http://118.113.164.50:9008/dsm/' + url}); | |
| 127 | + if (!url || url.endsWith('.bin')) return; | |
| 128 | + items.push(url); | |
| 129 | 129 | }); |
| 130 | - | |
| 131 | - UIkit.lightbox.create(items, {keyboard: true}).show(); | |
| 130 | + gb_common.$get('/api/minio/preSignedUrl', {objects: items}, function(res) { | |
| 131 | + urls = res.urls; | |
| 132 | + items = []; | |
| 133 | + urls.forEach(function (url, idx) { | |
| 134 | + file = url.substring(0, url.indexOf('?')); | |
| 135 | + if (file.endsWith('.png') || file.endsWith('.jpg')) { | |
| 136 | + type = 'image'; | |
| 137 | + } else { | |
| 138 | + type = 'video'; | |
| 139 | + } | |
| 140 | + items.push({title: '附件' + (idx + 1), source: url, type: type}); | |
| 141 | + }); | |
| 142 | + if (items.length > 0) { | |
| 143 | + UIkit.lightbox.create(items, {keyboard: true}).show(); | |
| 144 | + } | |
| 145 | + }) | |
| 132 | 146 | }) |
| 133 | 147 | }); |
| 134 | 148 | }; | ... | ... |
src/main/resources/static/real_control_v2/fragments/north/nav/alarm/dsm.html
| ... | ... | @@ -125,13 +125,27 @@ |
| 125 | 125 | if (resetPagination) |
| 126 | 126 | pagination(rs.totalPages + 1, rs.page); |
| 127 | 127 | $('.image_link').on('click', function() { |
| 128 | - var urls = $(this).data('url').split(','), items = []; | |
| 128 | + var urls = $(this).data('url').split(','), items = [], file, type; | |
| 129 | 129 | urls.forEach(function (url, idx) { |
| 130 | - if (url.endsWith('.bin')) return; | |
| 131 | - items.push({title: '附件' + (idx + 1), source: 'http://118.113.164.50:9008/dsm/' + url}); | |
| 130 | + if (!url || url.endsWith('.bin')) return; | |
| 131 | + items.push(url); | |
| 132 | 132 | }); |
| 133 | - | |
| 134 | - UIkit.lightbox.create(items, {keyboard: true}).show(); | |
| 133 | + gb_common.$get('/api/minio/preSignedUrl', {objects: items}, function(res) { | |
| 134 | + urls = res.urls; | |
| 135 | + items = []; | |
| 136 | + urls.forEach(function (url, idx) { | |
| 137 | + file = url.substring(0, url.indexOf('?')); | |
| 138 | + if (file.endsWith('.png') || file.endsWith('.jpg')) { | |
| 139 | + type = 'image'; | |
| 140 | + } else { | |
| 141 | + type = 'video'; | |
| 142 | + } | |
| 143 | + items.push({title: '附件' + (idx + 1), source: url, type: type}); | |
| 144 | + }); | |
| 145 | + if (items.length > 0) { | |
| 146 | + UIkit.lightbox.create(items, {keyboard: true}).show(); | |
| 147 | + } | |
| 148 | + }) | |
| 135 | 149 | }) |
| 136 | 150 | }); |
| 137 | 151 | }; | ... | ... |
src/main/resources/static/real_control_v2/fragments/north/nav/info_publish/scan_statistic.html
0 → 100644
| 1 | +<div class="uk-modal ct_move_modal" id="scan_statistic_modal" style="z-index: 99;"> | |
| 2 | + <div class="uk-modal-dialog" style="width: 1150px;"> | |
| 3 | + <a href="" class="uk-modal-close uk-close"></a> | |
| 4 | + <div class="uk-modal-header"> | |
| 5 | + <h2>一站一码扫码情况</h2></div> | |
| 6 | + | |
| 7 | + <div class="uk-panel uk-panel-box uk-panel-box-primary"> | |
| 8 | + <form class="uk-form search-form"> | |
| 9 | + <fieldset data-uk-margin> | |
| 10 | + <legend> | |
| 11 | + 数据检索 | |
| 12 | + </legend> | |
| 13 | + <span class="horizontal-field">日期</span> | |
| 14 | + <input class="uk-input" type="text" id="rq" name="rq" placeholder="日期" autocomplete="false"> | |
| 15 | + <span class="horizontal-field">站名</span> | |
| 16 | + <input type="text" name="stationName" placeholder="站名" autocomplete="false"/> | |
| 17 | + <button class="uk-button">检索</button> | |
| 18 | + <button id="exportBtn" class="uk-button">导出</button> | |
| 19 | + </fieldset> | |
| 20 | + </form> | |
| 21 | + </div> | |
| 22 | + <div style="height: 495px;margin:5px 0 -18px;"> | |
| 23 | + <table class="ct-fixed-table uk-table uk-table-hover"> | |
| 24 | + <thead> | |
| 25 | + <tr> | |
| 26 | + <th style="width: 10%;">日期</th> | |
| 27 | + <th style="width: 11%;">站名</th> | |
| 28 | + <th style="width: 11%;">站点编码</th> | |
| 29 | + <th style="width: 12%;">次数</th> | |
| 30 | + </tr> | |
| 31 | + </thead> | |
| 32 | + <tbody data-uk-observe> | |
| 33 | + </tbody> | |
| 34 | + </table> | |
| 35 | + </div> | |
| 36 | + | |
| 37 | + <div class="uk-modal-footer uk-text-right pagination-wrap"> | |
| 38 | + </div> | |
| 39 | + </div> | |
| 40 | + | |
| 41 | + | |
| 42 | + <script id="scan-statistic-table-template" type="text/html"> | |
| 43 | + {{each array as statistic i}} | |
| 44 | + <tr> | |
| 45 | + <td style="width: 14%;">{{statistic.rq}}</td> | |
| 46 | + <td style="width: 14%;">{{statistic.stationName}}</td> | |
| 47 | + <td style="width: 13%;">{{statistic.physicalCode}}</td> | |
| 48 | + <td style="width: 13%;">{{statistic.count}}</td> | |
| 49 | + </tr> | |
| 50 | + {{/each}} | |
| 51 | + </script> | |
| 52 | + | |
| 53 | + <script> | |
| 54 | + (function() { | |
| 55 | + var modal = '#scan_statistic_modal'; | |
| 56 | + var form = $('.search-form', modal); | |
| 57 | + var page = 0; | |
| 58 | + var pageSize = 12; | |
| 59 | + | |
| 60 | + //日期选择框 | |
| 61 | + var fs='YYYYMMDD', ets=moment().subtract(1, 'days').format(fs), sts=moment().subtract(6, 'days').format(fs); | |
| 62 | + flatpickr('#rq', { | |
| 63 | + mode: "range", dateFormat: "Ymd","locale": "zh", defaultDate: [sts, ets] | |
| 64 | + }); | |
| 65 | + | |
| 66 | + $(modal).on('init', function(e, data) { | |
| 67 | + e.stopPropagation(); | |
| 68 | + //车辆 autocomplete | |
| 69 | + gb_common.carAutocomplete($('.autocomplete-cars', modal), gb_data_basic.carsArray()); | |
| 70 | + | |
| 71 | + query(); | |
| 72 | + }); | |
| 73 | + | |
| 74 | + //sumit event | |
| 75 | + form.on('submit', function(e) { | |
| 76 | + e.preventDefault(); | |
| 77 | + resetPagination = true; | |
| 78 | + page=0; | |
| 79 | + query(); | |
| 80 | + }); | |
| 81 | + | |
| 82 | + $('#exportBtn').on('click', function(e) { | |
| 83 | + var data = form.serializeJSON(); | |
| 84 | + if (!data.rq) { | |
| 85 | + layer.msg('日期为必填项') | |
| 86 | + return; | |
| 87 | + } | |
| 88 | + var start = data.rq.substring(0, 8), end = data.rq.substring(11); | |
| 89 | + data.rq = start + '-' + end; | |
| 90 | + | |
| 91 | + gb_common.$get('/api/info_publish/scan_statistic/list_export', data, function (rs) { | |
| 92 | + window.open('/downloadFile/download?fileName=一站一码扫码情况' + data.rq + '.xls'); | |
| 93 | + }) | |
| 94 | + }) | |
| 95 | + | |
| 96 | + var query = function() { | |
| 97 | + var data = form.serializeJSON(); | |
| 98 | + if (!data.rq) { | |
| 99 | + layer.msg('日期为必填项') | |
| 100 | + return; | |
| 101 | + } | |
| 102 | + var start = data.rq.substring(0, 8), end = data.rq.substring(11); | |
| 103 | + data.rq = start + '-' + end; | |
| 104 | + data.page = page; | |
| 105 | + data.size = pageSize; | |
| 106 | + | |
| 107 | + gb_common.$post('/api/info_publish/scan_statistic/list', data, function (rs) { | |
| 108 | + var bodyHtml = template('scan-statistic-table-template', { | |
| 109 | + array: rs.list | |
| 110 | + }); | |
| 111 | + $('table tbody', modal).html(bodyHtml); | |
| 112 | + if (resetPagination) | |
| 113 | + pagination(rs.totalPages + 1, rs.page); | |
| 114 | + }); | |
| 115 | + }; | |
| 116 | + | |
| 117 | + var resetPagination = true; | |
| 118 | + var pagination = function(pages, currentPage) { | |
| 119 | + var wrap = $('.pagination-wrap', modal).empty() | |
| 120 | + ,e = $('<ul class="uk-pagination"></ul>').appendTo(wrap); | |
| 121 | + | |
| 122 | + var pagination = UIkit.pagination(e, { | |
| 123 | + pages: pages, | |
| 124 | + currentPage: currentPage | |
| 125 | + }); | |
| 126 | + | |
| 127 | + e.on('select.uk.pagination', function(e, pageIndex){ | |
| 128 | + page = pageIndex; | |
| 129 | + query(); | |
| 130 | + }); | |
| 131 | + | |
| 132 | + resetPagination = false; | |
| 133 | + }; | |
| 134 | + })(); | |
| 135 | + </script> | |
| 136 | +</div> | ... | ... |
src/main/resources/static/real_control_v2/js/data/json/north_toolbar.json
src/main/resources/static/real_control_v2/js/north/toolbar.js
| ... | ... | @@ -212,6 +212,9 @@ var gb_northToolbar = (function () { |
| 212 | 212 | form_repairReport: function () { |
| 213 | 213 | gb_embed_form_hanlde.open_modal_form_fragment('/pages/forms/statement/repairReport.html', '维修上报记录'); |
| 214 | 214 | }, |
| 215 | + form_scanStatistic: function () { | |
| 216 | + open_modal('/real_control_v2/fragments/north/nav/info_publish/scan_statistic.html', {}, modal_opts); | |
| 217 | + }, | |
| 215 | 218 | form_turnoutrate: function () { |
| 216 | 219 | gb_embed_form_hanlde.open_modal_form_fragment('/pages/mforms/changetochanges/turnoutrate.html', '营运线路出车率统计表'); |
| 217 | 220 | }, | ... | ... |
src/main/resources/static/real_control_v2/js/safe_driv/safeDriv.js
| ... | ... | @@ -108,16 +108,28 @@ var gb_safe_driv = (function () { |
| 108 | 108 | case 'dsm': |
| 109 | 109 | // adas报警数据 |
| 110 | 110 | case 'adas': |
| 111 | - var urls = $(this).data('url').split(','), items = []; | |
| 111 | + var urls = $(this).data('url').split(','), items = [], that = this, file, type; | |
| 112 | 112 | urls.forEach(function (url, idx) { |
| 113 | 113 | if (!url || url.endsWith('.bin')) return; |
| 114 | - items.push({title: '附件' + (idx + 1), source: 'http://118.113.164.50:9008/dsm/' + url}); | |
| 114 | + items.push(url); | |
| 115 | 115 | }); |
| 116 | - $(this).remove(); | |
| 117 | - | |
| 118 | - if (items.length > 0) { | |
| 119 | - UIkit.lightbox.create(items, {keyboard: true}).show(); | |
| 120 | - } | |
| 116 | + gb_common.$get('/api/minio/preSignedUrl', {objects: items}, function(res) { | |
| 117 | + $(that).remove(); | |
| 118 | + urls = res.urls; | |
| 119 | + items = []; | |
| 120 | + urls.forEach(function (url, idx) { | |
| 121 | + file = url.substring(0, url.indexOf('?')); | |
| 122 | + if (file.endsWith('.png') || file.endsWith('.jpg')) { | |
| 123 | + type = 'image'; | |
| 124 | + } else { | |
| 125 | + type = 'video'; | |
| 126 | + } | |
| 127 | + items.push({title: '附件' + (idx + 1), source: url, type: type}); | |
| 128 | + }); | |
| 129 | + if (items.length > 0) { | |
| 130 | + UIkit.lightbox.create(items, {keyboard: true}).show(); | |
| 131 | + } | |
| 132 | + }) | |
| 121 | 133 | break; |
| 122 | 134 | default: |
| 123 | 135 | break; | ... | ... |