Commit ba641265ef49f4ec5749fd2635147d145ccf86e0
1 parent
7ad9e686
1.线路调查表按线路名排序、第一列加序号、数字数据导出时保持数字格式
Showing
5 changed files
with
1717 additions
and
1702 deletions
src/main/java/com/bsth/repository/calc/CalcInvestigateMonthRepository.java
| 1 | -package com.bsth.repository.calc; | |
| 2 | - | |
| 3 | - | |
| 4 | -import java.util.List; | |
| 5 | - | |
| 6 | -import org.springframework.data.jpa.repository.Query; | |
| 7 | -import org.springframework.stereotype.Repository; | |
| 8 | - | |
| 9 | -import com.bsth.entity.calc.CalcInvestigateMonth; | |
| 10 | -import com.bsth.repository.BaseRepository; | |
| 11 | - | |
| 12 | -@Repository | |
| 13 | -public interface CalcInvestigateMonthRepository extends BaseRepository<CalcInvestigateMonth, Integer>{ | |
| 14 | - | |
| 15 | - @Query(value="select c from CalcInvestigateMonth c where month = ?1") | |
| 16 | - List<CalcInvestigateMonth> findByMonth(String month); | |
| 17 | - | |
| 18 | - @Query(value="select c from CalcInvestigateMonth c where month = ?1 and gsbm like %?2% and fgsbm like %?3%") | |
| 19 | - List<CalcInvestigateMonth> findByMonth(String month, String gsbm, String fgsbm); | |
| 20 | - | |
| 21 | - @Query(value="select c from CalcInvestigateMonth c where month = ?1 and xl = ?2") | |
| 22 | - List<CalcInvestigateMonth> findByMonthAndLine(String month, String line); | |
| 23 | - | |
| 24 | -} | |
| 1 | +package com.bsth.repository.calc; | |
| 2 | + | |
| 3 | + | |
| 4 | +import java.util.List; | |
| 5 | + | |
| 6 | +import org.springframework.data.jpa.repository.Query; | |
| 7 | +import org.springframework.stereotype.Repository; | |
| 8 | + | |
| 9 | +import com.bsth.entity.calc.CalcInvestigateMonth; | |
| 10 | +import com.bsth.repository.BaseRepository; | |
| 11 | + | |
| 12 | +@Repository | |
| 13 | +public interface CalcInvestigateMonthRepository extends BaseRepository<CalcInvestigateMonth, Integer>{ | |
| 14 | + | |
| 15 | + @Query(value="select c from CalcInvestigateMonth c where month = ?1 order by c.xlmc") | |
| 16 | + List<CalcInvestigateMonth> findByMonth(String month); | |
| 17 | + | |
| 18 | + @Query(value="select c from CalcInvestigateMonth c where month = ?1 and gsbm like %?2% and fgsbm like %?3% order by c.xlmc") | |
| 19 | + List<CalcInvestigateMonth> findByMonth(String month, String gsbm, String fgsbm); | |
| 20 | + | |
| 21 | + @Query(value="select c from CalcInvestigateMonth c where month = ?1 and xl = ?2 order by c.xlmc") | |
| 22 | + List<CalcInvestigateMonth> findByMonthAndLine(String month, String line); | |
| 23 | + | |
| 24 | +} | ... | ... |
src/main/java/com/bsth/service/calc/impl/CalcMixServiceImpl.java
| 1 | -package com.bsth.service.calc.impl; | |
| 2 | - | |
| 3 | -import java.sql.ResultSet; | |
| 4 | -import java.sql.SQLException; | |
| 5 | -import java.text.SimpleDateFormat; | |
| 6 | -import java.util.ArrayList; | |
| 7 | -import java.util.HashMap; | |
| 8 | -import java.util.Iterator; | |
| 9 | -import java.util.List; | |
| 10 | -import java.util.Map; | |
| 11 | - | |
| 12 | -import com.bsth.data.BasicData; | |
| 13 | -import com.bsth.entity.calc.CalcWaybill; | |
| 14 | -import com.bsth.entity.calc.CalcInvestigateMonth; | |
| 15 | -import com.bsth.entity.mcy_forms.Singledata; | |
| 16 | -import com.bsth.repository.calc.CalcInvestigateMonthRepository; | |
| 17 | -import com.bsth.repository.calc.CalcWaybillRepository; | |
| 18 | -import com.bsth.service.LineService; | |
| 19 | -import com.bsth.service.calc.CalcMixService; | |
| 20 | -import com.bsth.util.Arith; | |
| 21 | -import com.bsth.util.ReportUtils; | |
| 22 | - | |
| 23 | -import org.slf4j.Logger; | |
| 24 | -import org.slf4j.LoggerFactory; | |
| 25 | -import org.springframework.beans.factory.annotation.Autowired; | |
| 26 | -import org.springframework.jdbc.core.JdbcTemplate; | |
| 27 | -import org.springframework.jdbc.core.RowMapper; | |
| 28 | -import org.springframework.stereotype.Service; | |
| 29 | - | |
| 30 | -/** | |
| 31 | - * Created by 19/02/28. | |
| 32 | - */ | |
| 33 | -@Service | |
| 34 | -public class CalcMixServiceImpl implements CalcMixService { | |
| 35 | - | |
| 36 | - @Autowired | |
| 37 | - private CalcWaybillRepository calcRepository; | |
| 38 | - | |
| 39 | - @Autowired | |
| 40 | - private CalcInvestigateMonthRepository calcInvestigateMonthRepository; | |
| 41 | - | |
| 42 | - @Autowired | |
| 43 | - private LineService lineService; | |
| 44 | - | |
| 45 | - @Autowired | |
| 46 | - JdbcTemplate jdbcTemplate; | |
| 47 | - | |
| 48 | - | |
| 49 | - Logger logger = LoggerFactory.getLogger(this.getClass()); | |
| 50 | - | |
| 51 | - | |
| 52 | - @Override | |
| 53 | - public List<Map<String, Object>> calcjsyspy(String line, String date, String date2, | |
| 54 | - String empnames, String cont, String gsdm, String fgsdm) { | |
| 55 | - | |
| 56 | - List<Map<String, Object>> resList = new ArrayList<Map<String, Object>>(); | |
| 57 | - List<CalcWaybill> list = null; | |
| 58 | - | |
| 59 | - int flag = 0; | |
| 60 | - if("驾驶员".equals(empnames)){ | |
| 61 | - flag = 1; | |
| 62 | - list = calcRepository.scheduleByJsy(line, date, date2, gsdm, fgsdm, cont); | |
| 63 | - } else if("售票员".equals(empnames)){ | |
| 64 | - flag = 2; | |
| 65 | - list = calcRepository.scheduleBySpy(line, date, date2, gsdm, fgsdm, cont); | |
| 66 | - } else if("车辆自编号".equals(empnames)){ | |
| 67 | - flag = 3; | |
| 68 | - list = calcRepository.scheduleByZbh(line, date, date2, gsdm, fgsdm, cont); | |
| 69 | - } | |
| 70 | - | |
| 71 | - List<CalcWaybill> calcList = new ArrayList<CalcWaybill>(); | |
| 72 | - Map<String, CalcWaybill> calcMap = new HashMap<String, CalcWaybill>(); | |
| 73 | - | |
| 74 | - CalcWaybill zjCalc = this.initCalcWaybill(); | |
| 75 | - zjCalc.setjName("合计"); | |
| 76 | - for(CalcWaybill c : list){ | |
| 77 | - String key = ""; | |
| 78 | - if(flag == 1){ | |
| 79 | - if(c.getjGh() != null && c.getjName() != null) | |
| 80 | - key = c.getjGh() + "/" + c.getjName(); | |
| 81 | - } else if(flag == 2){ | |
| 82 | - if(c.getsGh() != null && c.getsName() != null) | |
| 83 | - key = c.getsGh() + "/" + c.getsName(); | |
| 84 | - } else if(flag == 3){ | |
| 85 | - if(c.getCl() != null) | |
| 86 | - key = c.getCl(); | |
| 87 | - } | |
| 88 | - | |
| 89 | - if(key.trim().length() == 0 || "/".equals(key)) | |
| 90 | - continue; | |
| 91 | - | |
| 92 | - CalcWaybill calc = null; | |
| 93 | - if(calcMap.containsKey(key)){ | |
| 94 | - calc = calcMap.get(key); | |
| 95 | - } else { | |
| 96 | - calc = this.initCalcWaybill(); | |
| 97 | - calc.setjName(key); | |
| 98 | - calcList.add(calc); | |
| 99 | - calcMap.put(key, calc); | |
| 100 | - } | |
| 101 | - | |
| 102 | - this.summation(calc, c); | |
| 103 | - | |
| 104 | - this.summation(zjCalc, c); | |
| 105 | - | |
| 106 | - } | |
| 107 | - calcList.add(zjCalc); | |
| 108 | - calcMap.put("合计", zjCalc); | |
| 109 | - | |
| 110 | - for(CalcWaybill c : calcList){ | |
| 111 | - Map<String, Object> m = new HashMap<String, Object>(); | |
| 112 | - m.put("jName", c.getjName()); | |
| 113 | - m.put("jhyybc", c.getJhyybc()); | |
| 114 | - m.put("jhfyybc", c.getJhfyybc()); | |
| 115 | - m.put("sjyybc", c.getSjyybc()); | |
| 116 | - m.put("sjfyybc", c.getSjfyybc()); | |
| 117 | - m.put("lbbc", c.getLbbc()); | |
| 118 | - m.put("ljbc", c.getLjbc()); | |
| 119 | - m.put("jhzlc", Arith.add(c.getJhyylc(), c.getJhfyylc())); | |
| 120 | - m.put("jhyylc", c.getJhyylc()); | |
| 121 | - m.put("jhfyylc", c.getJhfyylc()); | |
| 122 | - m.put("sjzlc", Arith.add(c.getSjyylc(), c.getSjfyylc())); | |
| 123 | - m.put("sjyylc", c.getSjyylc()); | |
| 124 | - m.put("sjfyylc", c.getSjfyylc()); | |
| 125 | - m.put("lblc", c.getLblc()); | |
| 126 | - m.put("ljyylc", c.getLjyylc()); | |
| 127 | - m.put("ljfyylc", c.getLjfyylc()); | |
| 128 | - resList.add(m); | |
| 129 | - } | |
| 130 | - | |
| 131 | - return resList; | |
| 132 | - } | |
| 133 | - | |
| 134 | - public CalcWaybill initCalcWaybill(){ | |
| 135 | - CalcWaybill calc = new CalcWaybill(); | |
| 136 | - calc.setJhyybc(0); | |
| 137 | - calc.setJhyylc(0d); | |
| 138 | - calc.setJhfyybc(0); | |
| 139 | - calc.setJhfyylc(0d); | |
| 140 | - calc.setSjyybc(0); | |
| 141 | - calc.setSjyylc(0d); | |
| 142 | - calc.setSjfyybc(0); | |
| 143 | - calc.setSjfyylc(0d); | |
| 144 | - calc.setLbbc(0); | |
| 145 | - calc.setLblc(0d); | |
| 146 | - calc.setLjbc(0); | |
| 147 | - calc.setLjyylc(0d); | |
| 148 | - calc.setLjfyylc(0d); | |
| 149 | - return calc; | |
| 150 | - } | |
| 151 | - | |
| 152 | - public CalcWaybill summation(CalcWaybill c1, CalcWaybill c2){ | |
| 153 | - c1.setJhyybc(c1.getJhyybc() + c2.getJhyybc()); | |
| 154 | - c1.setJhyylc(Arith.add(c1.getJhyylc(), c2.getJhyylc())); | |
| 155 | - c1.setJhfyybc(c1.getJhfyybc() + c2.getJhfyybc()); | |
| 156 | - c1.setJhfyylc(Arith.add(c1.getJhfyylc(), c2.getJhfyylc())); | |
| 157 | - c1.setSjyybc(c1.getSjyybc() + c2.getSjyybc()); | |
| 158 | - c1.setSjyylc(Arith.add(c1.getSjyylc(), c2.getSjyylc())); | |
| 159 | - c1.setSjyylc(Arith.add(c1.getSjyylc(), c2.getLjyylc())); | |
| 160 | - c1.setSjfyybc(c1.getSjfyybc() + c2.getSjfyybc()); | |
| 161 | - c1.setSjfyylc(Arith.add(c1.getSjfyylc(), c2.getSjfyylc())); | |
| 162 | - c1.setSjfyylc(Arith.add(c1.getSjfyylc(), c2.getLjfyylc())); | |
| 163 | - c1.setLbbc(c1.getLbbc() + c2.getLbbc()); | |
| 164 | - c1.setLblc(Arith.add(c1.getLblc(), c2.getLblc())); | |
| 165 | - c1.setLjbc(c1.getLjbc() + c2.getLjbc()); | |
| 166 | - c1.setLjyylc(Arith.add(c1.getLjyylc(), c2.getLjyylc())); | |
| 167 | - c1.setLjfyylc(Arith.add(c1.getLjfyylc(), c2.getLjfyylc())); | |
| 168 | - return c1; | |
| 169 | - } | |
| 170 | - | |
| 171 | - @Override | |
| 172 | - public List<Map<String, Object>> singledatatj(String line, | |
| 173 | - String date, String date2, | |
| 174 | - String tjtype, String cont, | |
| 175 | - String gsdm, String fgsdm, String sfdc) { | |
| 176 | - | |
| 177 | - List<Map<String, Object>> resList = new ArrayList<Map<String, Object>>(); | |
| 178 | - List<CalcWaybill> list = null; | |
| 179 | - | |
| 180 | - int flag = 0; | |
| 181 | - if("驾驶员".equals(tjtype)){ | |
| 182 | - flag = 1; | |
| 183 | - list = calcRepository.scheduleByJsy(line, date, date2, gsdm, fgsdm, cont); | |
| 184 | - } else if("售票员".equals(tjtype)){ | |
| 185 | - flag = 2; | |
| 186 | - list = calcRepository.scheduleBySpy(line, date, date2, gsdm, fgsdm, cont); | |
| 187 | - } else if("车辆自编号".equals(tjtype)){ | |
| 188 | - flag = 3; | |
| 189 | - if("1".equals(sfdc)){ | |
| 190 | - list = calcRepository.scheduleByZbh(line, date, date2, gsdm, fgsdm, cont, true); | |
| 191 | - } else if("0".equals(sfdc)){ | |
| 192 | - list = calcRepository.scheduleByZbh(line, date, date2, gsdm, fgsdm, cont, false); | |
| 193 | - } else { | |
| 194 | - list = calcRepository.scheduleByZbh(line, date, date2, gsdm, fgsdm, cont); | |
| 195 | - } | |
| 196 | - } | |
| 197 | - | |
| 198 | - List<CalcWaybill> calcList = new ArrayList<CalcWaybill>(); | |
| 199 | - Map<String, CalcWaybill> calcMap = new HashMap<String, CalcWaybill>(); | |
| 200 | - | |
| 201 | - CalcWaybill zjCalc = this.initCalcWaybill(); | |
| 202 | - zjCalc.setjName("合计"); | |
| 203 | - for(CalcWaybill c : list){ | |
| 204 | - String key = ""; | |
| 205 | - if(flag == 1){ | |
| 206 | - if(c.getjGh() != null && c.getjName() != null) | |
| 207 | - key += c.getjGh() + "/" + c.getjName(); | |
| 208 | - } else if(flag == 2){ | |
| 209 | - if(c.getsGh() != null && c.getsName() != null) | |
| 210 | - key += c.getsGh() + "/" + c.getsName(); | |
| 211 | - } else if(flag == 3){ | |
| 212 | - if(c.getCl() != null) | |
| 213 | - key += c.getCl(); | |
| 214 | - } | |
| 215 | - | |
| 216 | - if(key.trim().length() == 0 || "/".equals(key)) | |
| 217 | - continue; | |
| 218 | - | |
| 219 | - String jName = key; | |
| 220 | - key = c.getXl() + "/" + key; | |
| 221 | - | |
| 222 | - CalcWaybill calc = null; | |
| 223 | - if(calcMap.containsKey(key)){ | |
| 224 | - calc = calcMap.get(key); | |
| 225 | - } else { | |
| 226 | - calc = this.initCalcWaybill(); | |
| 227 | - calc.setXlName(c.getXlName()); | |
| 228 | - calc.setXl(c.getXl()); | |
| 229 | - calc.setjName(jName); | |
| 230 | - calcList.add(calc); | |
| 231 | - calcMap.put(key, calc); | |
| 232 | - } | |
| 233 | - | |
| 234 | - this.summation(calc, c); | |
| 235 | - | |
| 236 | - this.summation(zjCalc, c); | |
| 237 | - | |
| 238 | - } | |
| 239 | - calcList.add(zjCalc); | |
| 240 | - calcMap.put("合计", zjCalc); | |
| 241 | - | |
| 242 | - Map<String, Map<String, Object>> keyMap = new HashMap<String, Map<String, Object>>(); | |
| 243 | - for(CalcWaybill c : calcList){ | |
| 244 | - Map<String, Object> m = new HashMap<String, Object>(); | |
| 245 | - m.put("gS", BasicData.businessFgsCodeNameMap.get(fgsdm+"_"+gsdm)); | |
| 246 | - m.put("xl", c.getXl()); | |
| 247 | - m.put("xlName", c.getXlName()); | |
| 248 | - m.put("jName", c.getjName()); | |
| 249 | - m.put("jhyybc", c.getJhyybc()); | |
| 250 | - m.put("jhfyybc", c.getJhfyybc()); | |
| 251 | - m.put("sjyybc", c.getSjyybc()); | |
| 252 | - m.put("sjfyybc", c.getSjfyybc()); | |
| 253 | - m.put("lbbc", c.getLbbc()); | |
| 254 | - m.put("ljbc", c.getLjbc()); | |
| 255 | - m.put("jhzlc", Arith.add(c.getJhyylc(), c.getJhfyylc())); | |
| 256 | - m.put("jhyylc", c.getJhyylc()); | |
| 257 | - m.put("jhfyylc", c.getJhfyylc()); | |
| 258 | - m.put("sjzlc", Arith.add(c.getSjyylc(), c.getSjfyylc())); | |
| 259 | - m.put("sjyylc", c.getSjyylc()); | |
| 260 | - m.put("sjfyylc", c.getSjfyylc()); | |
| 261 | - m.put("lblc", c.getLblc()); | |
| 262 | - m.put("ljyylc", c.getLjyylc()); | |
| 263 | - m.put("ljfyylc", c.getLjfyylc()); | |
| 264 | - if(flag != 2){ | |
| 265 | - m.put("hyl", 0); | |
| 266 | - m.put("jzl", 0); | |
| 267 | - m.put("sh", 0); | |
| 268 | - } | |
| 269 | - resList.add(m); | |
| 270 | - keyMap.put(c.getXl() + "/" + c.getjName(), m); | |
| 271 | - } | |
| 272 | - | |
| 273 | - String linesql=""; | |
| 274 | - if(!line.equals("")){ | |
| 275 | - linesql +=" and xlbm ='"+line+"' "; | |
| 276 | - } | |
| 277 | - if(!gsdm.equals("")){ | |
| 278 | - linesql +=" and ssgsdm ='"+gsdm+"' "; | |
| 279 | - } | |
| 280 | - if(!fgsdm.equals("")){ | |
| 281 | - linesql +=" and fgsdm ='"+fgsdm+"' "; | |
| 282 | - } | |
| 283 | - String nysql="SELECT id,xlbm,nbbm,jsy,jzl as jzl,yh as yh,sh as sh,fgsdm FROM bsth_c_ylb" | |
| 284 | - + " WHERE rq >= '"+date+"' and rq <= '"+date2+"'" | |
| 285 | - + linesql | |
| 286 | - + " union" | |
| 287 | - + " SELECT id,xlbm,nbbm,jsy,cdl as jzl,hd as yh,sh as sh,fgsdm FROM bsth_c_dlb" | |
| 288 | - + " WHERE rq >= '"+date+"' and rq <= '"+date2+"'" | |
| 289 | - + linesql; | |
| 290 | - List<Singledata> listNy = jdbcTemplate.query(nysql, new RowMapper<Singledata>() { | |
| 291 | - @Override | |
| 292 | - public Singledata mapRow(ResultSet arg0, int arg1) throws SQLException { | |
| 293 | - Singledata sin = new Singledata(); | |
| 294 | - sin.setxL(arg0.getString("xlbm")); | |
| 295 | - sin.setJsy(arg0.getString("jsy")); | |
| 296 | - sin.setClzbh(arg0.getString("nbbm")); | |
| 297 | - sin.setJzl(arg0.getString("jzl")); | |
| 298 | - sin.setHyl(arg0.getString("yh")); | |
| 299 | - sin.setUnyyyl(arg0.getString("sh")); | |
| 300 | - sin.setgS(arg0.getString("fgsdm")); | |
| 301 | - return sin; | |
| 302 | - } | |
| 303 | - }); | |
| 304 | - | |
| 305 | - if(flag != 2){ | |
| 306 | - Map<String, Object> last = resList.get(resList.size()-1); | |
| 307 | - last.put("hyl", 0); | |
| 308 | - last.put("jzl", 0); | |
| 309 | - last.put("sh", 0); | |
| 310 | - | |
| 311 | - //统计油,电表中手动添加的或者有加注没里程的数据 | |
| 312 | - for (int i = 0; i < listNy.size(); i++) { | |
| 313 | - Singledata sin_=listNy.get(i); | |
| 314 | - String xl=sin_.getxL(); | |
| 315 | - String str = ""; | |
| 316 | - if(flag == 1){ | |
| 317 | - str = sin_.getJsy() + "/" + BasicData.allPerson.get(gsdm+"-"+sin_.getJsy()); | |
| 318 | - } else { | |
| 319 | - str = sin_.getClzbh(); | |
| 320 | - } | |
| 321 | -// boolean fages=true; | |
| 322 | - if(keyMap.containsKey(xl + "/" + str)){ | |
| 323 | - Map<String, Object> m = keyMap.get(xl + "/" + str); | |
| 324 | - m.put("hyl", Arith.add(m.get("hyl"), sin_.getHyl()!=null?sin_.getHyl():0)); | |
| 325 | - m.put("jzl", Arith.add(m.get("jzl"), sin_.getJzl()!=null?sin_.getJzl():0)); | |
| 326 | - m.put("sh", Arith.add(m.get("sh"), sin_.getUnyyyl()!=null?sin_.getUnyyyl():0)); | |
| 327 | - last.put("hyl", Arith.add(last.get("hyl"), sin_.getHyl()!=null?sin_.getHyl():0)); | |
| 328 | - last.put("jzl", Arith.add(last.get("jzl"), sin_.getJzl()!=null?sin_.getJzl():0)); | |
| 329 | - last.put("sh", Arith.add(last.get("sh"), sin_.getUnyyyl()!=null?sin_.getUnyyyl():0)); | |
| 330 | - } else { | |
| 331 | - Map<String, Object> m = new HashMap<String, Object>(); | |
| 332 | - m.put("gS", BasicData.businessFgsCodeNameMap.get(fgsdm+"_"+gsdm)); | |
| 333 | - m.put("xl", xl); | |
| 334 | - m.put("xlName", BasicData.lineCodeAllNameMap.get(xl)); | |
| 335 | - m.put("jName", str); | |
| 336 | - m.put("jhzlc", 0); | |
| 337 | - m.put("sjzlc", 0); | |
| 338 | - m.put("sjfyylc", 0); | |
| 339 | - m.put("hyl", sin_.getHyl()!=null?sin_.getHyl():0); | |
| 340 | - m.put("jzl", sin_.getJzl()!=null?sin_.getJzl():0); | |
| 341 | - m.put("sh", sin_.getUnyyyl()!=null?sin_.getUnyyyl():0); | |
| 342 | - last.put("hyl", Arith.add(last.get("hyl"), m.get("hyl"))); | |
| 343 | - last.put("jzl", Arith.add(last.get("jzl"), m.get("jzl"))); | |
| 344 | - last.put("sh", Arith.add(last.get("sh"), m.get("sh"))); | |
| 345 | - | |
| 346 | - keyMap.put(xl + "/" + str, m); | |
| 347 | - } | |
| 348 | - } | |
| 349 | - resList.remove(last); | |
| 350 | - resList.add(last); | |
| 351 | - } | |
| 352 | - | |
| 353 | - return resList; | |
| 354 | - } | |
| 355 | - | |
| 356 | - //浦东公交线路调查表 | |
| 357 | - @Override | |
| 358 | - public List<CalcInvestigateMonth> calcInvestigateMonth(String gsbm, String fgsbm, String month, String line, | |
| 359 | - String xlName, String nature, String type) { | |
| 360 | - List<CalcInvestigateMonth> resList = new ArrayList<CalcInvestigateMonth>(); | |
| 361 | - | |
| 362 | - Map<String, Boolean> lineNature = lineService.lineNature(); | |
| 363 | - | |
| 364 | - List<CalcInvestigateMonth> list = new ArrayList<CalcInvestigateMonth>(); | |
| 365 | - if(line != null && line.length() > 0){ | |
| 366 | - list = calcInvestigateMonthRepository.findByMonthAndLine(month, line); | |
| 367 | - } else if(gsbm != null && gsbm.length() > 0){ | |
| 368 | - list = calcInvestigateMonthRepository.findByMonth(month, gsbm, fgsbm); | |
| 369 | - } else { | |
| 370 | - list = calcInvestigateMonthRepository.findByMonth(month); | |
| 371 | - } | |
| 372 | - | |
| 373 | - for(CalcInvestigateMonth c : list){ | |
| 374 | - if("1".equals(nature)){ | |
| 375 | - if(lineNature.containsKey(c.getXl()) && lineNature.get(c.getXl())){ | |
| 376 | - resList.add(c); | |
| 377 | - } | |
| 378 | - } else if("2".equals(nature)){ | |
| 379 | - if(!lineNature.containsKey(c.getXl()) || !lineNature.get(c.getXl())){ | |
| 380 | - resList.add(c); | |
| 381 | - } | |
| 382 | - } else { | |
| 383 | - resList.add(c); | |
| 384 | - } | |
| 385 | - } | |
| 386 | - | |
| 387 | - if (type != null && type.length() != 0 && type.equals("export")) { | |
| 388 | - SimpleDateFormat sdfMonth = new SimpleDateFormat("yyyy-MM-dd"), | |
| 389 | - sdfSimple = new SimpleDateFormat("yyyyMMdd"), | |
| 390 | - monSimple = new SimpleDateFormat("yyyy-MM"); | |
| 391 | - List<Iterator<?>> listI = new ArrayList<Iterator<?>>(); | |
| 392 | - Map<String, Object> m = new HashMap<String, Object>(); | |
| 393 | - m.put("date", month); | |
| 394 | - m.put("currMonth", "(" + month + ")"); | |
| 395 | - ReportUtils ee = new ReportUtils(); | |
| 396 | - try { | |
| 397 | - List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>(); | |
| 398 | - for(CalcInvestigateMonth c : resList){ | |
| 399 | - Map<String, Object> map = new HashMap<String, Object>(); | |
| 400 | - map.put("xlmc", c.getXlmc()!=null?c.getXlmc():""); | |
| 401 | - map.put("qzpcs", c.getQzpcs()!=null?c.getQzpcs():""); | |
| 402 | - map.put("xlsx", c.getXlsx()!=null?c.getXlsx():""); | |
| 403 | - map.put("xllx", c.getXllx()!=null?c.getXllx():""); | |
| 404 | - map.put("xlcd", c.getXlcd()!=null?c.getXlcd():""); | |
| 405 | - map.put("sfgp", c.getSfgp()!=null?c.getSfgp():""); | |
| 406 | - map.put("sflpxl", c.getSflpxl()!=null?c.getSflpxl():""); | |
| 407 | - map.put("smbsj", c.getSmbsj()!=null?c.getSmbsj():""); | |
| 408 | - if("常规".equals(c.getDdfs())){ | |
| 409 | - map.put("ddfsCg", "√"); | |
| 410 | - map.put("ddfsDyh", ""); | |
| 411 | - } else if("多样化".equals(c.getDdfs())){ | |
| 412 | - map.put("ddfsCg", ""); | |
| 413 | - map.put("ddfsDyh", "√"); | |
| 414 | - } else { | |
| 415 | - map.put("ddfsCg", ""); | |
| 416 | - map.put("ddfsDyh", ""); | |
| 417 | - } | |
| 418 | - map.put("gfjgsj", c.getGfjgsj()!=null?c.getGfjgsj():""); | |
| 419 | - map.put("dgjgsj", c.getDgjgsj()!=null?c.getDgjgsj():""); | |
| 420 | - map.put("sfxjgj", c.getSfxjgj()!=null?c.getSfxjgj():""); | |
| 421 | - map.put("rjlc", c.getRjlc()!=null?c.getRjlc():""); | |
| 422 | - map.put("rjbc", c.getRjbc()!=null?c.getRjbc():""); | |
| 423 | - map.put("rjrc", c.getRjrc()!=null?c.getRjrc():""); | |
| 424 | - map.put("rjdcrc", c.getRjdcrc()!=null?c.getRjdcrc():""); | |
| 425 | - map.put("mbcrc", c.getMbcrc()!=null?c.getMbcrc():""); | |
| 426 | - map.put("jhzgl", c.getJhzgl()!=null?c.getJhzgl():""); | |
| 427 | - map.put("sjzgl", c.getSjzgl()!=null?c.getSjzgl():""); | |
| 428 | - map.put("sjyylc", c.getSjyylc()!=null?c.getSjyylc():""); | |
| 429 | - map.put("lclyl", c.getLclyl()!=null?c.getLclyl():""); | |
| 430 | - map.put("bglrc", c.getBglrc()!=null?c.getBglrc():""); | |
| 431 | - map.put("yrc", c.getYrc()!=null?c.getYrc():""); | |
| 432 | - map.put("yys", c.getYys()!=null?c.getYys():""); | |
| 433 | - map.put("pj", c.getPj()!=null?c.getPj():""); | |
| 434 | - map.put("bglys", c.getBglys()!=null?c.getBglys():""); | |
| 435 | - map.put("tjgjz", c.getTjgjz()!=null?c.getTjgjz():""); | |
| 436 | - map.put("tjsjyy", c.getTjsjyy()!=null?c.getTjsjyy():""); | |
| 437 | - map.put("wyx", c.getWyx()!=null?c.getWyx():""); | |
| 438 | - map.put("xlzx", c.getXlzx()!=null?c.getXlzx():""); | |
| 439 | - map.put("xlzd", c.getXlzd()!=null?c.getXlzd():""); | |
| 440 | - map.put("bz", c.getBz()!=null?c.getBz():""); | |
| 441 | - mapList.add(map); | |
| 442 | - } | |
| 443 | - listI.add(mapList.iterator()); | |
| 444 | - String path = this.getClass().getResource("/").getPath() + "static/pages/forms/"; | |
| 445 | - ee.excelReplace(listI, new Object[]{m}, path + "mould/calcInvestigateMonth.xls", | |
| 446 | - path + "export/" + "浦东公交线路调查表"+month+".xls"); | |
| 447 | - } catch (Exception e) { | |
| 448 | - // TODO: handle exception | |
| 449 | - e.printStackTrace(); | |
| 450 | - logger.info("" , e); | |
| 451 | - } | |
| 452 | - } | |
| 453 | - return resList; | |
| 454 | - } | |
| 455 | - | |
| 456 | -} | |
| 1 | +package com.bsth.service.calc.impl; | |
| 2 | + | |
| 3 | +import java.sql.ResultSet; | |
| 4 | +import java.sql.SQLException; | |
| 5 | +import java.text.SimpleDateFormat; | |
| 6 | +import java.util.ArrayList; | |
| 7 | +import java.util.HashMap; | |
| 8 | +import java.util.Iterator; | |
| 9 | +import java.util.List; | |
| 10 | +import java.util.Map; | |
| 11 | + | |
| 12 | +import com.bsth.data.BasicData; | |
| 13 | +import com.bsth.entity.calc.CalcWaybill; | |
| 14 | +import com.bsth.entity.calc.CalcInvestigateMonth; | |
| 15 | +import com.bsth.entity.mcy_forms.Singledata; | |
| 16 | +import com.bsth.repository.calc.CalcInvestigateMonthRepository; | |
| 17 | +import com.bsth.repository.calc.CalcWaybillRepository; | |
| 18 | +import com.bsth.service.LineService; | |
| 19 | +import com.bsth.service.calc.CalcMixService; | |
| 20 | +import com.bsth.util.Arith; | |
| 21 | +import com.bsth.util.ReportUtils; | |
| 22 | + | |
| 23 | +import org.slf4j.Logger; | |
| 24 | +import org.slf4j.LoggerFactory; | |
| 25 | +import org.springframework.beans.factory.annotation.Autowired; | |
| 26 | +import org.springframework.jdbc.core.JdbcTemplate; | |
| 27 | +import org.springframework.jdbc.core.RowMapper; | |
| 28 | +import org.springframework.stereotype.Service; | |
| 29 | + | |
| 30 | +/** | |
| 31 | + * Created by 19/02/28. | |
| 32 | + */ | |
| 33 | +@Service | |
| 34 | +public class CalcMixServiceImpl implements CalcMixService { | |
| 35 | + | |
| 36 | + @Autowired | |
| 37 | + private CalcWaybillRepository calcRepository; | |
| 38 | + | |
| 39 | + @Autowired | |
| 40 | + private CalcInvestigateMonthRepository calcInvestigateMonthRepository; | |
| 41 | + | |
| 42 | + @Autowired | |
| 43 | + private LineService lineService; | |
| 44 | + | |
| 45 | + @Autowired | |
| 46 | + JdbcTemplate jdbcTemplate; | |
| 47 | + | |
| 48 | + | |
| 49 | + Logger logger = LoggerFactory.getLogger(this.getClass()); | |
| 50 | + | |
| 51 | + | |
| 52 | + @Override | |
| 53 | + public List<Map<String, Object>> calcjsyspy(String line, String date, String date2, | |
| 54 | + String empnames, String cont, String gsdm, String fgsdm) { | |
| 55 | + | |
| 56 | + List<Map<String, Object>> resList = new ArrayList<Map<String, Object>>(); | |
| 57 | + List<CalcWaybill> list = null; | |
| 58 | + | |
| 59 | + int flag = 0; | |
| 60 | + if("驾驶员".equals(empnames)){ | |
| 61 | + flag = 1; | |
| 62 | + list = calcRepository.scheduleByJsy(line, date, date2, gsdm, fgsdm, cont); | |
| 63 | + } else if("售票员".equals(empnames)){ | |
| 64 | + flag = 2; | |
| 65 | + list = calcRepository.scheduleBySpy(line, date, date2, gsdm, fgsdm, cont); | |
| 66 | + } else if("车辆自编号".equals(empnames)){ | |
| 67 | + flag = 3; | |
| 68 | + list = calcRepository.scheduleByZbh(line, date, date2, gsdm, fgsdm, cont); | |
| 69 | + } | |
| 70 | + | |
| 71 | + List<CalcWaybill> calcList = new ArrayList<CalcWaybill>(); | |
| 72 | + Map<String, CalcWaybill> calcMap = new HashMap<String, CalcWaybill>(); | |
| 73 | + | |
| 74 | + CalcWaybill zjCalc = this.initCalcWaybill(); | |
| 75 | + zjCalc.setjName("合计"); | |
| 76 | + for(CalcWaybill c : list){ | |
| 77 | + String key = ""; | |
| 78 | + if(flag == 1){ | |
| 79 | + if(c.getjGh() != null && c.getjName() != null) | |
| 80 | + key = c.getjGh() + "/" + c.getjName(); | |
| 81 | + } else if(flag == 2){ | |
| 82 | + if(c.getsGh() != null && c.getsName() != null) | |
| 83 | + key = c.getsGh() + "/" + c.getsName(); | |
| 84 | + } else if(flag == 3){ | |
| 85 | + if(c.getCl() != null) | |
| 86 | + key = c.getCl(); | |
| 87 | + } | |
| 88 | + | |
| 89 | + if(key.trim().length() == 0 || "/".equals(key)) | |
| 90 | + continue; | |
| 91 | + | |
| 92 | + CalcWaybill calc = null; | |
| 93 | + if(calcMap.containsKey(key)){ | |
| 94 | + calc = calcMap.get(key); | |
| 95 | + } else { | |
| 96 | + calc = this.initCalcWaybill(); | |
| 97 | + calc.setjName(key); | |
| 98 | + calcList.add(calc); | |
| 99 | + calcMap.put(key, calc); | |
| 100 | + } | |
| 101 | + | |
| 102 | + this.summation(calc, c); | |
| 103 | + | |
| 104 | + this.summation(zjCalc, c); | |
| 105 | + | |
| 106 | + } | |
| 107 | + calcList.add(zjCalc); | |
| 108 | + calcMap.put("合计", zjCalc); | |
| 109 | + | |
| 110 | + for(CalcWaybill c : calcList){ | |
| 111 | + Map<String, Object> m = new HashMap<String, Object>(); | |
| 112 | + m.put("jName", c.getjName()); | |
| 113 | + m.put("jhyybc", c.getJhyybc()); | |
| 114 | + m.put("jhfyybc", c.getJhfyybc()); | |
| 115 | + m.put("sjyybc", c.getSjyybc()); | |
| 116 | + m.put("sjfyybc", c.getSjfyybc()); | |
| 117 | + m.put("lbbc", c.getLbbc()); | |
| 118 | + m.put("ljbc", c.getLjbc()); | |
| 119 | + m.put("jhzlc", Arith.add(c.getJhyylc(), c.getJhfyylc())); | |
| 120 | + m.put("jhyylc", c.getJhyylc()); | |
| 121 | + m.put("jhfyylc", c.getJhfyylc()); | |
| 122 | + m.put("sjzlc", Arith.add(c.getSjyylc(), c.getSjfyylc())); | |
| 123 | + m.put("sjyylc", c.getSjyylc()); | |
| 124 | + m.put("sjfyylc", c.getSjfyylc()); | |
| 125 | + m.put("lblc", c.getLblc()); | |
| 126 | + m.put("ljyylc", c.getLjyylc()); | |
| 127 | + m.put("ljfyylc", c.getLjfyylc()); | |
| 128 | + resList.add(m); | |
| 129 | + } | |
| 130 | + | |
| 131 | + return resList; | |
| 132 | + } | |
| 133 | + | |
| 134 | + public CalcWaybill initCalcWaybill(){ | |
| 135 | + CalcWaybill calc = new CalcWaybill(); | |
| 136 | + calc.setJhyybc(0); | |
| 137 | + calc.setJhyylc(0d); | |
| 138 | + calc.setJhfyybc(0); | |
| 139 | + calc.setJhfyylc(0d); | |
| 140 | + calc.setSjyybc(0); | |
| 141 | + calc.setSjyylc(0d); | |
| 142 | + calc.setSjfyybc(0); | |
| 143 | + calc.setSjfyylc(0d); | |
| 144 | + calc.setLbbc(0); | |
| 145 | + calc.setLblc(0d); | |
| 146 | + calc.setLjbc(0); | |
| 147 | + calc.setLjyylc(0d); | |
| 148 | + calc.setLjfyylc(0d); | |
| 149 | + return calc; | |
| 150 | + } | |
| 151 | + | |
| 152 | + public CalcWaybill summation(CalcWaybill c1, CalcWaybill c2){ | |
| 153 | + c1.setJhyybc(c1.getJhyybc() + c2.getJhyybc()); | |
| 154 | + c1.setJhyylc(Arith.add(c1.getJhyylc(), c2.getJhyylc())); | |
| 155 | + c1.setJhfyybc(c1.getJhfyybc() + c2.getJhfyybc()); | |
| 156 | + c1.setJhfyylc(Arith.add(c1.getJhfyylc(), c2.getJhfyylc())); | |
| 157 | + c1.setSjyybc(c1.getSjyybc() + c2.getSjyybc()); | |
| 158 | + c1.setSjyylc(Arith.add(c1.getSjyylc(), c2.getSjyylc())); | |
| 159 | + c1.setSjyylc(Arith.add(c1.getSjyylc(), c2.getLjyylc())); | |
| 160 | + c1.setSjfyybc(c1.getSjfyybc() + c2.getSjfyybc()); | |
| 161 | + c1.setSjfyylc(Arith.add(c1.getSjfyylc(), c2.getSjfyylc())); | |
| 162 | + c1.setSjfyylc(Arith.add(c1.getSjfyylc(), c2.getLjfyylc())); | |
| 163 | + c1.setLbbc(c1.getLbbc() + c2.getLbbc()); | |
| 164 | + c1.setLblc(Arith.add(c1.getLblc(), c2.getLblc())); | |
| 165 | + c1.setLjbc(c1.getLjbc() + c2.getLjbc()); | |
| 166 | + c1.setLjyylc(Arith.add(c1.getLjyylc(), c2.getLjyylc())); | |
| 167 | + c1.setLjfyylc(Arith.add(c1.getLjfyylc(), c2.getLjfyylc())); | |
| 168 | + return c1; | |
| 169 | + } | |
| 170 | + | |
| 171 | + @Override | |
| 172 | + public List<Map<String, Object>> singledatatj(String line, | |
| 173 | + String date, String date2, | |
| 174 | + String tjtype, String cont, | |
| 175 | + String gsdm, String fgsdm, String sfdc) { | |
| 176 | + | |
| 177 | + List<Map<String, Object>> resList = new ArrayList<Map<String, Object>>(); | |
| 178 | + List<CalcWaybill> list = null; | |
| 179 | + | |
| 180 | + int flag = 0; | |
| 181 | + if("驾驶员".equals(tjtype)){ | |
| 182 | + flag = 1; | |
| 183 | + list = calcRepository.scheduleByJsy(line, date, date2, gsdm, fgsdm, cont); | |
| 184 | + } else if("售票员".equals(tjtype)){ | |
| 185 | + flag = 2; | |
| 186 | + list = calcRepository.scheduleBySpy(line, date, date2, gsdm, fgsdm, cont); | |
| 187 | + } else if("车辆自编号".equals(tjtype)){ | |
| 188 | + flag = 3; | |
| 189 | + if("1".equals(sfdc)){ | |
| 190 | + list = calcRepository.scheduleByZbh(line, date, date2, gsdm, fgsdm, cont, true); | |
| 191 | + } else if("0".equals(sfdc)){ | |
| 192 | + list = calcRepository.scheduleByZbh(line, date, date2, gsdm, fgsdm, cont, false); | |
| 193 | + } else { | |
| 194 | + list = calcRepository.scheduleByZbh(line, date, date2, gsdm, fgsdm, cont); | |
| 195 | + } | |
| 196 | + } | |
| 197 | + | |
| 198 | + List<CalcWaybill> calcList = new ArrayList<CalcWaybill>(); | |
| 199 | + Map<String, CalcWaybill> calcMap = new HashMap<String, CalcWaybill>(); | |
| 200 | + | |
| 201 | + CalcWaybill zjCalc = this.initCalcWaybill(); | |
| 202 | + zjCalc.setjName("合计"); | |
| 203 | + for(CalcWaybill c : list){ | |
| 204 | + String key = ""; | |
| 205 | + if(flag == 1){ | |
| 206 | + if(c.getjGh() != null && c.getjName() != null) | |
| 207 | + key += c.getjGh() + "/" + c.getjName(); | |
| 208 | + } else if(flag == 2){ | |
| 209 | + if(c.getsGh() != null && c.getsName() != null) | |
| 210 | + key += c.getsGh() + "/" + c.getsName(); | |
| 211 | + } else if(flag == 3){ | |
| 212 | + if(c.getCl() != null) | |
| 213 | + key += c.getCl(); | |
| 214 | + } | |
| 215 | + | |
| 216 | + if(key.trim().length() == 0 || "/".equals(key)) | |
| 217 | + continue; | |
| 218 | + | |
| 219 | + String jName = key; | |
| 220 | + key = c.getXl() + "/" + key; | |
| 221 | + | |
| 222 | + CalcWaybill calc = null; | |
| 223 | + if(calcMap.containsKey(key)){ | |
| 224 | + calc = calcMap.get(key); | |
| 225 | + } else { | |
| 226 | + calc = this.initCalcWaybill(); | |
| 227 | + calc.setXlName(c.getXlName()); | |
| 228 | + calc.setXl(c.getXl()); | |
| 229 | + calc.setjName(jName); | |
| 230 | + calcList.add(calc); | |
| 231 | + calcMap.put(key, calc); | |
| 232 | + } | |
| 233 | + | |
| 234 | + this.summation(calc, c); | |
| 235 | + | |
| 236 | + this.summation(zjCalc, c); | |
| 237 | + | |
| 238 | + } | |
| 239 | + calcList.add(zjCalc); | |
| 240 | + calcMap.put("合计", zjCalc); | |
| 241 | + | |
| 242 | + Map<String, Map<String, Object>> keyMap = new HashMap<String, Map<String, Object>>(); | |
| 243 | + for(CalcWaybill c : calcList){ | |
| 244 | + Map<String, Object> m = new HashMap<String, Object>(); | |
| 245 | + m.put("gS", BasicData.businessFgsCodeNameMap.get(fgsdm+"_"+gsdm)); | |
| 246 | + m.put("xl", c.getXl()); | |
| 247 | + m.put("xlName", c.getXlName()); | |
| 248 | + m.put("jName", c.getjName()); | |
| 249 | + m.put("jhyybc", c.getJhyybc()); | |
| 250 | + m.put("jhfyybc", c.getJhfyybc()); | |
| 251 | + m.put("sjyybc", c.getSjyybc()); | |
| 252 | + m.put("sjfyybc", c.getSjfyybc()); | |
| 253 | + m.put("lbbc", c.getLbbc()); | |
| 254 | + m.put("ljbc", c.getLjbc()); | |
| 255 | + m.put("jhzlc", Arith.add(c.getJhyylc(), c.getJhfyylc())); | |
| 256 | + m.put("jhyylc", c.getJhyylc()); | |
| 257 | + m.put("jhfyylc", c.getJhfyylc()); | |
| 258 | + m.put("sjzlc", Arith.add(c.getSjyylc(), c.getSjfyylc())); | |
| 259 | + m.put("sjyylc", c.getSjyylc()); | |
| 260 | + m.put("sjfyylc", c.getSjfyylc()); | |
| 261 | + m.put("lblc", c.getLblc()); | |
| 262 | + m.put("ljyylc", c.getLjyylc()); | |
| 263 | + m.put("ljfyylc", c.getLjfyylc()); | |
| 264 | + if(flag != 2){ | |
| 265 | + m.put("hyl", 0); | |
| 266 | + m.put("jzl", 0); | |
| 267 | + m.put("sh", 0); | |
| 268 | + } | |
| 269 | + resList.add(m); | |
| 270 | + keyMap.put(c.getXl() + "/" + c.getjName(), m); | |
| 271 | + } | |
| 272 | + | |
| 273 | + String linesql=""; | |
| 274 | + if(!line.equals("")){ | |
| 275 | + linesql +=" and xlbm ='"+line+"' "; | |
| 276 | + } | |
| 277 | + if(!gsdm.equals("")){ | |
| 278 | + linesql +=" and ssgsdm ='"+gsdm+"' "; | |
| 279 | + } | |
| 280 | + if(!fgsdm.equals("")){ | |
| 281 | + linesql +=" and fgsdm ='"+fgsdm+"' "; | |
| 282 | + } | |
| 283 | + String nysql="SELECT id,xlbm,nbbm,jsy,jzl as jzl,yh as yh,sh as sh,fgsdm FROM bsth_c_ylb" | |
| 284 | + + " WHERE rq >= '"+date+"' and rq <= '"+date2+"'" | |
| 285 | + + linesql | |
| 286 | + + " union" | |
| 287 | + + " SELECT id,xlbm,nbbm,jsy,cdl as jzl,hd as yh,sh as sh,fgsdm FROM bsth_c_dlb" | |
| 288 | + + " WHERE rq >= '"+date+"' and rq <= '"+date2+"'" | |
| 289 | + + linesql; | |
| 290 | + List<Singledata> listNy = jdbcTemplate.query(nysql, new RowMapper<Singledata>() { | |
| 291 | + @Override | |
| 292 | + public Singledata mapRow(ResultSet arg0, int arg1) throws SQLException { | |
| 293 | + Singledata sin = new Singledata(); | |
| 294 | + sin.setxL(arg0.getString("xlbm")); | |
| 295 | + sin.setJsy(arg0.getString("jsy")); | |
| 296 | + sin.setClzbh(arg0.getString("nbbm")); | |
| 297 | + sin.setJzl(arg0.getString("jzl")); | |
| 298 | + sin.setHyl(arg0.getString("yh")); | |
| 299 | + sin.setUnyyyl(arg0.getString("sh")); | |
| 300 | + sin.setgS(arg0.getString("fgsdm")); | |
| 301 | + return sin; | |
| 302 | + } | |
| 303 | + }); | |
| 304 | + | |
| 305 | + if(flag != 2){ | |
| 306 | + Map<String, Object> last = resList.get(resList.size()-1); | |
| 307 | + last.put("hyl", 0); | |
| 308 | + last.put("jzl", 0); | |
| 309 | + last.put("sh", 0); | |
| 310 | + | |
| 311 | + //统计油,电表中手动添加的或者有加注没里程的数据 | |
| 312 | + for (int i = 0; i < listNy.size(); i++) { | |
| 313 | + Singledata sin_=listNy.get(i); | |
| 314 | + String xl=sin_.getxL(); | |
| 315 | + String str = ""; | |
| 316 | + if(flag == 1){ | |
| 317 | + str = sin_.getJsy() + "/" + BasicData.allPerson.get(gsdm+"-"+sin_.getJsy()); | |
| 318 | + } else { | |
| 319 | + str = sin_.getClzbh(); | |
| 320 | + } | |
| 321 | +// boolean fages=true; | |
| 322 | + if(keyMap.containsKey(xl + "/" + str)){ | |
| 323 | + Map<String, Object> m = keyMap.get(xl + "/" + str); | |
| 324 | + m.put("hyl", Arith.add(m.get("hyl"), sin_.getHyl()!=null?sin_.getHyl():0)); | |
| 325 | + m.put("jzl", Arith.add(m.get("jzl"), sin_.getJzl()!=null?sin_.getJzl():0)); | |
| 326 | + m.put("sh", Arith.add(m.get("sh"), sin_.getUnyyyl()!=null?sin_.getUnyyyl():0)); | |
| 327 | + last.put("hyl", Arith.add(last.get("hyl"), sin_.getHyl()!=null?sin_.getHyl():0)); | |
| 328 | + last.put("jzl", Arith.add(last.get("jzl"), sin_.getJzl()!=null?sin_.getJzl():0)); | |
| 329 | + last.put("sh", Arith.add(last.get("sh"), sin_.getUnyyyl()!=null?sin_.getUnyyyl():0)); | |
| 330 | + } else { | |
| 331 | + Map<String, Object> m = new HashMap<String, Object>(); | |
| 332 | + m.put("gS", BasicData.businessFgsCodeNameMap.get(fgsdm+"_"+gsdm)); | |
| 333 | + m.put("xl", xl); | |
| 334 | + m.put("xlName", BasicData.lineCodeAllNameMap.get(xl)); | |
| 335 | + m.put("jName", str); | |
| 336 | + m.put("jhzlc", 0); | |
| 337 | + m.put("sjzlc", 0); | |
| 338 | + m.put("sjfyylc", 0); | |
| 339 | + m.put("hyl", sin_.getHyl()!=null?sin_.getHyl():0); | |
| 340 | + m.put("jzl", sin_.getJzl()!=null?sin_.getJzl():0); | |
| 341 | + m.put("sh", sin_.getUnyyyl()!=null?sin_.getUnyyyl():0); | |
| 342 | + last.put("hyl", Arith.add(last.get("hyl"), m.get("hyl"))); | |
| 343 | + last.put("jzl", Arith.add(last.get("jzl"), m.get("jzl"))); | |
| 344 | + last.put("sh", Arith.add(last.get("sh"), m.get("sh"))); | |
| 345 | + | |
| 346 | + keyMap.put(xl + "/" + str, m); | |
| 347 | + } | |
| 348 | + } | |
| 349 | + resList.remove(last); | |
| 350 | + resList.add(last); | |
| 351 | + } | |
| 352 | + | |
| 353 | + return resList; | |
| 354 | + } | |
| 355 | + | |
| 356 | + //浦东公交线路调查表 | |
| 357 | + @Override | |
| 358 | + public List<CalcInvestigateMonth> calcInvestigateMonth(String gsbm, String fgsbm, String month, String line, | |
| 359 | + String xlName, String nature, String type) { | |
| 360 | + List<CalcInvestigateMonth> resList = new ArrayList<CalcInvestigateMonth>(); | |
| 361 | + | |
| 362 | + Map<String, Boolean> lineNature = lineService.lineNature(); | |
| 363 | + | |
| 364 | + List<CalcInvestigateMonth> list = new ArrayList<CalcInvestigateMonth>(); | |
| 365 | + if(line != null && line.length() > 0){ | |
| 366 | + list = calcInvestigateMonthRepository.findByMonthAndLine(month, line); | |
| 367 | + } else if(gsbm != null && gsbm.length() > 0){ | |
| 368 | + list = calcInvestigateMonthRepository.findByMonth(month, gsbm, fgsbm); | |
| 369 | + } else { | |
| 370 | + list = calcInvestigateMonthRepository.findByMonth(month); | |
| 371 | + } | |
| 372 | + | |
| 373 | + for(CalcInvestigateMonth c : list){ | |
| 374 | + if("1".equals(nature)){ | |
| 375 | + if(lineNature.containsKey(c.getXl()) && lineNature.get(c.getXl())){ | |
| 376 | + resList.add(c); | |
| 377 | + } | |
| 378 | + } else if("2".equals(nature)){ | |
| 379 | + if(!lineNature.containsKey(c.getXl()) || !lineNature.get(c.getXl())){ | |
| 380 | + resList.add(c); | |
| 381 | + } | |
| 382 | + } else { | |
| 383 | + resList.add(c); | |
| 384 | + } | |
| 385 | + } | |
| 386 | + | |
| 387 | + if (type != null && type.length() != 0 && type.equals("export")) { | |
| 388 | + SimpleDateFormat sdfMonth = new SimpleDateFormat("yyyy-MM-dd"), | |
| 389 | + sdfSimple = new SimpleDateFormat("yyyyMMdd"), | |
| 390 | + monSimple = new SimpleDateFormat("yyyy-MM"); | |
| 391 | + List<Iterator<?>> listI = new ArrayList<Iterator<?>>(); | |
| 392 | + Map<String, Object> m = new HashMap<String, Object>(); | |
| 393 | + m.put("date", month); | |
| 394 | + m.put("currMonth", "(" + month + ")"); | |
| 395 | + ReportUtils ee = new ReportUtils(); | |
| 396 | + try { | |
| 397 | + List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>(); | |
| 398 | + int row = 0; | |
| 399 | + for(CalcInvestigateMonth c : resList){ | |
| 400 | + Map<String, Object> map = new HashMap<String, Object>(); | |
| 401 | + map.put("row", ++row); | |
| 402 | + map.put("xlmc", c.getXlmc()!=null?c.getXlmc():""); | |
| 403 | + map.put("qzpcs", c.getQzpcs()!=null?c.getQzpcs():""); | |
| 404 | + map.put("xlsx", c.getXlsx()!=null?c.getXlsx():""); | |
| 405 | + map.put("xllx", c.getXllx()!=null?c.getXllx():""); | |
| 406 | + map.put("xlcd", c.getXlcd()!=null?c.getXlcd():""); | |
| 407 | + map.put("sfgp", c.getSfgp()!=null?c.getSfgp():""); | |
| 408 | + map.put("sflpxl", c.getSflpxl()!=null?c.getSflpxl():""); | |
| 409 | + map.put("smbsj", c.getSmbsj()!=null?c.getSmbsj():""); | |
| 410 | + if("常规".equals(c.getDdfs())){ | |
| 411 | + map.put("ddfsCg", "√"); | |
| 412 | + map.put("ddfsDyh", ""); | |
| 413 | + } else if("多样化".equals(c.getDdfs())){ | |
| 414 | + map.put("ddfsCg", ""); | |
| 415 | + map.put("ddfsDyh", "√"); | |
| 416 | + } else { | |
| 417 | + map.put("ddfsCg", ""); | |
| 418 | + map.put("ddfsDyh", ""); | |
| 419 | + } | |
| 420 | + map.put("gfjgsj", c.getGfjgsj()!=null?c.getGfjgsj():""); | |
| 421 | + map.put("dgjgsj", c.getDgjgsj()!=null?c.getDgjgsj():""); | |
| 422 | + map.put("sfxjgj", c.getSfxjgj()!=null?c.getSfxjgj():""); | |
| 423 | + map.put("rjlc", c.getRjlc()!=null?c.getRjlc():""); | |
| 424 | + map.put("rjbc", c.getRjbc()!=null?c.getRjbc():""); | |
| 425 | + map.put("rjrc", c.getRjrc()!=null?c.getRjrc():""); | |
| 426 | + map.put("rjdcrc", c.getRjdcrc()!=null?c.getRjdcrc():""); | |
| 427 | + map.put("mbcrc", c.getMbcrc()!=null?c.getMbcrc():""); | |
| 428 | + map.put("jhzgl", c.getJhzgl()!=null?c.getJhzgl():""); | |
| 429 | + map.put("sjzgl", c.getSjzgl()!=null?c.getSjzgl():""); | |
| 430 | + map.put("sjyylc", c.getSjyylc()!=null?c.getSjyylc():""); | |
| 431 | + map.put("lclyl", c.getLclyl()!=null?c.getLclyl():""); | |
| 432 | + map.put("bglrc", c.getBglrc()!=null?c.getBglrc():""); | |
| 433 | + map.put("yrc", c.getYrc()!=null?c.getYrc():""); | |
| 434 | + map.put("yys", c.getYys()!=null?c.getYys():""); | |
| 435 | + map.put("pj", c.getPj()!=null?c.getPj():""); | |
| 436 | + map.put("bglys", c.getBglys()!=null?c.getBglys():""); | |
| 437 | + map.put("tjgjz", c.getTjgjz()!=null?c.getTjgjz():""); | |
| 438 | + map.put("tjsjyy", c.getTjsjyy()!=null?c.getTjsjyy():""); | |
| 439 | + map.put("wyx", c.getWyx()!=null?c.getWyx():""); | |
| 440 | + map.put("xlzx", c.getXlzx()!=null?c.getXlzx():""); | |
| 441 | + map.put("xlzd", c.getXlzd()!=null?c.getXlzd():""); | |
| 442 | + map.put("bz", c.getBz()!=null?c.getBz():""); | |
| 443 | + mapList.add(map); | |
| 444 | + } | |
| 445 | + listI.add(mapList.iterator()); | |
| 446 | + String path = this.getClass().getResource("/").getPath() + "static/pages/forms/"; | |
| 447 | + ee.excelReplace(listI, new Object[]{m}, path + "mould/calcInvestigateMonth.xls", | |
| 448 | + path + "export/" + "浦东公交线路调查表"+month+".xls"); | |
| 449 | + } catch (Exception e) { | |
| 450 | + // TODO: handle exception | |
| 451 | + e.printStackTrace(); | |
| 452 | + logger.info("" , e); | |
| 453 | + } | |
| 454 | + } | |
| 455 | + return resList; | |
| 456 | + } | |
| 457 | + | |
| 458 | +} | ... | ... |
src/main/java/com/bsth/util/ReportUtils.java
| 1 | -package com.bsth.util; | |
| 2 | - | |
| 3 | -import java.io.File; | |
| 4 | -import java.io.FileInputStream; | |
| 5 | -import java.io.FileOutputStream; | |
| 6 | -import java.util.ArrayList; | |
| 7 | -import java.util.HashMap; | |
| 8 | -import java.util.Iterator; | |
| 9 | -import java.util.List; | |
| 10 | -import java.util.Map; | |
| 11 | -import java.util.TreeMap; | |
| 12 | -import java.util.regex.Matcher; | |
| 13 | -import java.util.regex.Pattern; | |
| 14 | - | |
| 15 | -import com.bsth.common.ResponseCode; | |
| 16 | -import org.apache.poi.hssf.usermodel.HSSFCell; | |
| 17 | -import org.apache.poi.hssf.usermodel.HSSFCellStyle; | |
| 18 | -import org.apache.poi.hssf.usermodel.HSSFFont; | |
| 19 | -import org.apache.poi.hssf.usermodel.HSSFRow; | |
| 20 | -import org.apache.poi.hssf.usermodel.HSSFSheet; | |
| 21 | -import org.apache.poi.hssf.usermodel.HSSFWorkbook; | |
| 22 | -import org.apache.poi.poifs.filesystem.POIFSFileSystem; | |
| 23 | -import org.apache.poi.ss.usermodel.Cell; | |
| 24 | -import org.apache.poi.ss.usermodel.Workbook; | |
| 25 | -import org.apache.poi.ss.util.CellRangeAddress; | |
| 26 | - | |
| 27 | -import com.bsth.entity.Line; | |
| 28 | -import com.bsth.entity.realcontrol.ScheduleRealInfo; | |
| 29 | -import org.apache.poi.ss.util.RegionUtil; | |
| 30 | -import org.apache.poi.xssf.usermodel.XSSFCell; | |
| 31 | - | |
| 32 | -public class ReportUtils { | |
| 33 | - // private final String packaegName = "com.bsth.entity."; | |
| 34 | - private final String packaegName = "com.bsth.entity.realcontrol."; | |
| 35 | - | |
| 36 | - /** | |
| 37 | - * /** | |
| 38 | - * | |
| 39 | - * @param list | |
| 40 | - * 模板中,需要重复显示的行所需的数据 | |
| 41 | - * @param map | |
| 42 | - * 模板中除以上list外所有的数据 | |
| 43 | - * @param index | |
| 44 | - * 需要重复的行号,该值为行号减1 | |
| 45 | - * @param sourcePath | |
| 46 | - * 模板路径 | |
| 47 | - * @param targetPath | |
| 48 | - * 生成路径 | |
| 49 | - */ | |
| 50 | - public void excelReplace(List<Iterator<?>> list, Object[] tArray, | |
| 51 | - String sourcePath, String targetPath) { | |
| 52 | - try { | |
| 53 | - // 把源文件放入流中 | |
| 54 | - POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream( | |
| 55 | - sourcePath)); | |
| 56 | - HSSFWorkbook wb = new HSSFWorkbook(fs); | |
| 57 | - if(tArray.length != 0 && tArray[0] instanceof java.util.Map){ | |
| 58 | - Map<String, Object> m = (Map<String, Object>)tArray[0]; | |
| 59 | - if(m.containsKey("sheetName") && m.get("sheetName")!=null | |
| 60 | - && m.get("sheetName").toString().trim().length()!=0) | |
| 61 | - wb.setSheetName(0, m.get("sheetName").toString()); | |
| 62 | - } | |
| 63 | - HSSFSheet sheet = wb.getSheetAt(0); | |
| 64 | - HSSFRow row; | |
| 65 | - HSSFCell cell = null; | |
| 66 | - String key; | |
| 67 | - // 取得总行数 | |
| 68 | - int rowNum = sheet.getLastRowNum(); | |
| 69 | - // 取得总列数 | |
| 70 | - int cellNum = sheet.getRow(0).getLastCellNum(); | |
| 71 | - | |
| 72 | - // 遍历行 | |
| 73 | - for (int i = 0; i < rowNum; i++) { | |
| 74 | - row = sheet.getRow(i); | |
| 75 | - // 遍历列 | |
| 76 | - for (int j = 0; j < cellNum; j++) { | |
| 77 | - if (row == null) { | |
| 78 | - continue; | |
| 79 | - } | |
| 80 | - cell = row.getCell(j); | |
| 81 | - if (cell == null) { | |
| 82 | - continue; | |
| 83 | - } | |
| 84 | - // 取得每列的内容,如果列内容是$key$格式,则替换内容 | |
| 85 | - key = getCellValue(cell); | |
| 86 | - if (key != null && (key.indexOf("$") != -1 || key.indexOf("#list#") != -1)) { | |
| 87 | - // * 列中内容有#list#,则表示该行为模板行,需要以该行为模板 | |
| 88 | - // * 例如:模板行格式 #list#0_0 $Car.id$ | |
| 89 | - // * 第一个0表示需要在list中取iterator的索引值 | |
| 90 | - // * 第二个0表示在iterator中取的第几个对象,如果不为0,则取下一个对象,而不是沿用前面获取的对象 | |
| 91 | - // * $Car.id$表示所取的对象为Car的对象,并且取值为id的值 | |
| 92 | - if (key.indexOf("#list#") != -1) { | |
| 93 | - key = key.replace("#list#", "").trim(); | |
| 94 | - String[] lists = key.split(" "); | |
| 95 | - // 取得list中的索引值 | |
| 96 | - int listIndex = Integer | |
| 97 | - .valueOf(lists[0].split("_")[0]); | |
| 98 | - Iterator<?> iterator = list.get(listIndex); | |
| 99 | - // 根据模板创建行并填弃数据,返回增加的行数 | |
| 100 | - int rowCount = iteratorFillCellValue(wb, sheet, | |
| 101 | - cell, iterator, i, rowNum, key); | |
| 102 | - rowNum += rowCount; | |
| 103 | - i += rowCount; | |
| 104 | - break; | |
| 105 | - } else { | |
| 106 | - // 直接填充数据的列,从对象数组中取得值,这里的数组不传值 | |
| 107 | - getValueAndSetCellValue(cell, key, tArray, new String[]{""}); | |
| 108 | - } | |
| 109 | - } | |
| 110 | - | |
| 111 | - } | |
| 112 | - } | |
| 113 | - // 创建目标文件夹 | |
| 114 | - createFolder(targetPath); | |
| 115 | - // 输出文件 | |
| 116 | - FileOutputStream fileOut = new FileOutputStream(targetPath); | |
| 117 | - wb.write(fileOut); | |
| 118 | - fileOut.close(); | |
| 119 | - } catch (Exception e) { | |
| 120 | - e.printStackTrace(); | |
| 121 | - } | |
| 122 | - } | |
| 123 | - | |
| 124 | - | |
| 125 | - /** | |
| 126 | - * | |
| 127 | - * @param sheetList 多sheet模板中,需要重复显示的行所需的数据 | |
| 128 | - * @param tArray | |
| 129 | - * @param sourcePath 模板路径 | |
| 130 | - * @param targetPath 生成路径 | |
| 131 | - */ | |
| 132 | - public void excelMoreSheetReplace(List<List<Iterator<?>>> sheetList, Object[] tArray, | |
| 133 | - String sourcePath, String targetPath) { | |
| 134 | - try { | |
| 135 | - // 把源文件放入流中 | |
| 136 | - POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream( | |
| 137 | - sourcePath)); | |
| 138 | - HSSFWorkbook wb = new HSSFWorkbook(fs); | |
| 139 | - for (int s=0; s<sheetList.size(); s++){ | |
| 140 | - List<Iterator<?>> list = sheetList.get(s); | |
| 141 | - if(tArray.length != 0 && tArray[0] instanceof java.util.Map){ | |
| 142 | - Map<String, Object> m = (Map<String, Object>)tArray[0]; | |
| 143 | - if(m.containsKey("sheetName"+s+1) && m.get("sheetName"+s+1)!=null | |
| 144 | - && m.get("sheetName"+s+1).toString().trim().length()!=0) | |
| 145 | - wb.setSheetName(0, m.get("sheetName"+s+1).toString()); | |
| 146 | - } | |
| 147 | - HSSFSheet sheet = wb.getSheetAt(s); | |
| 148 | - HSSFRow row; | |
| 149 | - HSSFCell cell = null; | |
| 150 | - String key; | |
| 151 | - // 取得总行数 | |
| 152 | - int rowNum = sheet.getLastRowNum(); | |
| 153 | - // 取得总列数 | |
| 154 | - int cellNum = sheet.getRow(0).getLastCellNum(); | |
| 155 | - | |
| 156 | - // 遍历行 | |
| 157 | - for (int i = 0; i <= rowNum; i++) { | |
| 158 | - row = sheet.getRow(i); | |
| 159 | - // 遍历列 | |
| 160 | - for (int j = 0; j < cellNum; j++) { | |
| 161 | - if (row == null) { | |
| 162 | - continue; | |
| 163 | - } | |
| 164 | - cell = row.getCell(j); | |
| 165 | - if (cell == null) { | |
| 166 | - continue; | |
| 167 | - } | |
| 168 | - // 取得每列的内容,如果列内容是$key$格式,则替换内容 | |
| 169 | - key = getCellValue(cell); | |
| 170 | - if (key != null && (key.indexOf("$") != -1 || key.indexOf("#list#") != -1)) { | |
| 171 | - // * 列中内容有#list#,则表示该行为模板行,需要以该行为模板 | |
| 172 | - // * 例如:模板行格式 #list#0_0 $Car.id$ | |
| 173 | - // * 第一个0表示需要在list中取iterator的索引值 | |
| 174 | - // * 第二个0表示在iterator中取的第几个对象,如果不为0,则取下一个对象,而不是沿用前面获取的对象 | |
| 175 | - // * $Car.id$表示所取的对象为Car的对象,并且取值为id的值 | |
| 176 | - if (key.indexOf("#list#") != -1) { | |
| 177 | - key = key.replace("#list#", "").trim(); | |
| 178 | - String[] lists = key.split(" "); | |
| 179 | - // 取得list中的索引值 | |
| 180 | - int listIndex = Integer | |
| 181 | - .valueOf(lists[0].split("_")[0]); | |
| 182 | - Iterator<?> iterator = list.get(listIndex); | |
| 183 | - // 根据模板创建行并填充数据,返回增加的行数 | |
| 184 | - int rowCount = iteratorFillCellValue(wb, sheet, | |
| 185 | - cell, iterator, i, rowNum, key); | |
| 186 | - rowNum += rowCount; | |
| 187 | - i += rowCount; | |
| 188 | - break; | |
| 189 | - } else { | |
| 190 | - // 直接填充数据的列,从对象数组中取得值,这里的数组不传值 | |
| 191 | - getValueAndSetCellValue(cell, key, tArray, new String[]{""}); | |
| 192 | - } | |
| 193 | - } | |
| 194 | - | |
| 195 | - } | |
| 196 | - } | |
| 197 | - } | |
| 198 | - | |
| 199 | - // 创建目标文件夹 | |
| 200 | - createFolder(targetPath); | |
| 201 | - // 输出文件 | |
| 202 | - FileOutputStream fileOut = new FileOutputStream(targetPath); | |
| 203 | - wb.write(fileOut); | |
| 204 | - fileOut.close(); | |
| 205 | - } catch (Exception e) { | |
| 206 | - e.printStackTrace(); | |
| 207 | - } | |
| 208 | - } | |
| 209 | - | |
| 210 | - /** | |
| 211 | - * 将file1中的一页sheet复制到file2中 | |
| 212 | - * | |
| 213 | - * @param file1 | |
| 214 | - * 原sheet所在的excel文件 | |
| 215 | - * @param file2 | |
| 216 | - * 目标excel文件 | |
| 217 | - * @param page | |
| 218 | - * 原excel中要被复制的sheet的位置(从0开始) | |
| 219 | - * @param rate | |
| 220 | - * 调整复制后的缩放倍率(列如:145,则为缩放145%) | |
| 221 | - */ | |
| 222 | - public void copySheetByFile(File file1, File file2, int page, int rate) { | |
| 223 | - try { | |
| 224 | - // 把源文件放入流中 | |
| 225 | - POIFSFileSystem fs1 = new POIFSFileSystem(new FileInputStream(file1)); | |
| 226 | - HSSFWorkbook wb1 = new HSSFWorkbook(fs1); | |
| 227 | - HSSFSheet sheet = wb1.getSheetAt(page); | |
| 228 | - POIFSFileSystem fs2 = new POIFSFileSystem(new FileInputStream(file2)); | |
| 229 | - HSSFWorkbook wb2 = new HSSFWorkbook(fs2); | |
| 230 | - HSSFSheet createSheet = wb2.createSheet(sheet.getSheetName()); | |
| 231 | - HSSFCellStyle createCellStyle = wb2.createCellStyle(); | |
| 232 | - HSSFRow row; | |
| 233 | - | |
| 234 | - createSheet.setZoom(rate, 100); | |
| 235 | - for(int i = 0; i < sheet.getRow(0).getPhysicalNumberOfCells(); i++){ | |
| 236 | - createSheet.setColumnWidth(i, sheet.getColumnWidth(i)); | |
| 237 | - } | |
| 238 | - | |
| 239 | - List<CellRangeAddress> mergedRegions = sheet.getMergedRegions(); | |
| 240 | - for(int l = 0; l < mergedRegions.size(); l++){ | |
| 241 | - //复制源表中的合并单元格 | |
| 242 | - createSheet.addMergedRegion(mergedRegions.get(l)); | |
| 243 | - } | |
| 244 | - int firstRow = sheet.getFirstRowNum(); | |
| 245 | - int lastRow = sheet.getLastRowNum(); | |
| 246 | - for(int k = firstRow; k <= lastRow; k++){ | |
| 247 | - // 创建新建excel Sheet的行 | |
| 248 | - HSSFRow rowCreat = createSheet.createRow(k); | |
| 249 | - // 取得源有excel Sheet的行 | |
| 250 | - row = sheet.getRow(k); | |
| 251 | -// rowCreat.setHeight(row.getHeight()); //设置行高 | |
| 252 | - // 单元格式样 | |
| 253 | - int firstCell = row.getFirstCellNum(); | |
| 254 | - int lastCell = row.getLastCellNum(); | |
| 255 | - for (int j = firstCell; j < lastCell; j++) { | |
| 256 | - // 自动适应列宽 貌似不起作用 | |
| 257 | -// createSheet.autoSizeColumn(j); | |
| 258 | -// System.out.println(row.getCell(j)); | |
| 259 | - rowCreat.createCell(j); | |
| 260 | - String strVal = ""; | |
| 261 | - if (row.getCell(j)==null) { | |
| 262 | - | |
| 263 | - } else { | |
| 264 | - strVal = row.getCell(j).getStringCellValue(); | |
| 265 | - rowCreat.getCell(j).setCellValue(strVal); | |
| 266 | - copyCellStyle(wb1, row.getCell(j).getCellStyle(), createCellStyle); | |
| 267 | - createCellStyle.setBorderTop((short)1); | |
| 268 | - createCellStyle.setBorderLeft((short)1); | |
| 269 | - createCellStyle.setBorderRight((short)1); | |
| 270 | - createCellStyle.setBorderBottom((short)1); | |
| 271 | - rowCreat.getCell(j).setCellStyle(createCellStyle); | |
| 272 | - } | |
| 273 | - } | |
| 274 | - } | |
| 275 | - | |
| 276 | -// int firstRowNum = createSheet.getFirstRowNum(); | |
| 277 | -// int lastRowNum = createSheet.getLastRowNum(); | |
| 278 | -// int test = 0; | |
| 279 | -// for(int k = firstRowNum; k <= lastRowNum; k++){ | |
| 280 | -// HSSFRow createRow = createSheet.getRow(k); | |
| 281 | -// int firstCellNum = createRow.getFirstCellNum(); | |
| 282 | -// int lastCellNum = createRow.getLastCellNum(); | |
| 283 | -// for(int i = firstCellNum; i < lastCellNum; i++){ | |
| 284 | -// HSSFCell cell = createRow.getCell(i); | |
| 285 | -// cell.getCellStyle().setBorderTop(HSSFCellStyle.BORDER_THIN); | |
| 286 | -// cell.getCellStyle().setBorderLeft(HSSFCellStyle.BORDER_THIN); | |
| 287 | -// cell.getCellStyle().setBorderRight(HSSFCellStyle.BORDER_THIN); | |
| 288 | -// cell.getCellStyle().setBorderBottom(HSSFCellStyle.BORDER_THIN); | |
| 289 | -// test ++; | |
| 290 | -// } | |
| 291 | -// } | |
| 292 | -// System.out.println("test = " + test); | |
| 293 | - | |
| 294 | - FileOutputStream fileOut = new FileOutputStream(file2); | |
| 295 | - wb2.write(fileOut); | |
| 296 | - fileOut.close(); | |
| 297 | - wb2.close(); | |
| 298 | - wb1.close(); | |
| 299 | - fs2.close(); | |
| 300 | - fs1.close(); | |
| 301 | - file1.delete(); | |
| 302 | -// // 创建目标文件夹 | |
| 303 | -// createFolder(targetPath); | |
| 304 | - // 输出文件 | |
| 305 | - } catch (Exception e) { | |
| 306 | - e.printStackTrace(); | |
| 307 | - } | |
| 308 | - } | |
| 309 | - | |
| 310 | - public void test(File file){ | |
| 311 | - POIFSFileSystem fs; | |
| 312 | - try { | |
| 313 | - fs = new POIFSFileSystem(new FileInputStream(file)); | |
| 314 | - HSSFWorkbook wb = new HSSFWorkbook(fs); | |
| 315 | - for(int j = 0; j < wb.getNumberOfSheets(); j++){ | |
| 316 | - HSSFSheet sheet = wb.getSheetAt(j); | |
| 317 | - int firstRowNum = sheet.getFirstRowNum(); | |
| 318 | - int lastRowNum = sheet.getLastRowNum(); | |
| 319 | - int test = 0; | |
| 320 | - for(int k = firstRowNum; k <= lastRowNum; k++){ | |
| 321 | - HSSFRow createRow = sheet.getRow(k); | |
| 322 | - int firstCellNum = createRow.getFirstCellNum(); | |
| 323 | - int lastCellNum = createRow.getLastCellNum(); | |
| 324 | - for(int i = firstCellNum; i < lastCellNum; i++){ | |
| 325 | - HSSFCell cell = createRow.getCell(i); | |
| 326 | - HSSFCellStyle cellStyle = wb.createCellStyle(); | |
| 327 | - | |
| 328 | - cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); | |
| 329 | - cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); | |
| 330 | - cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); | |
| 331 | - cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); | |
| 332 | - cell.setCellStyle(cellStyle); | |
| 333 | - test ++; | |
| 334 | - } | |
| 335 | - } | |
| 336 | - System.out.println("test = " + test); | |
| 337 | - | |
| 338 | - FileOutputStream fileOut = new FileOutputStream(file); | |
| 339 | - wb.write(fileOut); | |
| 340 | - fileOut.close(); | |
| 341 | - } | |
| 342 | - } catch (Exception e) { | |
| 343 | - // TODO Auto-generated catch block | |
| 344 | - e.printStackTrace(); | |
| 345 | - } | |
| 346 | - | |
| 347 | - } | |
| 348 | - | |
| 349 | - public String getCellValue(HSSFCell cell) { | |
| 350 | - int cellType = 0; | |
| 351 | - String result = ""; | |
| 352 | - double d; | |
| 353 | - if(cell != null) { | |
| 354 | - // 获取列数据类型 | |
| 355 | - cellType = cell.getCellType(); | |
| 356 | - // 不同列数据类型,取值方法不同 | |
| 357 | - switch(cellType) { | |
| 358 | - // String | |
| 359 | - case HSSFCell.CELL_TYPE_STRING: | |
| 360 | - result = cell.getStringCellValue().toString(); | |
| 361 | - break; | |
| 362 | - // numeric类型,excel中,日期格式会转成数字格式存储 | |
| 363 | - case HSSFCell.CELL_TYPE_NUMERIC: | |
| 364 | - result = cell.getNumericCellValue()+""; | |
| 365 | - break; | |
| 366 | - // 公式类型 | |
| 367 | - case HSSFCell.CELL_TYPE_FORMULA: | |
| 368 | - result = cell.getCellFormula() + ""; | |
| 369 | - break; | |
| 370 | - // boolean类型 | |
| 371 | - case HSSFCell.CELL_TYPE_BOOLEAN: | |
| 372 | - result = cell.getBooleanCellValue() + ""; | |
| 373 | - break; | |
| 374 | - // 空格 | |
| 375 | - case HSSFCell.CELL_TYPE_BLANK: | |
| 376 | - result = null; | |
| 377 | - break; | |
| 378 | - // 错误值 | |
| 379 | - case HSSFCell.CELL_TYPE_ERROR: | |
| 380 | - result = null; | |
| 381 | - break; | |
| 382 | - default : | |
| 383 | - System.out.println("其它"); | |
| 384 | - break; | |
| 385 | - } | |
| 386 | - } | |
| 387 | - return result; | |
| 388 | - } | |
| 389 | - | |
| 390 | - /** | |
| 391 | - * 根据iterator,以及模板中的标识,填充模板 | |
| 392 | - * | |
| 393 | - * @param wb | |
| 394 | - * @param sheet | |
| 395 | - * @param cell | |
| 396 | - * @param iterator | |
| 397 | - * iterator | |
| 398 | - * @param index | |
| 399 | - * 模板行索引 | |
| 400 | - * @param rowNum | |
| 401 | - * 表格总行数 | |
| 402 | - * @param key | |
| 403 | - * 表格内容 | |
| 404 | - * @return | |
| 405 | - */ | |
| 406 | - private int iteratorFillCellValue(HSSFWorkbook wb, HSSFSheet sheet, | |
| 407 | - HSSFCell cell, Iterator<?> iterator, int index, int rowNum, | |
| 408 | - String key) { | |
| 409 | - int rowCount = 0; | |
| 410 | - Object obj = null; | |
| 411 | - int p = 0; | |
| 412 | - int i = index; | |
| 413 | - HSSFRow newRow = null; | |
| 414 | - int tmpCellNum = 0; | |
| 415 | - int k = 0; | |
| 416 | - int listIndex = 0; | |
| 417 | - // 取得模板行 | |
| 418 | - HSSFRow orgRow = sheet.getRow(index); | |
| 419 | - HSSFCellStyle style= wb.createCellStyle(); | |
| 420 | - try { | |
| 421 | - while (iterator.hasNext()) { | |
| 422 | - // 取得iterator的对象 | |
| 423 | - obj = iterator.next(); | |
| 424 | - // 移动当前编辑行以下的所有行 | |
| 425 | - if (p != 0) { | |
| 426 | - rowNum += 1; | |
| 427 | - i += 1; | |
| 428 | - rowCount += 1;// 增加的总行数 | |
| 429 | - // 把当前行以下的所有行往下移动1行 | |
| 430 | - sheet.shiftRows(i, rowNum, 1); | |
| 431 | - } | |
| 432 | - p = 1; | |
| 433 | - // 创建新行 | |
| 434 | - newRow = sheet.createRow(index + k++); | |
| 435 | - // 把新行的内容换成和模板行一样 | |
| 436 | - copyRow(wb, orgRow, newRow, true,style); | |
| 437 | - tmpCellNum = newRow.getLastCellNum(); | |
| 438 | - for (int l = 0; l < tmpCellNum; l++) { | |
| 439 | - cell = newRow.getCell(l); | |
| 440 | - key = getCellValue(cell); | |
| 441 | - /** | |
| 442 | - * 如果单无格内容为#list#,表示该行是模板行 #list#0_0 | |
| 443 | - * 第一个0表示需要在list中取iterator的索引值 | |
| 444 | - * 第二个0表示在iterator中取的第几个对象,如果不为0,则取下一个对象,而不是沿用前面获取的对象 | |
| 445 | - */ | |
| 446 | - if(key == null || key.equals("")){ | |
| 447 | - obj = iterator.next(); | |
| 448 | - } | |
| 449 | - if (key.indexOf("#list#") != -1 && key.indexOf("_0") == -1) { | |
| 450 | - if (iterator.hasNext()) { | |
| 451 | - obj = iterator.next(); | |
| 452 | - } else { | |
| 453 | - obj = null; | |
| 454 | - } | |
| 455 | - } | |
| 456 | - if (key.trim().indexOf(" ") != -1) { | |
| 457 | - key = key.split(" ")[1]; | |
| 458 | - } | |
| 459 | - getValueAndSetCellValue(cell, key, obj,new String[]{listIndex+""}); | |
| 460 | - } | |
| 461 | - // list的数量 | |
| 462 | - listIndex ++; | |
| 463 | - } | |
| 464 | - } catch (Exception e) { | |
| 465 | - e.printStackTrace(); | |
| 466 | - } | |
| 467 | - return rowCount; | |
| 468 | - } | |
| 469 | - | |
| 470 | - /** | |
| 471 | - * 取到相应的值并填入相应的列中 | |
| 472 | - * | |
| 473 | - * @param cell 列 | |
| 474 | - * @param key 列内容 | |
| 475 | - * @param obj 数据源对象 | |
| 476 | - * @param args 其他参数 数组 | |
| 477 | - * 数组内容: | |
| 478 | - * 0位:在list中的第几条数据 | |
| 479 | - */ | |
| 480 | - private void getValueAndSetCellValue(HSSFCell cell, String key, Object obj, String[] args) { | |
| 481 | - try { | |
| 482 | - // 保有存单元格的内容 | |
| 483 | - String cellValue = key = key.replace("\\n", ""); | |
| 484 | - String tmpKey; | |
| 485 | - // 判断单元格内容是否是公式 | |
| 486 | - if(cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA){ | |
| 487 | - Pattern p=Pattern.compile("(\\d+)"); // | |
| 488 | - Matcher m=p.matcher(key); | |
| 489 | - if(m.find()){ | |
| 490 | - cellValue = key.replace(m.group(1), | |
| 491 | - Integer.valueOf(m.group(1))+Integer.valueOf(args[0])+""); | |
| 492 | - cell.setCellFormula(cellValue); | |
| 493 | - return; | |
| 494 | - } | |
| 495 | - }else{//其他格式 | |
| 496 | - | |
| 497 | - // 循环截取两个$中间的内容,反射出值 | |
| 498 | - while (key.indexOf("$") != -1) { | |
| 499 | - key = key.substring(key.indexOf("$") + 1); | |
| 500 | - // 取两个$中间的内容 | |
| 501 | - tmpKey = key.substring(0, key.indexOf("$")); | |
| 502 | - key = key.substring(key.indexOf("$") + 1); | |
| 503 | - // 如果内容是如下格式Cars.id,则从obj中值得相应的对象的值 | |
| 504 | - if (tmpKey.indexOf(".") != -1) { | |
| 505 | - String className = tmpKey.substring(0, tmpKey.indexOf(".")); | |
| 506 | - // 取得类的全限定名 | |
| 507 | - String classWholeName = packaegName + className; | |
| 508 | - String fieldName = tmpKey.substring(tmpKey.indexOf(".") + 1); | |
| 509 | - // 如果obj是数组,循环判断哪个对象是对应的 | |
| 510 | - if (obj instanceof Object[]) { | |
| 511 | - Object[] objs = (Object[]) obj; | |
| 512 | - for (int k = 0; k < objs.length; k++) { | |
| 513 | - if (objs[k].getClass().getName().equals(classWholeName)) { | |
| 514 | - cellValue = cellValue.replace("$" + tmpKey | |
| 515 | - + "$", getKeyValue(objs[k], fieldName) | |
| 516 | - + ""); | |
| 517 | - } else if(objs[k].getClass().getName().equals("java.util.HashMap")){ | |
| 518 | - Map<String,Object> map = (HashMap<String,Object>)objs[k]; | |
| 519 | - cellValue = cellValue.replace("$" + tmpKey + "$",map.get(fieldName)+""); | |
| 520 | - } | |
| 521 | - } | |
| 522 | - } else if (obj.getClass().getName().equals(classWholeName)) { | |
| 523 | - cellValue = cellValue.replace("$" + tmpKey + "$",getKeyValue(obj, fieldName) + ""); | |
| 524 | - } else if (obj.getClass().getName().equals("java.util.HashMap")){ | |
| 525 | - Map<String,Object> map = (HashMap<String,Object>)obj; | |
| 526 | - cellValue = cellValue.replace("$" + tmpKey + "$",map.get(fieldName)+""); | |
| 527 | - } | |
| 528 | - } | |
| 529 | - } | |
| 530 | - } | |
| 531 | - cell.setCellValue(cellValue); | |
| 532 | - } catch (Exception e) { | |
| 533 | - e.printStackTrace(); | |
| 534 | - } | |
| 535 | - | |
| 536 | - } | |
| 537 | - | |
| 538 | - /** | |
| 539 | - * 给列填充数据 | |
| 540 | - * | |
| 541 | - * @param cell | |
| 542 | - * 列 | |
| 543 | - * @param obj | |
| 544 | - * 数据源对象 | |
| 545 | - * @param fieldName | |
| 546 | - * 需要取数据的字段 | |
| 547 | - */ | |
| 548 | - private Object getKeyValue(Object obj, String fieldName) { | |
| 549 | - Object value = ""; | |
| 550 | - try { | |
| 551 | - if (obj != null) { | |
| 552 | - ReportRelatedUtils test = new ReportRelatedUtils(); | |
| 553 | - value = test.getValue(obj, fieldName) == null ? "" : test .getValue(obj, fieldName); | |
| 554 | - } | |
| 555 | - } catch (Exception e) { | |
| 556 | - e.printStackTrace(); | |
| 557 | - } | |
| 558 | - return value; | |
| 559 | - } | |
| 560 | - | |
| 561 | - public static void main(String[] args) { | |
| 562 | - | |
| 563 | - try { | |
| 564 | - ReportUtils ee = new ReportUtils(); | |
| 565 | - List<Iterator<?>> list = new ArrayList<Iterator<?>>(); | |
| 566 | - Line line = new Line(); | |
| 567 | - line.setId(1); | |
| 568 | - line.setName("line1"); | |
| 569 | - | |
| 570 | - List<Object> dataList = new ArrayList<Object>(); | |
| 571 | - | |
| 572 | - | |
| 573 | - ScheduleRealInfo srr = new ScheduleRealInfo(); | |
| 574 | - srr.setId((long) 111); | |
| 575 | - srr.setXlName("abc11"); | |
| 576 | - | |
| 577 | - ScheduleRealInfo sr = new ScheduleRealInfo(); | |
| 578 | - sr.setZdsj("06:10"); | |
| 579 | - sr.setZdsjActual("06:25"); | |
| 580 | - dataList.add(sr); | |
| 581 | - sr = new ScheduleRealInfo(); | |
| 582 | - sr.setZdsj("06:20"); | |
| 583 | - sr.setZdsjActual(""); | |
| 584 | - dataList.add(sr); | |
| 585 | - list.add(dataList.iterator()); | |
| 586 | - | |
| 587 | - ee.excelReplace(list, new Object[] { srr }, "D:/waybill.xls", | |
| 588 | - "D:/22.xls"); | |
| 589 | - System.out.println("ok"); | |
| 590 | - } catch (Exception e) { | |
| 591 | - e.printStackTrace(); | |
| 592 | - } | |
| 593 | - } | |
| 594 | - | |
| 595 | - /** | |
| 596 | - * 行复制功能 | |
| 597 | - * | |
| 598 | - * @param fromRow | |
| 599 | - * @param toRow | |
| 600 | - */ | |
| 601 | - private void copyRow(HSSFWorkbook wb, HSSFRow fromRow, HSSFRow toRow, | |
| 602 | - boolean copyValueFlag, HSSFCellStyle style) { | |
| 603 | - for (Iterator<Cell> cellIt = fromRow.cellIterator(); cellIt.hasNext();) { | |
| 604 | - HSSFCell tmpCell = (HSSFCell) cellIt.next(); | |
| 605 | - HSSFCell newCell = toRow.createCell(tmpCell.getColumnIndex(), 0); | |
| 606 | - copyCell(wb, tmpCell, newCell, copyValueFlag, tmpCell.getCellStyle()); | |
| 607 | - } | |
| 608 | - } | |
| 609 | - | |
| 610 | - /** | |
| 611 | - * 复制单元格 | |
| 612 | - * | |
| 613 | - * @param srcCell | |
| 614 | - * @param distCell | |
| 615 | - * @param copyValueFlag | |
| 616 | - * true则连同cell的内容一起复制 | |
| 617 | - */ | |
| 618 | - public void copyCell(HSSFWorkbook wb, HSSFCell srcCell, HSSFCell distCell, | |
| 619 | - boolean copyValueFlag, HSSFCellStyle newstyle) { | |
| 620 | -// HSSFCellStyle newstyle = wb.createCellStyle(); | |
| 621 | - copyCellStyle(wb, srcCell.getCellStyle(), newstyle); | |
| 622 | - // 样式 | |
| 623 | - distCell.setCellStyle(newstyle); | |
| 624 | - // 评论 | |
| 625 | - if (srcCell.getCellComment() != null) { | |
| 626 | - distCell.setCellComment(srcCell.getCellComment()); | |
| 627 | - } | |
| 628 | - // 不同数据类型处理 | |
| 629 | - int srcCellType = srcCell.getCellType(); | |
| 630 | - distCell.setCellType(srcCellType); | |
| 631 | - if (copyValueFlag) { | |
| 632 | - if (srcCellType == HSSFCell.CELL_TYPE_NUMERIC) { | |
| 633 | - distCell.setCellValue(srcCell.getDateCellValue()); | |
| 634 | - } else if (srcCellType == HSSFCell.CELL_TYPE_STRING) { | |
| 635 | - distCell.setCellValue(srcCell.getRichStringCellValue()); | |
| 636 | - } else if (srcCellType == HSSFCell.CELL_TYPE_BLANK) { | |
| 637 | - | |
| 638 | - } else if (srcCellType == HSSFCell.CELL_TYPE_BOOLEAN) { | |
| 639 | - distCell.setCellValue(srcCell.getBooleanCellValue()); | |
| 640 | - } else if (srcCellType == HSSFCell.CELL_TYPE_ERROR) { | |
| 641 | - distCell.setCellErrorValue(srcCell.getErrorCellValue()); | |
| 642 | - } else if (srcCellType == HSSFCell.CELL_TYPE_FORMULA) { | |
| 643 | - distCell.setCellFormula(srcCell.getCellFormula()); | |
| 644 | - } else { | |
| 645 | - } | |
| 646 | - } | |
| 647 | - } | |
| 648 | - | |
| 649 | - /** | |
| 650 | - * 复制一个单元格样式到目的单元格样式 | |
| 651 | - * | |
| 652 | - * @param fromStyle | |
| 653 | - * @param toStyle | |
| 654 | - */ | |
| 655 | - public void copyCellStyle(HSSFWorkbook wb, HSSFCellStyle fromStyle, | |
| 656 | - HSSFCellStyle toStyle) { | |
| 657 | - toStyle.setAlignment(fromStyle.getAlignment()); | |
| 658 | - // 边框和边框颜色 | |
| 659 | - toStyle.setBorderBottom(fromStyle.getBorderBottom()); | |
| 660 | - toStyle.setBorderLeft(fromStyle.getBorderLeft()); | |
| 661 | - toStyle.setBorderRight(fromStyle.getBorderRight()); | |
| 662 | - toStyle.setBorderTop(fromStyle.getBorderTop()); | |
| 663 | - toStyle.setTopBorderColor(fromStyle.getTopBorderColor()); | |
| 664 | - toStyle.setBottomBorderColor(fromStyle.getBottomBorderColor()); | |
| 665 | - toStyle.setRightBorderColor(fromStyle.getRightBorderColor()); | |
| 666 | - toStyle.setLeftBorderColor(fromStyle.getLeftBorderColor()); | |
| 667 | - | |
| 668 | - // 背景和前景 | |
| 669 | - toStyle.setFillBackgroundColor(fromStyle.getFillBackgroundColor()); | |
| 670 | - toStyle.setFillForegroundColor(fromStyle.getFillForegroundColor()); | |
| 671 | - | |
| 672 | - toStyle.setDataFormat(fromStyle.getDataFormat()); | |
| 673 | - toStyle.setFillPattern(fromStyle.getFillPattern()); | |
| 674 | - toStyle.setHidden(fromStyle.getHidden()); | |
| 675 | - toStyle.setIndention(fromStyle.getIndention());// 首行缩进 | |
| 676 | - toStyle.setLocked(fromStyle.getLocked()); | |
| 677 | - toStyle.setRotation(fromStyle.getRotation());// 旋转 | |
| 678 | - toStyle.setVerticalAlignment(fromStyle.getVerticalAlignment()); | |
| 679 | - toStyle.setWrapText(fromStyle.getWrapText()); | |
| 680 | - // 字体 | |
| 681 | - toStyle.setFont(fromStyle.getFont(wb)); | |
| 682 | - | |
| 683 | - } | |
| 684 | - | |
| 685 | - /** | |
| 686 | - * 创建文件夹,并删除原有文件 | |
| 687 | - * | |
| 688 | - * @param path | |
| 689 | - */ | |
| 690 | - private void createFolder(String path) { | |
| 691 | - File targetFile = null; | |
| 692 | - targetFile = new File(path); | |
| 693 | - if (targetFile.exists()) {// 删除原有文件 | |
| 694 | - targetFile.delete(); | |
| 695 | - } | |
| 696 | - // 创建目标文件夹 | |
| 697 | - targetFile = new File(path.substring(0, path.lastIndexOf("/"))); | |
| 698 | - if (!targetFile.exists()) { | |
| 699 | - targetFile.mkdirs(); | |
| 700 | - } | |
| 701 | - } | |
| 702 | - | |
| 703 | - public void createFlie(List<List<String>> list, String name, String type){ | |
| 704 | - HSSFWorkbook workbook = new HSSFWorkbook(); | |
| 705 | - // 生成一个样式 | |
| 706 | - HSSFCellStyle style = workbook.createCellStyle(); | |
| 707 | - // 设置这些样式 | |
| 708 | -// style.setFillForegroundColor(HSSFColor.SKY_BLUE.index); | |
| 709 | -// style.setFillPattern(HSSFCellStyle.BORDER_THIN); | |
| 710 | - style.setBorderBottom(HSSFCellStyle.BORDER_THIN); | |
| 711 | - style.setBorderLeft(HSSFCellStyle.BORDER_THIN); | |
| 712 | - style.setBorderRight(HSSFCellStyle.BORDER_THIN); | |
| 713 | - style.setBorderTop(HSSFCellStyle.BORDER_THIN); | |
| 714 | - style.setAlignment(HSSFCellStyle.ALIGN_CENTER); | |
| 715 | - // 生成一个字体 | |
| 716 | - HSSFFont font = workbook.createFont(); | |
| 717 | -// font.setColor(HSSFColor.VIOLET.index); | |
| 718 | - font.setFontHeightInPoints((short) 12); | |
| 719 | - font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); | |
| 720 | - // 把字体应用到当前的样式 | |
| 721 | - style.setFont(font); | |
| 722 | - HSSFCellStyle cellStyle =workbook.createCellStyle(); | |
| 723 | - cellStyle.setFont(font); | |
| 724 | - cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); //水平布局:居中 | |
| 725 | - cellStyle.setWrapText(true); | |
| 726 | - | |
| 727 | - //设置wordsheet名 | |
| 728 | - HSSFSheet sheetYS = workbook.createSheet();//设置wordsheet名 | |
| 729 | - HSSFRow row = sheetYS.createRow(0); | |
| 730 | - setCellStyleAndValue(row, style, 0, name); | |
| 731 | - CellRangeAddress callRangeAddress = new CellRangeAddress(0,0,0,list.get(0).size()-1); | |
| 732 | - sheetYS.addMergedRegion(callRangeAddress); | |
| 733 | - // 样式 | |
| 734 | - setMergeCellStyle (callRangeAddress, sheetYS, workbook); | |
| 735 | - | |
| 736 | - try{ | |
| 737 | - for(int i=0; i<list.size(); i++){ | |
| 738 | - HSSFRow rowYSi = sheetYS.createRow(i+1); | |
| 739 | - List<String> stringList = list.get(i); | |
| 740 | - int num = 4; | |
| 741 | - if("xl".equals(type)) | |
| 742 | - num = 2; | |
| 743 | - else if("cl".equals(type)) | |
| 744 | - num = 5; | |
| 745 | - for(int j=0; j<stringList.size(); j++){ | |
| 746 | - String str = stringList.get(j); | |
| 747 | - if(i == list.size()-1){ | |
| 748 | - if(j==0) { | |
| 749 | - setCellStyleAndValue(rowYSi, style, j, str); | |
| 750 | - CellRangeAddress callRangeAddressYSi = new CellRangeAddress(i+1,i+1,0,num); | |
| 751 | - sheetYS.addMergedRegion(callRangeAddressYSi); | |
| 752 | - // 样式 | |
| 753 | - setMergeCellStyle (callRangeAddressYSi, sheetYS, workbook); | |
| 754 | - }else | |
| 755 | - setCellStyleAndValue(rowYSi,style,j+num,str); | |
| 756 | - } else { | |
| 757 | - setCellStyleAndValue(rowYSi,style,j,str); | |
| 758 | - } | |
| 759 | - } | |
| 760 | - } | |
| 761 | - | |
| 762 | - // 给列设置宽度自适应 | |
| 763 | - setSizeColumn1(sheetYS,1,list.get(0).size()); | |
| 764 | - String path = this.getClass().getResource("/").getPath() + "static/pages/forms/export/"; | |
| 765 | - String targetPath = path+name+".xls"; | |
| 766 | - createFolder(targetPath); | |
| 767 | - FileOutputStream fout = new FileOutputStream(targetPath); | |
| 768 | - //5.输出 | |
| 769 | - workbook.write(fout); | |
| 770 | - fout.close(); | |
| 771 | - } catch (Exception e) { | |
| 772 | - e.printStackTrace(); | |
| 773 | - } | |
| 774 | - } | |
| 775 | - | |
| 776 | - /** | |
| 777 | - * 自适应宽度(中文支持) | |
| 778 | - * @param sheet | |
| 779 | - * @param size | |
| 780 | - */ | |
| 781 | - private static void setSizeColumn(HSSFSheet sheet, int size) { | |
| 782 | - for (int columnNum = 0; columnNum < size; columnNum++) { | |
| 783 | - int columnWidth = sheet.getColumnWidth(columnNum) / 256; | |
| 784 | - for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) { | |
| 785 | - HSSFRow currentRow; | |
| 786 | - //当前行未被使用过 | |
| 787 | - if (sheet.getRow(rowNum) == null) { | |
| 788 | - currentRow = sheet.createRow(rowNum); | |
| 789 | - } else { | |
| 790 | - currentRow = sheet.getRow(rowNum); | |
| 791 | - } | |
| 792 | - if (currentRow.getCell(columnNum) != null) { | |
| 793 | - HSSFCell currentCell = currentRow.getCell(columnNum); | |
| 794 | - if (currentCell.getCellType() == XSSFCell.CELL_TYPE_STRING) { | |
| 795 | - int length = currentCell.getStringCellValue().getBytes().length; | |
| 796 | - if (columnWidth < length) { | |
| 797 | - columnWidth = length; | |
| 798 | - } | |
| 799 | - } | |
| 800 | - } | |
| 801 | - } | |
| 802 | - sheet.setColumnWidth(columnNum, columnWidth * 300); | |
| 803 | -// sheet.setColumnWidth(columnNum, columnWidth * 256); | |
| 804 | - } | |
| 805 | - } | |
| 806 | - | |
| 807 | - /** | |
| 808 | - * 自适应宽度(中文支持) | |
| 809 | - * @param sheet | |
| 810 | - * @param index 从那一行开始自适应 | |
| 811 | - * @param size | |
| 812 | - */ | |
| 813 | - private static void setSizeColumn1(HSSFSheet sheet,int index, int size) { | |
| 814 | - for (int columnNum = index; columnNum < size; columnNum++) { | |
| 815 | - int columnWidth = sheet.getColumnWidth(columnNum) / 256; | |
| 816 | - for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) { | |
| 817 | - HSSFRow currentRow; | |
| 818 | - //当前行未被使用过 | |
| 819 | - if (sheet.getRow(rowNum) == null) { | |
| 820 | - currentRow = sheet.createRow(rowNum); | |
| 821 | - } else { | |
| 822 | - currentRow = sheet.getRow(rowNum); | |
| 823 | - } | |
| 824 | - if (currentRow.getCell(columnNum) != null) { | |
| 825 | - HSSFCell currentCell = currentRow.getCell(columnNum); | |
| 826 | - if (currentCell.getCellType() == XSSFCell.CELL_TYPE_STRING) { | |
| 827 | - int length = currentCell.getStringCellValue().getBytes().length; | |
| 828 | - if (columnWidth < length) { | |
| 829 | - columnWidth = length; | |
| 830 | - } | |
| 831 | - } | |
| 832 | - } | |
| 833 | - } | |
| 834 | - sheet.setColumnWidth(columnNum, columnWidth * 300); | |
| 835 | -// sheet.setColumnWidth(columnNum, columnWidth * 256); | |
| 836 | - } | |
| 837 | - } | |
| 838 | - | |
| 839 | - /** | |
| 840 | - * 设置单元格值和样式 | |
| 841 | - * @param row | |
| 842 | - * @param style | |
| 843 | - * @param index | |
| 844 | - * @param value | |
| 845 | - */ | |
| 846 | - public static void setCellStyleAndValue(HSSFRow row,HSSFCellStyle style,int index,String value){ | |
| 847 | - HSSFCell cell = row.createCell(index); | |
| 848 | - cell.setCellValue(value); | |
| 849 | - cell.setCellStyle(style); | |
| 850 | - } | |
| 851 | - /** | |
| 852 | - * 设置合并单元格样式 | |
| 853 | - * @param cra | |
| 854 | - * @param sheet | |
| 855 | - * @param workbook | |
| 856 | - */ | |
| 857 | - public static void setMergeCellStyle (CellRangeAddress cra, HSSFSheet sheet, Workbook workbook){ | |
| 858 | - // 使用RegionUtil类为合并后的单元格添加边框 | |
| 859 | - RegionUtil.setBorderBottom(1, cra, sheet, workbook); // 下边框 | |
| 860 | - RegionUtil.setBorderLeft(1, cra, sheet, workbook); // 左边框 | |
| 861 | - RegionUtil.setBorderRight(1, cra, sheet, workbook); // 有边框 | |
| 862 | - RegionUtil.setBorderTop(1, cra, sheet, workbook); // 上边框 | |
| 863 | - } | |
| 864 | -} | |
| 1 | +package com.bsth.util; | |
| 2 | + | |
| 3 | +import java.io.File; | |
| 4 | +import java.io.FileInputStream; | |
| 5 | +import java.io.FileOutputStream; | |
| 6 | +import java.util.ArrayList; | |
| 7 | +import java.util.HashMap; | |
| 8 | +import java.util.Iterator; | |
| 9 | +import java.util.List; | |
| 10 | +import java.util.Map; | |
| 11 | +import java.util.TreeMap; | |
| 12 | +import java.util.regex.Matcher; | |
| 13 | +import java.util.regex.Pattern; | |
| 14 | + | |
| 15 | +import com.bsth.common.ResponseCode; | |
| 16 | +import org.apache.commons.lang3.StringUtils; | |
| 17 | +import org.apache.poi.hssf.usermodel.HSSFCell; | |
| 18 | +import org.apache.poi.hssf.usermodel.HSSFCellStyle; | |
| 19 | +import org.apache.poi.hssf.usermodel.HSSFFont; | |
| 20 | +import org.apache.poi.hssf.usermodel.HSSFRow; | |
| 21 | +import org.apache.poi.hssf.usermodel.HSSFSheet; | |
| 22 | +import org.apache.poi.hssf.usermodel.HSSFWorkbook; | |
| 23 | +import org.apache.poi.poifs.filesystem.POIFSFileSystem; | |
| 24 | +import org.apache.poi.ss.usermodel.Cell; | |
| 25 | +import org.apache.poi.ss.usermodel.Workbook; | |
| 26 | +import org.apache.poi.ss.util.CellRangeAddress; | |
| 27 | + | |
| 28 | +import com.bsth.entity.Line; | |
| 29 | +import com.bsth.entity.realcontrol.ScheduleRealInfo; | |
| 30 | +import org.apache.poi.ss.util.RegionUtil; | |
| 31 | +import org.apache.poi.xssf.usermodel.XSSFCell; | |
| 32 | + | |
| 33 | +public class ReportUtils { | |
| 34 | + // private final String packaegName = "com.bsth.entity."; | |
| 35 | + private final String packaegName = "com.bsth.entity.realcontrol."; | |
| 36 | + | |
| 37 | + private final Pattern pattern = Pattern.compile("^\\d+(\\.\\d*)?$"); | |
| 38 | + | |
| 39 | + /** | |
| 40 | + * /** | |
| 41 | + * | |
| 42 | + * @param list | |
| 43 | + * 模板中,需要重复显示的行所需的数据 | |
| 44 | + * @param map | |
| 45 | + * 模板中除以上list外所有的数据 | |
| 46 | + * @param index | |
| 47 | + * 需要重复的行号,该值为行号减1 | |
| 48 | + * @param sourcePath | |
| 49 | + * 模板路径 | |
| 50 | + * @param targetPath | |
| 51 | + * 生成路径 | |
| 52 | + */ | |
| 53 | + public void excelReplace(List<Iterator<?>> list, Object[] tArray, | |
| 54 | + String sourcePath, String targetPath) { | |
| 55 | + try { | |
| 56 | + // 把源文件放入流中 | |
| 57 | + POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream( | |
| 58 | + sourcePath)); | |
| 59 | + HSSFWorkbook wb = new HSSFWorkbook(fs); | |
| 60 | + if(tArray.length != 0 && tArray[0] instanceof java.util.Map){ | |
| 61 | + Map<String, Object> m = (Map<String, Object>)tArray[0]; | |
| 62 | + if(m.containsKey("sheetName") && m.get("sheetName")!=null | |
| 63 | + && m.get("sheetName").toString().trim().length()!=0) | |
| 64 | + wb.setSheetName(0, m.get("sheetName").toString()); | |
| 65 | + } | |
| 66 | + HSSFSheet sheet = wb.getSheetAt(0); | |
| 67 | + HSSFRow row; | |
| 68 | + HSSFCell cell = null; | |
| 69 | + String key; | |
| 70 | + // 取得总行数 | |
| 71 | + int rowNum = sheet.getLastRowNum(); | |
| 72 | + // 取得总列数 | |
| 73 | + int cellNum = sheet.getRow(0).getLastCellNum(); | |
| 74 | + | |
| 75 | + // 遍历行 | |
| 76 | + for (int i = 0; i < rowNum; i++) { | |
| 77 | + row = sheet.getRow(i); | |
| 78 | + // 遍历列 | |
| 79 | + for (int j = 0; j < cellNum; j++) { | |
| 80 | + if (row == null) { | |
| 81 | + continue; | |
| 82 | + } | |
| 83 | + cell = row.getCell(j); | |
| 84 | + if (cell == null) { | |
| 85 | + continue; | |
| 86 | + } | |
| 87 | + // 取得每列的内容,如果列内容是$key$格式,则替换内容 | |
| 88 | + key = getCellValue(cell); | |
| 89 | + if (key != null && (key.indexOf("$") != -1 || key.indexOf("#list#") != -1)) { | |
| 90 | + // * 列中内容有#list#,则表示该行为模板行,需要以该行为模板 | |
| 91 | + // * 例如:模板行格式 #list#0_0 $Car.id$ | |
| 92 | + // * 第一个0表示需要在list中取iterator的索引值 | |
| 93 | + // * 第二个0表示在iterator中取的第几个对象,如果不为0,则取下一个对象,而不是沿用前面获取的对象 | |
| 94 | + // * $Car.id$表示所取的对象为Car的对象,并且取值为id的值 | |
| 95 | + if (key.indexOf("#list#") != -1) { | |
| 96 | + key = key.replace("#list#", "").trim(); | |
| 97 | + String[] lists = key.split(" "); | |
| 98 | + // 取得list中的索引值 | |
| 99 | + int listIndex = Integer | |
| 100 | + .valueOf(lists[0].split("_")[0]); | |
| 101 | + Iterator<?> iterator = list.get(listIndex); | |
| 102 | + // 根据模板创建行并填弃数据,返回增加的行数 | |
| 103 | + int rowCount = iteratorFillCellValue(wb, sheet, | |
| 104 | + cell, iterator, i, rowNum, key); | |
| 105 | + rowNum += rowCount; | |
| 106 | + i += rowCount; | |
| 107 | + break; | |
| 108 | + } else { | |
| 109 | + // 直接填充数据的列,从对象数组中取得值,这里的数组不传值 | |
| 110 | + getValueAndSetCellValue(cell, key, tArray, new String[]{""}); | |
| 111 | + } | |
| 112 | + } | |
| 113 | + | |
| 114 | + } | |
| 115 | + } | |
| 116 | + // 创建目标文件夹 | |
| 117 | + createFolder(targetPath); | |
| 118 | + // 输出文件 | |
| 119 | + FileOutputStream fileOut = new FileOutputStream(targetPath); | |
| 120 | + wb.write(fileOut); | |
| 121 | + fileOut.close(); | |
| 122 | + } catch (Exception e) { | |
| 123 | + e.printStackTrace(); | |
| 124 | + } | |
| 125 | + } | |
| 126 | + | |
| 127 | + | |
| 128 | + /** | |
| 129 | + * | |
| 130 | + * @param sheetList 多sheet模板中,需要重复显示的行所需的数据 | |
| 131 | + * @param tArray | |
| 132 | + * @param sourcePath 模板路径 | |
| 133 | + * @param targetPath 生成路径 | |
| 134 | + */ | |
| 135 | + public void excelMoreSheetReplace(List<List<Iterator<?>>> sheetList, Object[] tArray, | |
| 136 | + String sourcePath, String targetPath) { | |
| 137 | + try { | |
| 138 | + // 把源文件放入流中 | |
| 139 | + POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream( | |
| 140 | + sourcePath)); | |
| 141 | + HSSFWorkbook wb = new HSSFWorkbook(fs); | |
| 142 | + for (int s=0; s<sheetList.size(); s++){ | |
| 143 | + List<Iterator<?>> list = sheetList.get(s); | |
| 144 | + if(tArray.length != 0 && tArray[0] instanceof java.util.Map){ | |
| 145 | + Map<String, Object> m = (Map<String, Object>)tArray[0]; | |
| 146 | + if(m.containsKey("sheetName"+s+1) && m.get("sheetName"+s+1)!=null | |
| 147 | + && m.get("sheetName"+s+1).toString().trim().length()!=0) | |
| 148 | + wb.setSheetName(0, m.get("sheetName"+s+1).toString()); | |
| 149 | + } | |
| 150 | + HSSFSheet sheet = wb.getSheetAt(s); | |
| 151 | + HSSFRow row; | |
| 152 | + HSSFCell cell = null; | |
| 153 | + String key; | |
| 154 | + // 取得总行数 | |
| 155 | + int rowNum = sheet.getLastRowNum(); | |
| 156 | + // 取得总列数 | |
| 157 | + int cellNum = sheet.getRow(0).getLastCellNum(); | |
| 158 | + | |
| 159 | + // 遍历行 | |
| 160 | + for (int i = 0; i <= rowNum; i++) { | |
| 161 | + row = sheet.getRow(i); | |
| 162 | + // 遍历列 | |
| 163 | + for (int j = 0; j < cellNum; j++) { | |
| 164 | + if (row == null) { | |
| 165 | + continue; | |
| 166 | + } | |
| 167 | + cell = row.getCell(j); | |
| 168 | + if (cell == null) { | |
| 169 | + continue; | |
| 170 | + } | |
| 171 | + // 取得每列的内容,如果列内容是$key$格式,则替换内容 | |
| 172 | + key = getCellValue(cell); | |
| 173 | + if (key != null && (key.indexOf("$") != -1 || key.indexOf("#list#") != -1)) { | |
| 174 | + // * 列中内容有#list#,则表示该行为模板行,需要以该行为模板 | |
| 175 | + // * 例如:模板行格式 #list#0_0 $Car.id$ | |
| 176 | + // * 第一个0表示需要在list中取iterator的索引值 | |
| 177 | + // * 第二个0表示在iterator中取的第几个对象,如果不为0,则取下一个对象,而不是沿用前面获取的对象 | |
| 178 | + // * $Car.id$表示所取的对象为Car的对象,并且取值为id的值 | |
| 179 | + if (key.indexOf("#list#") != -1) { | |
| 180 | + key = key.replace("#list#", "").trim(); | |
| 181 | + String[] lists = key.split(" "); | |
| 182 | + // 取得list中的索引值 | |
| 183 | + int listIndex = Integer | |
| 184 | + .valueOf(lists[0].split("_")[0]); | |
| 185 | + Iterator<?> iterator = list.get(listIndex); | |
| 186 | + // 根据模板创建行并填充数据,返回增加的行数 | |
| 187 | + int rowCount = iteratorFillCellValue(wb, sheet, | |
| 188 | + cell, iterator, i, rowNum, key); | |
| 189 | + rowNum += rowCount; | |
| 190 | + i += rowCount; | |
| 191 | + break; | |
| 192 | + } else { | |
| 193 | + // 直接填充数据的列,从对象数组中取得值,这里的数组不传值 | |
| 194 | + getValueAndSetCellValue(cell, key, tArray, new String[]{""}); | |
| 195 | + } | |
| 196 | + } | |
| 197 | + | |
| 198 | + } | |
| 199 | + } | |
| 200 | + } | |
| 201 | + | |
| 202 | + // 创建目标文件夹 | |
| 203 | + createFolder(targetPath); | |
| 204 | + // 输出文件 | |
| 205 | + FileOutputStream fileOut = new FileOutputStream(targetPath); | |
| 206 | + wb.write(fileOut); | |
| 207 | + fileOut.close(); | |
| 208 | + } catch (Exception e) { | |
| 209 | + e.printStackTrace(); | |
| 210 | + } | |
| 211 | + } | |
| 212 | + | |
| 213 | + /** | |
| 214 | + * 将file1中的一页sheet复制到file2中 | |
| 215 | + * | |
| 216 | + * @param file1 | |
| 217 | + * 原sheet所在的excel文件 | |
| 218 | + * @param file2 | |
| 219 | + * 目标excel文件 | |
| 220 | + * @param page | |
| 221 | + * 原excel中要被复制的sheet的位置(从0开始) | |
| 222 | + * @param rate | |
| 223 | + * 调整复制后的缩放倍率(列如:145,则为缩放145%) | |
| 224 | + */ | |
| 225 | + public void copySheetByFile(File file1, File file2, int page, int rate) { | |
| 226 | + try { | |
| 227 | + // 把源文件放入流中 | |
| 228 | + POIFSFileSystem fs1 = new POIFSFileSystem(new FileInputStream(file1)); | |
| 229 | + HSSFWorkbook wb1 = new HSSFWorkbook(fs1); | |
| 230 | + HSSFSheet sheet = wb1.getSheetAt(page); | |
| 231 | + POIFSFileSystem fs2 = new POIFSFileSystem(new FileInputStream(file2)); | |
| 232 | + HSSFWorkbook wb2 = new HSSFWorkbook(fs2); | |
| 233 | + HSSFSheet createSheet = wb2.createSheet(sheet.getSheetName()); | |
| 234 | + HSSFCellStyle createCellStyle = wb2.createCellStyle(); | |
| 235 | + HSSFRow row; | |
| 236 | + | |
| 237 | + createSheet.setZoom(rate, 100); | |
| 238 | + for(int i = 0; i < sheet.getRow(0).getPhysicalNumberOfCells(); i++){ | |
| 239 | + createSheet.setColumnWidth(i, sheet.getColumnWidth(i)); | |
| 240 | + } | |
| 241 | + | |
| 242 | + List<CellRangeAddress> mergedRegions = sheet.getMergedRegions(); | |
| 243 | + for(int l = 0; l < mergedRegions.size(); l++){ | |
| 244 | + //复制源表中的合并单元格 | |
| 245 | + createSheet.addMergedRegion(mergedRegions.get(l)); | |
| 246 | + } | |
| 247 | + int firstRow = sheet.getFirstRowNum(); | |
| 248 | + int lastRow = sheet.getLastRowNum(); | |
| 249 | + for(int k = firstRow; k <= lastRow; k++){ | |
| 250 | + // 创建新建excel Sheet的行 | |
| 251 | + HSSFRow rowCreat = createSheet.createRow(k); | |
| 252 | + // 取得源有excel Sheet的行 | |
| 253 | + row = sheet.getRow(k); | |
| 254 | +// rowCreat.setHeight(row.getHeight()); //设置行高 | |
| 255 | + // 单元格式样 | |
| 256 | + int firstCell = row.getFirstCellNum(); | |
| 257 | + int lastCell = row.getLastCellNum(); | |
| 258 | + for (int j = firstCell; j < lastCell; j++) { | |
| 259 | + // 自动适应列宽 貌似不起作用 | |
| 260 | +// createSheet.autoSizeColumn(j); | |
| 261 | +// System.out.println(row.getCell(j)); | |
| 262 | + rowCreat.createCell(j); | |
| 263 | + String strVal = ""; | |
| 264 | + if (row.getCell(j)==null) { | |
| 265 | + | |
| 266 | + } else { | |
| 267 | + strVal = row.getCell(j).getStringCellValue(); | |
| 268 | + rowCreat.getCell(j).setCellValue(strVal); | |
| 269 | + copyCellStyle(wb1, row.getCell(j).getCellStyle(), createCellStyle); | |
| 270 | + createCellStyle.setBorderTop((short)1); | |
| 271 | + createCellStyle.setBorderLeft((short)1); | |
| 272 | + createCellStyle.setBorderRight((short)1); | |
| 273 | + createCellStyle.setBorderBottom((short)1); | |
| 274 | + rowCreat.getCell(j).setCellStyle(createCellStyle); | |
| 275 | + } | |
| 276 | + } | |
| 277 | + } | |
| 278 | + | |
| 279 | +// int firstRowNum = createSheet.getFirstRowNum(); | |
| 280 | +// int lastRowNum = createSheet.getLastRowNum(); | |
| 281 | +// int test = 0; | |
| 282 | +// for(int k = firstRowNum; k <= lastRowNum; k++){ | |
| 283 | +// HSSFRow createRow = createSheet.getRow(k); | |
| 284 | +// int firstCellNum = createRow.getFirstCellNum(); | |
| 285 | +// int lastCellNum = createRow.getLastCellNum(); | |
| 286 | +// for(int i = firstCellNum; i < lastCellNum; i++){ | |
| 287 | +// HSSFCell cell = createRow.getCell(i); | |
| 288 | +// cell.getCellStyle().setBorderTop(HSSFCellStyle.BORDER_THIN); | |
| 289 | +// cell.getCellStyle().setBorderLeft(HSSFCellStyle.BORDER_THIN); | |
| 290 | +// cell.getCellStyle().setBorderRight(HSSFCellStyle.BORDER_THIN); | |
| 291 | +// cell.getCellStyle().setBorderBottom(HSSFCellStyle.BORDER_THIN); | |
| 292 | +// test ++; | |
| 293 | +// } | |
| 294 | +// } | |
| 295 | +// System.out.println("test = " + test); | |
| 296 | + | |
| 297 | + FileOutputStream fileOut = new FileOutputStream(file2); | |
| 298 | + wb2.write(fileOut); | |
| 299 | + fileOut.close(); | |
| 300 | + wb2.close(); | |
| 301 | + wb1.close(); | |
| 302 | + fs2.close(); | |
| 303 | + fs1.close(); | |
| 304 | + file1.delete(); | |
| 305 | +// // 创建目标文件夹 | |
| 306 | +// createFolder(targetPath); | |
| 307 | + // 输出文件 | |
| 308 | + } catch (Exception e) { | |
| 309 | + e.printStackTrace(); | |
| 310 | + } | |
| 311 | + } | |
| 312 | + | |
| 313 | + public void test(File file){ | |
| 314 | + POIFSFileSystem fs; | |
| 315 | + try { | |
| 316 | + fs = new POIFSFileSystem(new FileInputStream(file)); | |
| 317 | + HSSFWorkbook wb = new HSSFWorkbook(fs); | |
| 318 | + for(int j = 0; j < wb.getNumberOfSheets(); j++){ | |
| 319 | + HSSFSheet sheet = wb.getSheetAt(j); | |
| 320 | + int firstRowNum = sheet.getFirstRowNum(); | |
| 321 | + int lastRowNum = sheet.getLastRowNum(); | |
| 322 | + int test = 0; | |
| 323 | + for(int k = firstRowNum; k <= lastRowNum; k++){ | |
| 324 | + HSSFRow createRow = sheet.getRow(k); | |
| 325 | + int firstCellNum = createRow.getFirstCellNum(); | |
| 326 | + int lastCellNum = createRow.getLastCellNum(); | |
| 327 | + for(int i = firstCellNum; i < lastCellNum; i++){ | |
| 328 | + HSSFCell cell = createRow.getCell(i); | |
| 329 | + HSSFCellStyle cellStyle = wb.createCellStyle(); | |
| 330 | + | |
| 331 | + cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); | |
| 332 | + cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); | |
| 333 | + cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); | |
| 334 | + cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); | |
| 335 | + cell.setCellStyle(cellStyle); | |
| 336 | + test ++; | |
| 337 | + } | |
| 338 | + } | |
| 339 | + System.out.println("test = " + test); | |
| 340 | + | |
| 341 | + FileOutputStream fileOut = new FileOutputStream(file); | |
| 342 | + wb.write(fileOut); | |
| 343 | + fileOut.close(); | |
| 344 | + } | |
| 345 | + } catch (Exception e) { | |
| 346 | + // TODO Auto-generated catch block | |
| 347 | + e.printStackTrace(); | |
| 348 | + } | |
| 349 | + | |
| 350 | + } | |
| 351 | + | |
| 352 | + public String getCellValue(HSSFCell cell) { | |
| 353 | + int cellType = 0; | |
| 354 | + String result = ""; | |
| 355 | + double d; | |
| 356 | + if(cell != null) { | |
| 357 | + // 获取列数据类型 | |
| 358 | + cellType = cell.getCellType(); | |
| 359 | + // 不同列数据类型,取值方法不同 | |
| 360 | + switch(cellType) { | |
| 361 | + // String | |
| 362 | + case HSSFCell.CELL_TYPE_STRING: | |
| 363 | + result = cell.getStringCellValue().toString(); | |
| 364 | + break; | |
| 365 | + // numeric类型,excel中,日期格式会转成数字格式存储 | |
| 366 | + case HSSFCell.CELL_TYPE_NUMERIC: | |
| 367 | + result = cell.getNumericCellValue()+""; | |
| 368 | + break; | |
| 369 | + // 公式类型 | |
| 370 | + case HSSFCell.CELL_TYPE_FORMULA: | |
| 371 | + result = cell.getCellFormula() + ""; | |
| 372 | + break; | |
| 373 | + // boolean类型 | |
| 374 | + case HSSFCell.CELL_TYPE_BOOLEAN: | |
| 375 | + result = cell.getBooleanCellValue() + ""; | |
| 376 | + break; | |
| 377 | + // 空格 | |
| 378 | + case HSSFCell.CELL_TYPE_BLANK: | |
| 379 | + result = null; | |
| 380 | + break; | |
| 381 | + // 错误值 | |
| 382 | + case HSSFCell.CELL_TYPE_ERROR: | |
| 383 | + result = null; | |
| 384 | + break; | |
| 385 | + default : | |
| 386 | + System.out.println("其它"); | |
| 387 | + break; | |
| 388 | + } | |
| 389 | + } | |
| 390 | + return result; | |
| 391 | + } | |
| 392 | + | |
| 393 | + /** | |
| 394 | + * 根据iterator,以及模板中的标识,填充模板 | |
| 395 | + * | |
| 396 | + * @param wb | |
| 397 | + * @param sheet | |
| 398 | + * @param cell | |
| 399 | + * @param iterator | |
| 400 | + * iterator | |
| 401 | + * @param index | |
| 402 | + * 模板行索引 | |
| 403 | + * @param rowNum | |
| 404 | + * 表格总行数 | |
| 405 | + * @param key | |
| 406 | + * 表格内容 | |
| 407 | + * @return | |
| 408 | + */ | |
| 409 | + private int iteratorFillCellValue(HSSFWorkbook wb, HSSFSheet sheet, | |
| 410 | + HSSFCell cell, Iterator<?> iterator, int index, int rowNum, | |
| 411 | + String key) { | |
| 412 | + int rowCount = 0; | |
| 413 | + Object obj = null; | |
| 414 | + int p = 0; | |
| 415 | + int i = index; | |
| 416 | + HSSFRow newRow = null; | |
| 417 | + int tmpCellNum = 0; | |
| 418 | + int k = 0; | |
| 419 | + int listIndex = 0; | |
| 420 | + // 取得模板行 | |
| 421 | + HSSFRow orgRow = sheet.getRow(index); | |
| 422 | + HSSFCellStyle style= wb.createCellStyle(); | |
| 423 | + try { | |
| 424 | + while (iterator.hasNext()) { | |
| 425 | + // 取得iterator的对象 | |
| 426 | + obj = iterator.next(); | |
| 427 | + // 移动当前编辑行以下的所有行 | |
| 428 | + if (p != 0) { | |
| 429 | + rowNum += 1; | |
| 430 | + i += 1; | |
| 431 | + rowCount += 1;// 增加的总行数 | |
| 432 | + // 把当前行以下的所有行往下移动1行 | |
| 433 | + sheet.shiftRows(i, rowNum, 1); | |
| 434 | + } | |
| 435 | + p = 1; | |
| 436 | + // 创建新行 | |
| 437 | + newRow = sheet.createRow(index + k++); | |
| 438 | + // 把新行的内容换成和模板行一样 | |
| 439 | + copyRow(wb, orgRow, newRow, true,style); | |
| 440 | + tmpCellNum = newRow.getLastCellNum(); | |
| 441 | + for (int l = 0; l < tmpCellNum; l++) { | |
| 442 | + cell = newRow.getCell(l); | |
| 443 | + key = getCellValue(cell); | |
| 444 | + /** | |
| 445 | + * 如果单无格内容为#list#,表示该行是模板行 #list#0_0 | |
| 446 | + * 第一个0表示需要在list中取iterator的索引值 | |
| 447 | + * 第二个0表示在iterator中取的第几个对象,如果不为0,则取下一个对象,而不是沿用前面获取的对象 | |
| 448 | + */ | |
| 449 | + if(key == null || key.equals("")){ | |
| 450 | + obj = iterator.next(); | |
| 451 | + } | |
| 452 | + if (key.indexOf("#list#") != -1 && key.indexOf("_0") == -1) { | |
| 453 | + if (iterator.hasNext()) { | |
| 454 | + obj = iterator.next(); | |
| 455 | + } else { | |
| 456 | + obj = null; | |
| 457 | + } | |
| 458 | + } | |
| 459 | + if (key.trim().indexOf(" ") != -1) { | |
| 460 | + key = key.split(" ")[1]; | |
| 461 | + } | |
| 462 | + getValueAndSetCellValue(cell, key, obj,new String[]{listIndex+""}); | |
| 463 | + } | |
| 464 | + // list的数量 | |
| 465 | + listIndex ++; | |
| 466 | + } | |
| 467 | + } catch (Exception e) { | |
| 468 | + e.printStackTrace(); | |
| 469 | + } | |
| 470 | + return rowCount; | |
| 471 | + } | |
| 472 | + | |
| 473 | + /** | |
| 474 | + * 取到相应的值并填入相应的列中 | |
| 475 | + * | |
| 476 | + * @param cell 列 | |
| 477 | + * @param key 列内容 | |
| 478 | + * @param obj 数据源对象 | |
| 479 | + * @param args 其他参数 数组 | |
| 480 | + * 数组内容: | |
| 481 | + * 0位:在list中的第几条数据 | |
| 482 | + */ | |
| 483 | + private void getValueAndSetCellValue(HSSFCell cell, String key, Object obj, String[] args) { | |
| 484 | + try { | |
| 485 | + // 保有存单元格的内容 | |
| 486 | + String cellValue = key = key.replace("\\n", ""); | |
| 487 | + String tmpKey; | |
| 488 | + // 判断单元格内容是否是公式 | |
| 489 | + if(cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA){ | |
| 490 | + Pattern p=Pattern.compile("(\\d+)"); // | |
| 491 | + Matcher m=p.matcher(key); | |
| 492 | + if(m.find()){ | |
| 493 | + cellValue = key.replace(m.group(1), | |
| 494 | + Integer.valueOf(m.group(1))+Integer.valueOf(args[0])+""); | |
| 495 | + cell.setCellFormula(cellValue); | |
| 496 | + return; | |
| 497 | + } | |
| 498 | + }else{//其他格式 | |
| 499 | + | |
| 500 | + // 循环截取两个$中间的内容,反射出值 | |
| 501 | + while (key.indexOf("$") != -1) { | |
| 502 | + key = key.substring(key.indexOf("$") + 1); | |
| 503 | + // 取两个$中间的内容 | |
| 504 | + tmpKey = key.substring(0, key.indexOf("$")); | |
| 505 | + key = key.substring(key.indexOf("$") + 1); | |
| 506 | + // 如果内容是如下格式Cars.id,则从obj中值得相应的对象的值 | |
| 507 | + if (tmpKey.indexOf(".") != -1) { | |
| 508 | + String className = tmpKey.substring(0, tmpKey.indexOf(".")); | |
| 509 | + // 取得类的全限定名 | |
| 510 | + String classWholeName = packaegName + className; | |
| 511 | + String fieldName = tmpKey.substring(tmpKey.indexOf(".") + 1); | |
| 512 | + // 如果obj是数组,循环判断哪个对象是对应的 | |
| 513 | + if (obj instanceof Object[]) { | |
| 514 | + Object[] objs = (Object[]) obj; | |
| 515 | + for (int k = 0; k < objs.length; k++) { | |
| 516 | + if (objs[k].getClass().getName().equals(classWholeName)) { | |
| 517 | + cellValue = cellValue.replace("$" + tmpKey | |
| 518 | + + "$", getKeyValue(objs[k], fieldName) | |
| 519 | + + ""); | |
| 520 | + } else if(objs[k].getClass().getName().equals("java.util.HashMap")){ | |
| 521 | + Map<String,Object> map = (HashMap<String,Object>)objs[k]; | |
| 522 | + cellValue = cellValue.replace("$" + tmpKey + "$",map.get(fieldName)+""); | |
| 523 | + } | |
| 524 | + } | |
| 525 | + } else if (obj.getClass().getName().equals(classWholeName)) { | |
| 526 | + cellValue = cellValue.replace("$" + tmpKey + "$",getKeyValue(obj, fieldName) + ""); | |
| 527 | + } else if (obj.getClass().getName().equals("java.util.HashMap")){ | |
| 528 | + Map<String,Object> map = (HashMap<String,Object>)obj; | |
| 529 | + cellValue = cellValue.replace("$" + tmpKey + "$",map.get(fieldName)+""); | |
| 530 | + } | |
| 531 | + } | |
| 532 | + } | |
| 533 | + } | |
| 534 | + cell.setCellValue(cellValue); | |
| 535 | + Matcher matcher = pattern.matcher(cellValue); | |
| 536 | + if (matcher.matches()) { | |
| 537 | + if (cellValue.indexOf(".") > -1) { | |
| 538 | + cell.setCellValue(Double.parseDouble(cellValue)); | |
| 539 | + } else { | |
| 540 | + cell.setCellValue(Integer.parseInt(cellValue)); | |
| 541 | + } | |
| 542 | + } | |
| 543 | + } catch (Exception e) { | |
| 544 | + e.printStackTrace(); | |
| 545 | + } | |
| 546 | + | |
| 547 | + } | |
| 548 | + | |
| 549 | + /** | |
| 550 | + * 给列填充数据 | |
| 551 | + * | |
| 552 | + * @param cell | |
| 553 | + * 列 | |
| 554 | + * @param obj | |
| 555 | + * 数据源对象 | |
| 556 | + * @param fieldName | |
| 557 | + * 需要取数据的字段 | |
| 558 | + */ | |
| 559 | + private Object getKeyValue(Object obj, String fieldName) { | |
| 560 | + Object value = ""; | |
| 561 | + try { | |
| 562 | + if (obj != null) { | |
| 563 | + ReportRelatedUtils test = new ReportRelatedUtils(); | |
| 564 | + value = test.getValue(obj, fieldName) == null ? "" : test .getValue(obj, fieldName); | |
| 565 | + } | |
| 566 | + } catch (Exception e) { | |
| 567 | + e.printStackTrace(); | |
| 568 | + } | |
| 569 | + return value; | |
| 570 | + } | |
| 571 | + | |
| 572 | + public static void main(String[] args) { | |
| 573 | + | |
| 574 | + try { | |
| 575 | + ReportUtils ee = new ReportUtils(); | |
| 576 | + List<Iterator<?>> list = new ArrayList<Iterator<?>>(); | |
| 577 | + Line line = new Line(); | |
| 578 | + line.setId(1); | |
| 579 | + line.setName("line1"); | |
| 580 | + | |
| 581 | + List<Object> dataList = new ArrayList<Object>(); | |
| 582 | + | |
| 583 | + | |
| 584 | + ScheduleRealInfo srr = new ScheduleRealInfo(); | |
| 585 | + srr.setId((long) 111); | |
| 586 | + srr.setXlName("abc11"); | |
| 587 | + | |
| 588 | + ScheduleRealInfo sr = new ScheduleRealInfo(); | |
| 589 | + sr.setZdsj("06:10"); | |
| 590 | + sr.setZdsjActual("06:25"); | |
| 591 | + dataList.add(sr); | |
| 592 | + sr = new ScheduleRealInfo(); | |
| 593 | + sr.setZdsj("06:20"); | |
| 594 | + sr.setZdsjActual(""); | |
| 595 | + dataList.add(sr); | |
| 596 | + list.add(dataList.iterator()); | |
| 597 | + | |
| 598 | + ee.excelReplace(list, new Object[] { srr }, "D:/waybill.xls", | |
| 599 | + "D:/22.xls"); | |
| 600 | + System.out.println("ok"); | |
| 601 | + } catch (Exception e) { | |
| 602 | + e.printStackTrace(); | |
| 603 | + } | |
| 604 | + } | |
| 605 | + | |
| 606 | + /** | |
| 607 | + * 行复制功能 | |
| 608 | + * | |
| 609 | + * @param fromRow | |
| 610 | + * @param toRow | |
| 611 | + */ | |
| 612 | + private void copyRow(HSSFWorkbook wb, HSSFRow fromRow, HSSFRow toRow, | |
| 613 | + boolean copyValueFlag, HSSFCellStyle style) { | |
| 614 | + for (Iterator<Cell> cellIt = fromRow.cellIterator(); cellIt.hasNext();) { | |
| 615 | + HSSFCell tmpCell = (HSSFCell) cellIt.next(); | |
| 616 | + HSSFCell newCell = toRow.createCell(tmpCell.getColumnIndex(), 0); | |
| 617 | + copyCell(wb, tmpCell, newCell, copyValueFlag, tmpCell.getCellStyle()); | |
| 618 | + } | |
| 619 | + } | |
| 620 | + | |
| 621 | + /** | |
| 622 | + * 复制单元格 | |
| 623 | + * | |
| 624 | + * @param srcCell | |
| 625 | + * @param distCell | |
| 626 | + * @param copyValueFlag | |
| 627 | + * true则连同cell的内容一起复制 | |
| 628 | + */ | |
| 629 | + public void copyCell(HSSFWorkbook wb, HSSFCell srcCell, HSSFCell distCell, | |
| 630 | + boolean copyValueFlag, HSSFCellStyle newstyle) { | |
| 631 | +// HSSFCellStyle newstyle = wb.createCellStyle(); | |
| 632 | + copyCellStyle(wb, srcCell.getCellStyle(), newstyle); | |
| 633 | + // 样式 | |
| 634 | + distCell.setCellStyle(newstyle); | |
| 635 | + // 评论 | |
| 636 | + if (srcCell.getCellComment() != null) { | |
| 637 | + distCell.setCellComment(srcCell.getCellComment()); | |
| 638 | + } | |
| 639 | + // 不同数据类型处理 | |
| 640 | + int srcCellType = srcCell.getCellType(); | |
| 641 | + distCell.setCellType(srcCellType); | |
| 642 | + if (copyValueFlag) { | |
| 643 | + if (srcCellType == HSSFCell.CELL_TYPE_NUMERIC) { | |
| 644 | + distCell.setCellValue(srcCell.getDateCellValue()); | |
| 645 | + } else if (srcCellType == HSSFCell.CELL_TYPE_STRING) { | |
| 646 | + distCell.setCellValue(srcCell.getRichStringCellValue()); | |
| 647 | + } else if (srcCellType == HSSFCell.CELL_TYPE_BLANK) { | |
| 648 | + | |
| 649 | + } else if (srcCellType == HSSFCell.CELL_TYPE_BOOLEAN) { | |
| 650 | + distCell.setCellValue(srcCell.getBooleanCellValue()); | |
| 651 | + } else if (srcCellType == HSSFCell.CELL_TYPE_ERROR) { | |
| 652 | + distCell.setCellErrorValue(srcCell.getErrorCellValue()); | |
| 653 | + } else if (srcCellType == HSSFCell.CELL_TYPE_FORMULA) { | |
| 654 | + distCell.setCellFormula(srcCell.getCellFormula()); | |
| 655 | + } else { | |
| 656 | + } | |
| 657 | + } | |
| 658 | + } | |
| 659 | + | |
| 660 | + /** | |
| 661 | + * 复制一个单元格样式到目的单元格样式 | |
| 662 | + * | |
| 663 | + * @param fromStyle | |
| 664 | + * @param toStyle | |
| 665 | + */ | |
| 666 | + public void copyCellStyle(HSSFWorkbook wb, HSSFCellStyle fromStyle, | |
| 667 | + HSSFCellStyle toStyle) { | |
| 668 | + toStyle.setAlignment(fromStyle.getAlignment()); | |
| 669 | + // 边框和边框颜色 | |
| 670 | + toStyle.setBorderBottom(fromStyle.getBorderBottom()); | |
| 671 | + toStyle.setBorderLeft(fromStyle.getBorderLeft()); | |
| 672 | + toStyle.setBorderRight(fromStyle.getBorderRight()); | |
| 673 | + toStyle.setBorderTop(fromStyle.getBorderTop()); | |
| 674 | + toStyle.setTopBorderColor(fromStyle.getTopBorderColor()); | |
| 675 | + toStyle.setBottomBorderColor(fromStyle.getBottomBorderColor()); | |
| 676 | + toStyle.setRightBorderColor(fromStyle.getRightBorderColor()); | |
| 677 | + toStyle.setLeftBorderColor(fromStyle.getLeftBorderColor()); | |
| 678 | + | |
| 679 | + // 背景和前景 | |
| 680 | + toStyle.setFillBackgroundColor(fromStyle.getFillBackgroundColor()); | |
| 681 | + toStyle.setFillForegroundColor(fromStyle.getFillForegroundColor()); | |
| 682 | + | |
| 683 | + toStyle.setDataFormat(fromStyle.getDataFormat()); | |
| 684 | + toStyle.setFillPattern(fromStyle.getFillPattern()); | |
| 685 | + toStyle.setHidden(fromStyle.getHidden()); | |
| 686 | + toStyle.setIndention(fromStyle.getIndention());// 首行缩进 | |
| 687 | + toStyle.setLocked(fromStyle.getLocked()); | |
| 688 | + toStyle.setRotation(fromStyle.getRotation());// 旋转 | |
| 689 | + toStyle.setVerticalAlignment(fromStyle.getVerticalAlignment()); | |
| 690 | + toStyle.setWrapText(fromStyle.getWrapText()); | |
| 691 | + // 字体 | |
| 692 | + toStyle.setFont(fromStyle.getFont(wb)); | |
| 693 | + | |
| 694 | + } | |
| 695 | + | |
| 696 | + /** | |
| 697 | + * 创建文件夹,并删除原有文件 | |
| 698 | + * | |
| 699 | + * @param path | |
| 700 | + */ | |
| 701 | + private void createFolder(String path) { | |
| 702 | + File targetFile = null; | |
| 703 | + targetFile = new File(path); | |
| 704 | + if (targetFile.exists()) {// 删除原有文件 | |
| 705 | + targetFile.delete(); | |
| 706 | + } | |
| 707 | + // 创建目标文件夹 | |
| 708 | + targetFile = new File(path.substring(0, path.lastIndexOf("/"))); | |
| 709 | + if (!targetFile.exists()) { | |
| 710 | + targetFile.mkdirs(); | |
| 711 | + } | |
| 712 | + } | |
| 713 | + | |
| 714 | + public void createFlie(List<List<String>> list, String name, String type){ | |
| 715 | + HSSFWorkbook workbook = new HSSFWorkbook(); | |
| 716 | + // 生成一个样式 | |
| 717 | + HSSFCellStyle style = workbook.createCellStyle(); | |
| 718 | + // 设置这些样式 | |
| 719 | +// style.setFillForegroundColor(HSSFColor.SKY_BLUE.index); | |
| 720 | +// style.setFillPattern(HSSFCellStyle.BORDER_THIN); | |
| 721 | + style.setBorderBottom(HSSFCellStyle.BORDER_THIN); | |
| 722 | + style.setBorderLeft(HSSFCellStyle.BORDER_THIN); | |
| 723 | + style.setBorderRight(HSSFCellStyle.BORDER_THIN); | |
| 724 | + style.setBorderTop(HSSFCellStyle.BORDER_THIN); | |
| 725 | + style.setAlignment(HSSFCellStyle.ALIGN_CENTER); | |
| 726 | + // 生成一个字体 | |
| 727 | + HSSFFont font = workbook.createFont(); | |
| 728 | +// font.setColor(HSSFColor.VIOLET.index); | |
| 729 | + font.setFontHeightInPoints((short) 12); | |
| 730 | + font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); | |
| 731 | + // 把字体应用到当前的样式 | |
| 732 | + style.setFont(font); | |
| 733 | + HSSFCellStyle cellStyle =workbook.createCellStyle(); | |
| 734 | + cellStyle.setFont(font); | |
| 735 | + cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); //水平布局:居中 | |
| 736 | + cellStyle.setWrapText(true); | |
| 737 | + | |
| 738 | + //设置wordsheet名 | |
| 739 | + HSSFSheet sheetYS = workbook.createSheet();//设置wordsheet名 | |
| 740 | + HSSFRow row = sheetYS.createRow(0); | |
| 741 | + setCellStyleAndValue(row, style, 0, name); | |
| 742 | + CellRangeAddress callRangeAddress = new CellRangeAddress(0,0,0,list.get(0).size()-1); | |
| 743 | + sheetYS.addMergedRegion(callRangeAddress); | |
| 744 | + // 样式 | |
| 745 | + setMergeCellStyle (callRangeAddress, sheetYS, workbook); | |
| 746 | + | |
| 747 | + try{ | |
| 748 | + for(int i=0; i<list.size(); i++){ | |
| 749 | + HSSFRow rowYSi = sheetYS.createRow(i+1); | |
| 750 | + List<String> stringList = list.get(i); | |
| 751 | + int num = 4; | |
| 752 | + if("xl".equals(type)) | |
| 753 | + num = 2; | |
| 754 | + else if("cl".equals(type)) | |
| 755 | + num = 5; | |
| 756 | + for(int j=0; j<stringList.size(); j++){ | |
| 757 | + String str = stringList.get(j); | |
| 758 | + if(i == list.size()-1){ | |
| 759 | + if(j==0) { | |
| 760 | + setCellStyleAndValue(rowYSi, style, j, str); | |
| 761 | + CellRangeAddress callRangeAddressYSi = new CellRangeAddress(i+1,i+1,0,num); | |
| 762 | + sheetYS.addMergedRegion(callRangeAddressYSi); | |
| 763 | + // 样式 | |
| 764 | + setMergeCellStyle (callRangeAddressYSi, sheetYS, workbook); | |
| 765 | + }else | |
| 766 | + setCellStyleAndValue(rowYSi,style,j+num,str); | |
| 767 | + } else { | |
| 768 | + setCellStyleAndValue(rowYSi,style,j,str); | |
| 769 | + } | |
| 770 | + } | |
| 771 | + } | |
| 772 | + | |
| 773 | + // 给列设置宽度自适应 | |
| 774 | + setSizeColumn1(sheetYS,1,list.get(0).size()); | |
| 775 | + String path = this.getClass().getResource("/").getPath() + "static/pages/forms/export/"; | |
| 776 | + String targetPath = path+name+".xls"; | |
| 777 | + createFolder(targetPath); | |
| 778 | + FileOutputStream fout = new FileOutputStream(targetPath); | |
| 779 | + //5.输出 | |
| 780 | + workbook.write(fout); | |
| 781 | + fout.close(); | |
| 782 | + } catch (Exception e) { | |
| 783 | + e.printStackTrace(); | |
| 784 | + } | |
| 785 | + } | |
| 786 | + | |
| 787 | + /** | |
| 788 | + * 自适应宽度(中文支持) | |
| 789 | + * @param sheet | |
| 790 | + * @param size | |
| 791 | + */ | |
| 792 | + private static void setSizeColumn(HSSFSheet sheet, int size) { | |
| 793 | + for (int columnNum = 0; columnNum < size; columnNum++) { | |
| 794 | + int columnWidth = sheet.getColumnWidth(columnNum) / 256; | |
| 795 | + for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) { | |
| 796 | + HSSFRow currentRow; | |
| 797 | + //当前行未被使用过 | |
| 798 | + if (sheet.getRow(rowNum) == null) { | |
| 799 | + currentRow = sheet.createRow(rowNum); | |
| 800 | + } else { | |
| 801 | + currentRow = sheet.getRow(rowNum); | |
| 802 | + } | |
| 803 | + if (currentRow.getCell(columnNum) != null) { | |
| 804 | + HSSFCell currentCell = currentRow.getCell(columnNum); | |
| 805 | + if (currentCell.getCellType() == XSSFCell.CELL_TYPE_STRING) { | |
| 806 | + int length = currentCell.getStringCellValue().getBytes().length; | |
| 807 | + if (columnWidth < length) { | |
| 808 | + columnWidth = length; | |
| 809 | + } | |
| 810 | + } | |
| 811 | + } | |
| 812 | + } | |
| 813 | + sheet.setColumnWidth(columnNum, columnWidth * 300); | |
| 814 | +// sheet.setColumnWidth(columnNum, columnWidth * 256); | |
| 815 | + } | |
| 816 | + } | |
| 817 | + | |
| 818 | + /** | |
| 819 | + * 自适应宽度(中文支持) | |
| 820 | + * @param sheet | |
| 821 | + * @param index 从那一行开始自适应 | |
| 822 | + * @param size | |
| 823 | + */ | |
| 824 | + private static void setSizeColumn1(HSSFSheet sheet,int index, int size) { | |
| 825 | + for (int columnNum = index; columnNum < size; columnNum++) { | |
| 826 | + int columnWidth = sheet.getColumnWidth(columnNum) / 256; | |
| 827 | + for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) { | |
| 828 | + HSSFRow currentRow; | |
| 829 | + //当前行未被使用过 | |
| 830 | + if (sheet.getRow(rowNum) == null) { | |
| 831 | + currentRow = sheet.createRow(rowNum); | |
| 832 | + } else { | |
| 833 | + currentRow = sheet.getRow(rowNum); | |
| 834 | + } | |
| 835 | + if (currentRow.getCell(columnNum) != null) { | |
| 836 | + HSSFCell currentCell = currentRow.getCell(columnNum); | |
| 837 | + if (currentCell.getCellType() == XSSFCell.CELL_TYPE_STRING) { | |
| 838 | + int length = currentCell.getStringCellValue().getBytes().length; | |
| 839 | + if (columnWidth < length) { | |
| 840 | + columnWidth = length; | |
| 841 | + } | |
| 842 | + } | |
| 843 | + } | |
| 844 | + } | |
| 845 | + sheet.setColumnWidth(columnNum, columnWidth * 300); | |
| 846 | +// sheet.setColumnWidth(columnNum, columnWidth * 256); | |
| 847 | + } | |
| 848 | + } | |
| 849 | + | |
| 850 | + /** | |
| 851 | + * 设置单元格值和样式 | |
| 852 | + * @param row | |
| 853 | + * @param style | |
| 854 | + * @param index | |
| 855 | + * @param value | |
| 856 | + */ | |
| 857 | + public static void setCellStyleAndValue(HSSFRow row,HSSFCellStyle style,int index,String value){ | |
| 858 | + HSSFCell cell = row.createCell(index); | |
| 859 | + cell.setCellValue(value); | |
| 860 | + cell.setCellStyle(style); | |
| 861 | + } | |
| 862 | + /** | |
| 863 | + * 设置合并单元格样式 | |
| 864 | + * @param cra | |
| 865 | + * @param sheet | |
| 866 | + * @param workbook | |
| 867 | + */ | |
| 868 | + public static void setMergeCellStyle (CellRangeAddress cra, HSSFSheet sheet, Workbook workbook){ | |
| 869 | + // 使用RegionUtil类为合并后的单元格添加边框 | |
| 870 | + RegionUtil.setBorderBottom(1, cra, sheet, workbook); // 下边框 | |
| 871 | + RegionUtil.setBorderLeft(1, cra, sheet, workbook); // 左边框 | |
| 872 | + RegionUtil.setBorderRight(1, cra, sheet, workbook); // 有边框 | |
| 873 | + RegionUtil.setBorderTop(1, cra, sheet, workbook); // 上边框 | |
| 874 | + } | |
| 875 | +} | ... | ... |
src/main/resources/static/pages/forms/calc/calcInvestigateMonth.html
| 1 | -<style type="text/css"> | |
| 2 | - .table-bordered { | |
| 3 | - border: 1px solid; } | |
| 4 | - .table-bordered > thead > tr > th, | |
| 5 | - .table-bordered > thead > tr > td, | |
| 6 | - .table-bordered > tbody > tr > th, | |
| 7 | - .table-bordered > tbody > tr > td, | |
| 8 | - .table-bordered > tfoot > tr > th, | |
| 9 | - .table-bordered > tfoot > tr > td { | |
| 10 | - border: 1px solid; } | |
| 11 | - .table-bordered > thead > tr > th, | |
| 12 | - .table-bordered > thead > tr > td { | |
| 13 | - border-bottom-width: 2px; } | |
| 14 | - | |
| 15 | - .table > tbody + tbody { | |
| 16 | - border-top: 1px solid; } | |
| 17 | - | |
| 18 | - #forms > thead > tr> td >span{ | |
| 19 | - width: 5px; | |
| 20 | - word-wrap: break-word; | |
| 21 | - letter-spacing: 20px; | |
| 22 | - } | |
| 23 | - | |
| 24 | - #forms > thead > tr> td >label{ | |
| 25 | - word-break: keep-all;white-space:nowrap; | |
| 26 | - } | |
| 27 | - | |
| 28 | - .table > thead > tr > td{ | |
| 29 | - min-width: 80px; | |
| 30 | - text-align:center; | |
| 31 | - } | |
| 32 | - .table > tbody > tr > td{ | |
| 33 | - text-align:center; | |
| 34 | - } | |
| 35 | -</style> | |
| 36 | - | |
| 37 | -<div class="page-head"> | |
| 38 | - <div class="page-title"> | |
| 39 | - <h1>浦东公交线路调查表</h1> | |
| 40 | - </div> | |
| 41 | -</div> | |
| 42 | - | |
| 43 | -<!-- <div class="row"> --> | |
| 44 | - <div class="col-md-12 portlet light porttlet-fit bordered" style="height:calc(100% - 56px)"> | |
| 45 | -<!-- <div> --> | |
| 46 | - <div class="portlet-title"> | |
| 47 | - <form class="form-inline" action=""> | |
| 48 | - <div style="display: inline-block;margin-left: 15px;" id="gsbmDiv"> | |
| 49 | - <span class="item-label" style="width: 80px;">公司: </span> | |
| 50 | - <select class="form-control" name="company" id="gsbm" style="width: 180px;"></select> | |
| 51 | - </div> | |
| 52 | - <div style="display: inline-block; margin-left: 9px;" id="fgsbmDiv"> | |
| 53 | - <span class="item-label" style="width: 80px;">分公司: </span> | |
| 54 | - <select class="form-control" name="subCompany" id="fgsbm" style="width: 180px;"></select> | |
| 55 | - </div> | |
| 56 | - <div style="margin-top: 2px"></div> | |
| 57 | - <div style="display: inline-block;margin-left: 15px;"> | |
| 58 | - <span class="item-label" style="width: 80px;">月份: </span> | |
| 59 | - <input class="form-control" type="text" id="month" style="width: 180px;"/> | |
| 60 | - </div> | |
| 61 | - <div style="display: inline-block; margin-left: 23px;"> | |
| 62 | - <span class="item-label" style="width: 80px;">线路: </span> | |
| 63 | - <select class="form-control" name="line" id="line" style="width: 180px;"></select> | |
| 64 | - </div> | |
| 65 | - <div style="display: inline-block;"> | |
| 66 | - <span class="item-label" style="width: 80px;">线路性质: </span> | |
| 67 | - <select | |
| 68 | - class="form-control" name="nature" id="nature" style="width: 180px;"> | |
| 69 | - <option value="0" selected="selected">全部线路</option> | |
| 70 | - <option value="1">营运线路</option> | |
| 71 | - <option value="2">非营运线路</option> | |
| 72 | - </select> | |
| 73 | - </div> | |
| 74 | - <div class="form-group"> | |
| 75 | - <input class="btn btn-default" type="button" id="query" value="查询"/> | |
| 76 | - <input class="btn btn-default" type="button" id="export" value="导出"/> | |
| 77 | - </div> | |
| 78 | - </form> | |
| 79 | - </div> | |
| 80 | - <label><b id="currMonth"></b></label> | |
| 81 | - <div class="portlet-body" id="tjrbBody" style="overflow:auto;height: calc(100% - 80px)"> | |
| 82 | - <div class="table-container" style="margin-top: 10px;min-width: 906px"> | |
| 83 | - <table class="table table-bordered table-hover table-checkable" id="forms"> | |
| 84 | - <thead> | |
| 85 | - <tr> | |
| 86 | - <th colspan="34"><label id="tjrq"></label>浦东公交线路调查表</th> | |
| 87 | - </tr> | |
| 88 | - <tr> | |
| 89 | - <td rowspan="2" style="min-width:90px">线路</td> | |
| 90 | - <td rowspan="2" style="min-width:90px">权证配车数</td> | |
| 91 | - <td rowspan="2">线路属性</td> | |
| 92 | - <td rowspan="2">线路类型</td> | |
| 93 | - <td rowspan="2">线路长度</td> | |
| 94 | - <td rowspan="2">是否挂牌线路</td> | |
| 95 | - <td rowspan="2">是否冷僻线路</td> | |
| 96 | - <td rowspan="2" style="min-width:175px">首末班时间</td> | |
| 97 | - | |
| 98 | - <td colspan="2">调度方式</td> | |
| 99 | - <td colspan="2">间隔时间</td> | |
| 100 | - | |
| 101 | - <td rowspan="2">是否衔接轨交末班</td> | |
| 102 | - <td rowspan="2">日均里程</td> | |
| 103 | - <td rowspan="2">日均班次</td> | |
| 104 | - <td rowspan="2">日均人次</td> | |
| 105 | - <td rowspan="2">日均单车人次</td> | |
| 106 | - <td rowspan="2">每班次单车人次</td> | |
| 107 | - | |
| 108 | - <td colspan="3">里程</td> | |
| 109 | - | |
| 110 | - <td rowspan="2" style="min-width:104px">里程利用率%</td> | |
| 111 | - <td rowspan="2" style="min-width:104px">百公里人次(载客公里)</td> | |
| 112 | - <td rowspan="2">月人次(票务)</td> | |
| 113 | - <td rowspan="2">月营收(票务)</td> | |
| 114 | - <td rowspan="2">票价(元)</td> | |
| 115 | - <td rowspan="2" style="min-width:104px">百公里营收(元)</td> | |
| 116 | - | |
| 117 | - <td colspan="3">线路特征</td> | |
| 118 | - | |
| 119 | - <td rowspan="2" style="min-width:400px">线路走向</td> | |
| 120 | - <td rowspan="2" style="min-width:600px">线路站点</td> | |
| 121 | - <td rowspan="2" style="min-width:160px">备注</td> | |
| 122 | - </tr> | |
| 123 | - <tr> | |
| 124 | - <td rowspan="1" style="min-width:60px">常规</td> | |
| 125 | - <td rowspan="1" style="min-width:60px">多样化</td> | |
| 126 | - <td rowspan="1">高峰</td> | |
| 127 | - <td rowspan="1">低谷</td> | |
| 128 | - <td rowspan="1" style="min-width:104px">运营总公里(含空驶)</td> | |
| 129 | - <td rowspan="1" style="min-width:104px">集调实际公里(含空驶)</td> | |
| 130 | - <td rowspan="1" style="min-width:104px">集调营运公里(载客公里)</td> | |
| 131 | - <td rowspan="1">途径轨交站</td> | |
| 132 | - <td rowspan="1">途径三级医院</td> | |
| 133 | - <td rowspan="1">唯一线</td> | |
| 134 | - </tr> | |
| 135 | - </thead> | |
| 136 | - <tbody class="calcInvestigateMonth"> | |
| 137 | - | |
| 138 | - </tbody> | |
| 139 | - </table> | |
| 140 | - </div> | |
| 141 | - </div> | |
| 142 | - </div> | |
| 143 | -<!-- </div> --> | |
| 144 | -<!-- </div> --> | |
| 145 | - | |
| 146 | -<script> | |
| 147 | - $(function(){ | |
| 148 | - $('#export').attr('disabled', "true"); | |
| 149 | - | |
| 150 | - // 关闭左侧栏 | |
| 151 | - if (!$('body').hasClass('page-sidebar-closed')) | |
| 152 | - $('.menu-toggler.sidebar-toggler').click(); | |
| 153 | - | |
| 154 | - var d = new Date(); | |
| 155 | - d.setTime(d.getTime() - 4*1000*60*60*24); | |
| 156 | - var year = d.getFullYear(); | |
| 157 | - var month = d.getMonth() + 1; | |
| 158 | - var day = d.getDate(); | |
| 159 | - if(month < 10) | |
| 160 | - month = "0" + month; | |
| 161 | - if(day < 10) | |
| 162 | - day = "0" + day; | |
| 163 | - var dateTime = year + "-" + month + "-" + day; | |
| 164 | - var initMonth = year + "-" + month; | |
| 165 | - $("#month").datetimepicker({ | |
| 166 | - format : 'YYYY-MM', | |
| 167 | - locale : 'zh-cn', | |
| 168 | - maxDate : initMonth | |
| 169 | - }); | |
| 170 | - $("#month").val(initMonth); | |
| 171 | - | |
| 172 | - | |
| 173 | - var fage=false; | |
| 174 | - var obj = []; | |
| 175 | - var xlList; | |
| 176 | - $.get('/report/lineList',function(result){ | |
| 177 | - xlList=result; | |
| 178 | - $.get('/user/companyData', function(result){ | |
| 179 | - obj = result; | |
| 180 | - var options = '<option value="">全部公司</option>'; | |
| 181 | - for(var i = 0; i < obj.length; i++){ | |
| 182 | - options += '<option value="'+obj[i].companyCode+'">'+obj[i].companyName+'</option>'; | |
| 183 | - } | |
| 184 | - | |
| 185 | - if(obj.length ==0){ | |
| 186 | - $("#gsbmDiv").css('display','none'); | |
| 187 | - }else if(obj.length ==1){ | |
| 188 | - $("#gsbmDiv").css('display','none'); | |
| 189 | - if(obj[0].children.length == 1 || obj[0].children.length ==0) | |
| 190 | - $('#fgsbmDiv').css('display','none'); | |
| 191 | - } | |
| 192 | - $('#gsbm').html(options); | |
| 193 | - updateCompany(); | |
| 194 | - }); | |
| 195 | - }) | |
| 196 | - $("#gsbm").on("change",updateCompany); | |
| 197 | - function updateCompany(){ | |
| 198 | - var company = $('#gsbm').val(); | |
| 199 | - var options = '<option value="">全部分公司</option>'; | |
| 200 | - for(var i = 0; i < obj.length; i++){ | |
| 201 | - if(obj[i].companyCode == company){ | |
| 202 | - var children = obj[i].children; | |
| 203 | - for(var j = 0; j < children.length; j++){ | |
| 204 | - options += '<option value="'+children[j].code+'">'+children[j].name+'</option>'; | |
| 205 | - } | |
| 206 | - } | |
| 207 | - } | |
| 208 | - $('#fgsbm').html(options); | |
| 209 | -// initXl(); | |
| 210 | - } | |
| 211 | - | |
| 212 | - var tempData = {}; | |
| 213 | - $.get('/report/lineList',function(xlList){ | |
| 214 | - var data = []; | |
| 215 | - data.push({id: " ", text: "全部线路"}); | |
| 216 | - $.get('/user/companyData', function(result){ | |
| 217 | - for(var i = 0; i < result.length; i++){ | |
| 218 | - var companyCode = result[i].companyCode; | |
| 219 | - var children = result[i].children; | |
| 220 | - for(var j = 0; j < children.length; j++){ | |
| 221 | - var code = children[j].code; | |
| 222 | - for(var k=0;k < xlList.length;k++ ){ | |
| 223 | - if(xlList[k]["fgsbm"]==code && xlList[k]["gsbm"]==companyCode){ | |
| 224 | - data.push({id: xlList[k]["xlbm"], text: xlList[k]["xlname"]}); | |
| 225 | - tempData[xlList[k]["xlbm"]] = companyCode+":"+code; | |
| 226 | - } | |
| 227 | - } | |
| 228 | - } | |
| 229 | - } | |
| 230 | - initPinYinSelect2('#line',data,''); | |
| 231 | - | |
| 232 | - }); | |
| 233 | - }); | |
| 234 | - | |
| 235 | - $("#line").on("change", function(){ | |
| 236 | - if($("#line").val() == " "){ | |
| 237 | - $("#gsbm").attr("disabled", false); | |
| 238 | - $("#fgsbm").attr("disabled", false); | |
| 239 | - } else { | |
| 240 | - var temp = tempData[$("#line").val()].split(":"); | |
| 241 | - $("#gsbm").val(temp[0]); | |
| 242 | - updateCompany(); | |
| 243 | - $("#fgsbm").val(temp[1]); | |
| 244 | - $("#gsbm").attr("disabled", true); | |
| 245 | - $("#fgsbm").attr("disabled", true); | |
| 246 | - $("#nature").val(0); | |
| 247 | - } | |
| 248 | - }); | |
| 249 | - | |
| 250 | - | |
| 251 | - var line =""; | |
| 252 | - var xlName =""; | |
| 253 | - var nature =""; | |
| 254 | - var month = ""; | |
| 255 | - var gsbm = ""; | |
| 256 | - var fgsbm = ""; | |
| 257 | - $("#query").on("click",function(){ | |
| 258 | - if($("#month").val() == null || $("#month").val().trim().length == 0){ | |
| 259 | - layer.msg("请选择月份!"); | |
| 260 | - return; | |
| 261 | - } | |
| 262 | -// $("#tjrbBody").height($(window).height()-100); | |
| 263 | - line = $("#line").val(); | |
| 264 | - xlName = $("#select2-line-container").html(); | |
| 265 | - nature = $("#nature").val(); | |
| 266 | - month = $("#month").val(); | |
| 267 | - gsbm = $("#gsbm").val(); | |
| 268 | - fgsbm = $("#fgsbm").val(); | |
| 269 | - if(line=="请选择"){ | |
| 270 | - line=""; | |
| 271 | - } | |
| 272 | - if(month==null || month ==""){ | |
| 273 | - layer.msg('请选择月份!'); | |
| 274 | - }else{ | |
| 275 | -// $("#tjrq").html(date+"至"+date2); | |
| 276 | - var params = {}; | |
| 277 | - params['gsbm'] = gsbm; | |
| 278 | - params['fgsbm'] = fgsbm; | |
| 279 | - params['month'] = month; | |
| 280 | - params['line'] = line; | |
| 281 | - params['xlName'] = xlName; | |
| 282 | - params['nature'] = nature; | |
| 283 | - params['type'] = "query"; | |
| 284 | - $get('/calc_mix/calcInvestigateMonth',params,function(result){ | |
| 285 | - $("#currMonth").html("("+month+")"); | |
| 286 | - // 把数据填充到模版中 | |
| 287 | - var tbodyHtml = template('calcInvestigateMonth',{list:result}); | |
| 288 | - // 把渲染好的模版html文本追加到表格中 | |
| 289 | - $('#forms .calcInvestigateMonth').html(tbodyHtml); | |
| 290 | - | |
| 291 | - if(result.length == 0) | |
| 292 | - $("#export").attr('disabled',"true"); | |
| 293 | - else | |
| 294 | - $("#export").removeAttr("disabled"); | |
| 295 | - }); | |
| 296 | - } | |
| 297 | - | |
| 298 | - }); | |
| 299 | -// $("#tjrbBody").height($(window).height()-100); | |
| 300 | - $("#export").on("click",function(){ | |
| 301 | - var params = {}; | |
| 302 | - params['gsbm'] = gsbm; | |
| 303 | - params['fgsbm'] = fgsbm; | |
| 304 | - params['month'] = month; | |
| 305 | - params['line'] = line; | |
| 306 | - params['xlName'] = xlName; | |
| 307 | - params['nature'] = nature; | |
| 308 | - params['type'] = "export"; | |
| 309 | - $get('/calc_mix/calcInvestigateMonth',params,function(result){ | |
| 310 | - window.open("/downloadFile/download?fileName=浦东公交线路调查表"+moment(month).format("YYYY-MM")); | |
| 311 | - }); | |
| 312 | - }); | |
| 313 | - | |
| 314 | - }); | |
| 315 | -</script> | |
| 316 | -<script type="text/html" id="calcInvestigateMonth"> | |
| 317 | - {{each list as obj i}} | |
| 318 | - <tr> | |
| 319 | - <td>{{obj.xlmc}}</td> | |
| 320 | - <td>{{obj.qzpcs}}</td> | |
| 321 | - <td>{{obj.xlsx}}</td> | |
| 322 | - <td>{{obj.xllx}}</td> | |
| 323 | - <td>{{obj.xlcd}}</td> | |
| 324 | - <td>{{obj.sfgp}}</td> | |
| 325 | - <td>{{obj.sflpxl}}</td> | |
| 326 | - <td>{{obj.smbsj}}</td> | |
| 327 | - <td>{{if obj.ddfs == '常规'}}√{{/if}}</td> | |
| 328 | - <td>{{if obj.ddfs == '多样化'}}√{{/if}}</td> | |
| 329 | - <td>{{obj.gfjgsj}}</td> | |
| 330 | - <td>{{obj.dgjgsj}}</td> | |
| 331 | - <td>{{obj.sfxjgj}}</td> | |
| 332 | - <td>{{obj.rjlc}}</td> | |
| 333 | - <td>{{obj.rjbc}}</td> | |
| 334 | - <td>{{obj.rjrc}}</td> | |
| 335 | - <td>{{obj.rjdcrc}}</td> | |
| 336 | - <td>{{obj.mbcrc}}</td> | |
| 337 | - <td>{{obj.jhzgl}}</td> | |
| 338 | - <td>{{obj.sjzgl}}</td> | |
| 339 | - <td>{{obj.sjyylc}}</td> | |
| 340 | - <td>{{obj.lclyl}}</td> | |
| 341 | - <td>{{obj.bglrc}}</td> | |
| 342 | - <td>{{obj.yrc}}</td> | |
| 343 | - <td>{{obj.yys}}</td> | |
| 344 | - <td>{{obj.pj}}</td> | |
| 345 | - <td>{{obj.bglys}}</td> | |
| 346 | - <td>{{obj.tjgjz}}</td> | |
| 347 | - <td>{{obj.tjsjyy}}</td> | |
| 348 | - <td>{{obj.wyx}}</td> | |
| 349 | - <td>{{obj.xlzx}}</td> | |
| 350 | - <td>{{obj.xlzd}}</td> | |
| 351 | - <td>{{obj.bz}}</td> | |
| 352 | - </tr> | |
| 353 | - {{/each}} | |
| 354 | - {{if list.length == 0}} | |
| 355 | - <tr> | |
| 356 | - <td colspan="34"><h6 class="muted">没有找到相关数据</h6></td> | |
| 357 | - </tr> | |
| 358 | - {{/if}} | |
| 1 | +<style type="text/css"> | |
| 2 | + .table-bordered { | |
| 3 | + border: 1px solid; } | |
| 4 | + .table-bordered > thead > tr > th, | |
| 5 | + .table-bordered > thead > tr > td, | |
| 6 | + .table-bordered > tbody > tr > th, | |
| 7 | + .table-bordered > tbody > tr > td, | |
| 8 | + .table-bordered > tfoot > tr > th, | |
| 9 | + .table-bordered > tfoot > tr > td { | |
| 10 | + border: 1px solid; } | |
| 11 | + .table-bordered > thead > tr > th, | |
| 12 | + .table-bordered > thead > tr > td { | |
| 13 | + border-bottom-width: 2px; } | |
| 14 | + | |
| 15 | + .table > tbody + tbody { | |
| 16 | + border-top: 1px solid; } | |
| 17 | + | |
| 18 | + #forms > thead > tr> td >span{ | |
| 19 | + width: 5px; | |
| 20 | + word-wrap: break-word; | |
| 21 | + letter-spacing: 20px; | |
| 22 | + } | |
| 23 | + | |
| 24 | + #forms > thead > tr> td >label{ | |
| 25 | + word-break: keep-all;white-space:nowrap; | |
| 26 | + } | |
| 27 | + | |
| 28 | + .table > thead > tr > td{ | |
| 29 | + min-width: 80px; | |
| 30 | + text-align:center; | |
| 31 | + } | |
| 32 | + .table > tbody > tr > td{ | |
| 33 | + text-align:center; | |
| 34 | + } | |
| 35 | +</style> | |
| 36 | + | |
| 37 | +<div class="page-head"> | |
| 38 | + <div class="page-title"> | |
| 39 | + <h1>浦东公交线路调查表</h1> | |
| 40 | + </div> | |
| 41 | +</div> | |
| 42 | + | |
| 43 | +<!-- <div class="row"> --> | |
| 44 | + <div class="col-md-12 portlet light porttlet-fit bordered" style="height:calc(100% - 56px)"> | |
| 45 | +<!-- <div> --> | |
| 46 | + <div class="portlet-title"> | |
| 47 | + <form class="form-inline" action=""> | |
| 48 | + <div style="display: inline-block;margin-left: 15px;" id="gsbmDiv"> | |
| 49 | + <span class="item-label" style="width: 80px;">公司: </span> | |
| 50 | + <select class="form-control" name="company" id="gsbm" style="width: 180px;"></select> | |
| 51 | + </div> | |
| 52 | + <div style="display: inline-block; margin-left: 9px;" id="fgsbmDiv"> | |
| 53 | + <span class="item-label" style="width: 80px;">分公司: </span> | |
| 54 | + <select class="form-control" name="subCompany" id="fgsbm" style="width: 180px;"></select> | |
| 55 | + </div> | |
| 56 | + <div style="margin-top: 2px"></div> | |
| 57 | + <div style="display: inline-block;margin-left: 15px;"> | |
| 58 | + <span class="item-label" style="width: 80px;">月份: </span> | |
| 59 | + <input class="form-control" type="text" id="month" style="width: 180px;"/> | |
| 60 | + </div> | |
| 61 | + <div style="display: inline-block; margin-left: 23px;"> | |
| 62 | + <span class="item-label" style="width: 80px;">线路: </span> | |
| 63 | + <select class="form-control" name="line" id="line" style="width: 180px;"></select> | |
| 64 | + </div> | |
| 65 | + <div style="display: inline-block;"> | |
| 66 | + <span class="item-label" style="width: 80px;">线路性质: </span> | |
| 67 | + <select | |
| 68 | + class="form-control" name="nature" id="nature" style="width: 180px;"> | |
| 69 | + <option value="0" selected="selected">全部线路</option> | |
| 70 | + <option value="1">营运线路</option> | |
| 71 | + <option value="2">非营运线路</option> | |
| 72 | + </select> | |
| 73 | + </div> | |
| 74 | + <div class="form-group"> | |
| 75 | + <input class="btn btn-default" type="button" id="query" value="查询"/> | |
| 76 | + <input class="btn btn-default" type="button" id="export" value="导出"/> | |
| 77 | + </div> | |
| 78 | + </form> | |
| 79 | + </div> | |
| 80 | + <label><b id="currMonth"></b></label> | |
| 81 | + <div class="portlet-body" id="tjrbBody" style="overflow:auto;height: calc(100% - 80px)"> | |
| 82 | + <div class="table-container" style="margin-top: 10px;min-width: 906px"> | |
| 83 | + <table class="table table-bordered table-hover table-checkable" id="forms"> | |
| 84 | + <thead> | |
| 85 | + <tr> | |
| 86 | + <th colspan="35"><label id="tjrq"></label>浦东公交线路调查表</th> | |
| 87 | + </tr> | |
| 88 | + <tr> | |
| 89 | + <td rowspan="2">序号</td> | |
| 90 | + <td rowspan="2" style="min-width:90px">线路</td> | |
| 91 | + <td rowspan="2" style="min-width:90px">权证配车数</td> | |
| 92 | + <td rowspan="2">线路属性</td> | |
| 93 | + <td rowspan="2">线路类型</td> | |
| 94 | + <td rowspan="2">线路长度</td> | |
| 95 | + <td rowspan="2">是否挂牌线路</td> | |
| 96 | + <td rowspan="2">是否冷僻线路</td> | |
| 97 | + <td rowspan="2" style="min-width:175px">首末班时间</td> | |
| 98 | + | |
| 99 | + <td colspan="2">调度方式</td> | |
| 100 | + <td colspan="2">间隔时间</td> | |
| 101 | + | |
| 102 | + <td rowspan="2">是否衔接轨交末班</td> | |
| 103 | + <td rowspan="2">日均里程</td> | |
| 104 | + <td rowspan="2">日均班次</td> | |
| 105 | + <td rowspan="2">日均人次</td> | |
| 106 | + <td rowspan="2">日均单车人次</td> | |
| 107 | + <td rowspan="2">每班次单车人次</td> | |
| 108 | + | |
| 109 | + <td colspan="3">里程</td> | |
| 110 | + | |
| 111 | + <td rowspan="2" style="min-width:104px">里程利用率%</td> | |
| 112 | + <td rowspan="2" style="min-width:104px">百公里人次(载客公里)</td> | |
| 113 | + <td rowspan="2">月人次(票务)</td> | |
| 114 | + <td rowspan="2">月营收(票务)</td> | |
| 115 | + <td rowspan="2">票价(元)</td> | |
| 116 | + <td rowspan="2" style="min-width:104px">百公里营收(元)</td> | |
| 117 | + | |
| 118 | + <td colspan="3">线路特征</td> | |
| 119 | + | |
| 120 | + <td rowspan="2" style="min-width:400px">线路走向</td> | |
| 121 | + <td rowspan="2" style="min-width:600px">线路站点</td> | |
| 122 | + <td rowspan="2" style="min-width:160px">备注</td> | |
| 123 | + </tr> | |
| 124 | + <tr> | |
| 125 | + <td rowspan="1" style="min-width:60px">常规</td> | |
| 126 | + <td rowspan="1" style="min-width:60px">多样化</td> | |
| 127 | + <td rowspan="1">高峰</td> | |
| 128 | + <td rowspan="1">低谷</td> | |
| 129 | + <td rowspan="1" style="min-width:104px">运营总公里(含空驶)</td> | |
| 130 | + <td rowspan="1" style="min-width:104px">集调实际公里(含空驶)</td> | |
| 131 | + <td rowspan="1" style="min-width:104px">集调营运公里(载客公里)</td> | |
| 132 | + <td rowspan="1">途径轨交站</td> | |
| 133 | + <td rowspan="1">途径三级医院</td> | |
| 134 | + <td rowspan="1">唯一线</td> | |
| 135 | + </tr> | |
| 136 | + </thead> | |
| 137 | + <tbody class="calcInvestigateMonth"> | |
| 138 | + | |
| 139 | + </tbody> | |
| 140 | + </table> | |
| 141 | + </div> | |
| 142 | + </div> | |
| 143 | + </div> | |
| 144 | +<!-- </div> --> | |
| 145 | +<!-- </div> --> | |
| 146 | + | |
| 147 | +<script> | |
| 148 | + $(function(){ | |
| 149 | + $('#export').attr('disabled', "true"); | |
| 150 | + | |
| 151 | + // 关闭左侧栏 | |
| 152 | + if (!$('body').hasClass('page-sidebar-closed')) | |
| 153 | + $('.menu-toggler.sidebar-toggler').click(); | |
| 154 | + | |
| 155 | + var d = new Date(); | |
| 156 | + d.setTime(d.getTime() - 4*1000*60*60*24); | |
| 157 | + var year = d.getFullYear(); | |
| 158 | + var month = d.getMonth() + 1; | |
| 159 | + var day = d.getDate(); | |
| 160 | + if(month < 10) | |
| 161 | + month = "0" + month; | |
| 162 | + if(day < 10) | |
| 163 | + day = "0" + day; | |
| 164 | + var dateTime = year + "-" + month + "-" + day; | |
| 165 | + var initMonth = year + "-" + month; | |
| 166 | + $("#month").datetimepicker({ | |
| 167 | + format : 'YYYY-MM', | |
| 168 | + locale : 'zh-cn', | |
| 169 | + maxDate : initMonth | |
| 170 | + }); | |
| 171 | + $("#month").val(initMonth); | |
| 172 | + | |
| 173 | + | |
| 174 | + var fage=false; | |
| 175 | + var obj = []; | |
| 176 | + var xlList; | |
| 177 | + $.get('/report/lineList',function(result){ | |
| 178 | + xlList=result; | |
| 179 | + $.get('/user/companyData', function(result){ | |
| 180 | + obj = result; | |
| 181 | + var options = '<option value="">全部公司</option>'; | |
| 182 | + for(var i = 0; i < obj.length; i++){ | |
| 183 | + options += '<option value="'+obj[i].companyCode+'">'+obj[i].companyName+'</option>'; | |
| 184 | + } | |
| 185 | + | |
| 186 | + if(obj.length ==0){ | |
| 187 | + $("#gsbmDiv").css('display','none'); | |
| 188 | + }else if(obj.length ==1){ | |
| 189 | + $("#gsbmDiv").css('display','none'); | |
| 190 | + if(obj[0].children.length == 1 || obj[0].children.length ==0) | |
| 191 | + $('#fgsbmDiv').css('display','none'); | |
| 192 | + } | |
| 193 | + $('#gsbm').html(options); | |
| 194 | + updateCompany(); | |
| 195 | + }); | |
| 196 | + }) | |
| 197 | + $("#gsbm").on("change",updateCompany); | |
| 198 | + function updateCompany(){ | |
| 199 | + var company = $('#gsbm').val(); | |
| 200 | + var options = '<option value="">全部分公司</option>'; | |
| 201 | + for(var i = 0; i < obj.length; i++){ | |
| 202 | + if(obj[i].companyCode == company){ | |
| 203 | + var children = obj[i].children; | |
| 204 | + for(var j = 0; j < children.length; j++){ | |
| 205 | + options += '<option value="'+children[j].code+'">'+children[j].name+'</option>'; | |
| 206 | + } | |
| 207 | + } | |
| 208 | + } | |
| 209 | + $('#fgsbm').html(options); | |
| 210 | +// initXl(); | |
| 211 | + } | |
| 212 | + | |
| 213 | + var tempData = {}; | |
| 214 | + $.get('/report/lineList',function(xlList){ | |
| 215 | + var data = []; | |
| 216 | + data.push({id: " ", text: "全部线路"}); | |
| 217 | + $.get('/user/companyData', function(result){ | |
| 218 | + for(var i = 0; i < result.length; i++){ | |
| 219 | + var companyCode = result[i].companyCode; | |
| 220 | + var children = result[i].children; | |
| 221 | + for(var j = 0; j < children.length; j++){ | |
| 222 | + var code = children[j].code; | |
| 223 | + for(var k=0;k < xlList.length;k++ ){ | |
| 224 | + if(xlList[k]["fgsbm"]==code && xlList[k]["gsbm"]==companyCode){ | |
| 225 | + data.push({id: xlList[k]["xlbm"], text: xlList[k]["xlname"]}); | |
| 226 | + tempData[xlList[k]["xlbm"]] = companyCode+":"+code; | |
| 227 | + } | |
| 228 | + } | |
| 229 | + } | |
| 230 | + } | |
| 231 | + initPinYinSelect2('#line',data,''); | |
| 232 | + | |
| 233 | + }); | |
| 234 | + }); | |
| 235 | + | |
| 236 | + $("#line").on("change", function(){ | |
| 237 | + if($("#line").val() == " "){ | |
| 238 | + $("#gsbm").attr("disabled", false); | |
| 239 | + $("#fgsbm").attr("disabled", false); | |
| 240 | + } else { | |
| 241 | + var temp = tempData[$("#line").val()].split(":"); | |
| 242 | + $("#gsbm").val(temp[0]); | |
| 243 | + updateCompany(); | |
| 244 | + $("#fgsbm").val(temp[1]); | |
| 245 | + $("#gsbm").attr("disabled", true); | |
| 246 | + $("#fgsbm").attr("disabled", true); | |
| 247 | + $("#nature").val(0); | |
| 248 | + } | |
| 249 | + }); | |
| 250 | + | |
| 251 | + | |
| 252 | + var line =""; | |
| 253 | + var xlName =""; | |
| 254 | + var nature =""; | |
| 255 | + var month = ""; | |
| 256 | + var gsbm = ""; | |
| 257 | + var fgsbm = ""; | |
| 258 | + $("#query").on("click",function(){ | |
| 259 | + if($("#month").val() == null || $("#month").val().trim().length == 0){ | |
| 260 | + layer.msg("请选择月份!"); | |
| 261 | + return; | |
| 262 | + } | |
| 263 | +// $("#tjrbBody").height($(window).height()-100); | |
| 264 | + line = $("#line").val(); | |
| 265 | + xlName = $("#select2-line-container").html(); | |
| 266 | + nature = $("#nature").val(); | |
| 267 | + month = $("#month").val(); | |
| 268 | + gsbm = $("#gsbm").val(); | |
| 269 | + fgsbm = $("#fgsbm").val(); | |
| 270 | + if(line=="请选择"){ | |
| 271 | + line=""; | |
| 272 | + } | |
| 273 | + if(month==null || month ==""){ | |
| 274 | + layer.msg('请选择月份!'); | |
| 275 | + }else{ | |
| 276 | +// $("#tjrq").html(date+"至"+date2); | |
| 277 | + var params = {}; | |
| 278 | + params['gsbm'] = gsbm; | |
| 279 | + params['fgsbm'] = fgsbm; | |
| 280 | + params['month'] = month; | |
| 281 | + params['line'] = line; | |
| 282 | + params['xlName'] = xlName; | |
| 283 | + params['nature'] = nature; | |
| 284 | + params['type'] = "query"; | |
| 285 | + $get('/calc_mix/calcInvestigateMonth',params,function(result){ | |
| 286 | + $("#currMonth").html("("+month+")"); | |
| 287 | + // 把数据填充到模版中 | |
| 288 | + var tbodyHtml = template('calcInvestigateMonth',{list:result}); | |
| 289 | + // 把渲染好的模版html文本追加到表格中 | |
| 290 | + $('#forms .calcInvestigateMonth').html(tbodyHtml); | |
| 291 | + | |
| 292 | + if(result.length == 0) | |
| 293 | + $("#export").attr('disabled',"true"); | |
| 294 | + else | |
| 295 | + $("#export").removeAttr("disabled"); | |
| 296 | + }); | |
| 297 | + } | |
| 298 | + | |
| 299 | + }); | |
| 300 | +// $("#tjrbBody").height($(window).height()-100); | |
| 301 | + $("#export").on("click",function(){ | |
| 302 | + var params = {}; | |
| 303 | + params['gsbm'] = gsbm; | |
| 304 | + params['fgsbm'] = fgsbm; | |
| 305 | + params['month'] = month; | |
| 306 | + params['line'] = line; | |
| 307 | + params['xlName'] = xlName; | |
| 308 | + params['nature'] = nature; | |
| 309 | + params['type'] = "export"; | |
| 310 | + $get('/calc_mix/calcInvestigateMonth',params,function(result){ | |
| 311 | + window.open("/downloadFile/download?fileName=浦东公交线路调查表"+moment(month).format("YYYY-MM")); | |
| 312 | + }); | |
| 313 | + }); | |
| 314 | + | |
| 315 | + }); | |
| 316 | +</script> | |
| 317 | +<script type="text/html" id="calcInvestigateMonth"> | |
| 318 | + {{each list as obj i}} | |
| 319 | + <tr> | |
| 320 | + <td>{{i + 1}}</td> | |
| 321 | + <td>{{obj.xlmc}}</td> | |
| 322 | + <td>{{obj.qzpcs}}</td> | |
| 323 | + <td>{{obj.xlsx}}</td> | |
| 324 | + <td>{{obj.xllx}}</td> | |
| 325 | + <td>{{obj.xlcd}}</td> | |
| 326 | + <td>{{obj.sfgp}}</td> | |
| 327 | + <td>{{obj.sflpxl}}</td> | |
| 328 | + <td>{{obj.smbsj}}</td> | |
| 329 | + <td>{{if obj.ddfs == '常规'}}√{{/if}}</td> | |
| 330 | + <td>{{if obj.ddfs == '多样化'}}√{{/if}}</td> | |
| 331 | + <td>{{obj.gfjgsj}}</td> | |
| 332 | + <td>{{obj.dgjgsj}}</td> | |
| 333 | + <td>{{obj.sfxjgj}}</td> | |
| 334 | + <td>{{obj.rjlc}}</td> | |
| 335 | + <td>{{obj.rjbc}}</td> | |
| 336 | + <td>{{obj.rjrc}}</td> | |
| 337 | + <td>{{obj.rjdcrc}}</td> | |
| 338 | + <td>{{obj.mbcrc}}</td> | |
| 339 | + <td>{{obj.jhzgl}}</td> | |
| 340 | + <td>{{obj.sjzgl}}</td> | |
| 341 | + <td>{{obj.sjyylc}}</td> | |
| 342 | + <td>{{obj.lclyl}}</td> | |
| 343 | + <td>{{obj.bglrc}}</td> | |
| 344 | + <td>{{obj.yrc}}</td> | |
| 345 | + <td>{{obj.yys}}</td> | |
| 346 | + <td>{{obj.pj}}</td> | |
| 347 | + <td>{{obj.bglys}}</td> | |
| 348 | + <td>{{obj.tjgjz}}</td> | |
| 349 | + <td>{{obj.tjsjyy}}</td> | |
| 350 | + <td>{{obj.wyx}}</td> | |
| 351 | + <td>{{obj.xlzx}}</td> | |
| 352 | + <td>{{obj.xlzd}}</td> | |
| 353 | + <td>{{obj.bz}}</td> | |
| 354 | + </tr> | |
| 355 | + {{/each}} | |
| 356 | + {{if list.length == 0}} | |
| 357 | + <tr> | |
| 358 | + <td colspan="34"><h6 class="muted">没有找到相关数据</h6></td> | |
| 359 | + </tr> | |
| 360 | + {{/if}} | |
| 359 | 361 | </script> |
| 360 | 362 | \ No newline at end of file | ... | ... |
src/main/resources/static/pages/forms/mould/calcInvestigateMonth.xls
No preview for this file type