Commit 99f4fec29c02cfcff209fb7ef2fc054e52763cad

Authored by 潘钊
2 parents 0d2d4166 e622b476
src/main/java/com/bsth/repository/realcontrol/ScheduleRealInfoRepository.java
@@ -21,7 +21,7 @@ public interface ScheduleRealInfoRepository extends BaseRepository<ScheduleRealI @@ -21,7 +21,7 @@ public interface ScheduleRealInfoRepository extends BaseRepository<ScheduleRealI
21 @Query(value="select s from ScheduleRealInfo s where s.xlBm = ?1 and DATE_FORMAT(s.scheduleDate,'%Y-%m-%d') = ?2 group by jName,clZbh,lpName") 21 @Query(value="select s from ScheduleRealInfo s where s.xlBm = ?1 and DATE_FORMAT(s.scheduleDate,'%Y-%m-%d') = ?2 group by jName,clZbh,lpName")
22 List<ScheduleRealInfo> queryUserInfo(String line,String date); 22 List<ScheduleRealInfo> queryUserInfo(String line,String date);
23 23
24 - @Query(value="select s from ScheduleRealInfo s where s.jName = ?1 and s.clZbh = ?2 and s.lpName = ?3") 24 + @Query(value="select s from ScheduleRealInfo s where s.jName = ?1 and s.clZbh = ?2 and s.lpName = ?3 order by fcsj")
25 List<ScheduleRealInfo> exportWaybill(String jName,String clZbh,String lpName); 25 List<ScheduleRealInfo> exportWaybill(String jName,String clZbh,String lpName);
26 26
27 @Query(value="select new map(clZbh as clZbh,jGh as jGh,jName as jName,sum(jhlc) as zgl,sum(addMileage) as ksgl,count(jName) as bcs) from ScheduleRealInfo s where s.xlBm = ?1 and DATE_FORMAT(s.scheduleDate,'%Y-%m-%d') = ?2 group by clZbh,jGh") 27 @Query(value="select new map(clZbh as clZbh,jGh as jGh,jName as jName,sum(jhlc) as zgl,sum(addMileage) as ksgl,count(jName) as bcs) from ScheduleRealInfo s where s.xlBm = ?1 and DATE_FORMAT(s.scheduleDate,'%Y-%m-%d') = ?2 group by clZbh,jGh")
src/main/java/com/bsth/service/realcontrol/impl/ScheduleRealInfoServiceImpl.java
@@ -411,12 +411,39 @@ public class ScheduleRealInfoServiceImpl extends BaseServiceImpl&lt;ScheduleRealInf @@ -411,12 +411,39 @@ public class ScheduleRealInfoServiceImpl extends BaseServiceImpl&lt;ScheduleRealInf
411 ReportUtils ee = new ReportUtils(); 411 ReportUtils ee = new ReportUtils();
412 List<Iterator<?>> list = new ArrayList<Iterator<?>>(); 412 List<Iterator<?>> list = new ArrayList<Iterator<?>>();
413 List<ScheduleRealInfo> scheduleRealInfos = scheduleRealInfoRepository.exportWaybill(jName, clZbh, lpName); 413 List<ScheduleRealInfo> scheduleRealInfos = scheduleRealInfoRepository.exportWaybill(jName, clZbh, lpName);
414 - ScheduleRealInfo scheduleRealInfo = scheduleRealInfoRepository.findOne(scheduleRealInfos.get(0).getId()); 414 + ScheduleRealInfo scheduleReal = scheduleRealInfoRepository.findOne(scheduleRealInfos.get(0).getId());
  415 +
  416 + DecimalFormat format = new DecimalFormat("0.00");
  417 + int cjbc = scheduleRealInfoRepository.findCjbc(jName, clZbh, lpName);
  418 + int ljbc = scheduleRealInfoRepository.findLjbc(jName, clZbh, lpName);
  419 + int jhbc = 0;
  420 + double jhlc = 0;
  421 + float realMileage = 0l,addMileage = 0l,remMileage = 0l;
  422 + Map<String,Object> map = new HashMap<String, Object>();
  423 + for(ScheduleRealInfo scheduleRealInfo : scheduleRealInfos){
  424 + if(scheduleRealInfo != null){
  425 + jhlc += scheduleRealInfo.getJhlc()==null?0:scheduleRealInfo.getJhlc();
  426 + realMileage += scheduleRealInfo.getRealMileage()==null?0:scheduleRealInfo.getRealMileage();
  427 + addMileage += scheduleRealInfo.getAddMileage()==null?0:scheduleRealInfo.getAddMileage();
  428 + remMileage += scheduleRealInfo.getRemMileage()==null?0:scheduleRealInfo.getRemMileage();
  429 + jhbc++;
  430 + }
  431 + }
  432 + map.put("jhlc", format.format(jhlc));
  433 + map.put("remMileage", format.format(remMileage));
  434 + map.put("addMileage", format.format(addMileage));
  435 + map.put("yygl", format.format(realMileage-addMileage));
  436 + map.put("ksgl", format.format(realMileage-addMileage));
  437 + map.put("realMileage", format.format(realMileage));
  438 + map.put("jhbc", jhbc);
  439 + map.put("cjbc", cjbc);
  440 + map.put("ljbc", ljbc);
  441 + map.put("sjbc", jhbc-cjbc+ljbc);
415 442
416 String path = this.getClass().getResource("/").getPath()+"static\\pages\\forms\\"; 443 String path = this.getClass().getResource("/").getPath()+"static\\pages\\forms\\";
417 444
418 list.add(scheduleRealInfos.iterator()); 445 list.add(scheduleRealInfos.iterator());
419 - ee.excelReplace(list, new Object[] { scheduleRealInfo }, path+"mould\\waybill.xls", 446 + ee.excelReplace(list, new Object[] { scheduleReal,map }, path+"mould\\waybill.xls",
420 path+"export\\" + jName + ".xls"); 447 path+"export\\" + jName + ".xls");
421 return scheduleRealInfos; 448 return scheduleRealInfos;
422 } 449 }
src/main/java/com/bsth/util/ReportUtils.java
@@ -4,8 +4,12 @@ import java.io.File; @@ -4,8 +4,12 @@ import java.io.File;
4 import java.io.FileInputStream; 4 import java.io.FileInputStream;
5 import java.io.FileOutputStream; 5 import java.io.FileOutputStream;
6 import java.util.ArrayList; 6 import java.util.ArrayList;
  7 +import java.util.HashMap;
7 import java.util.Iterator; 8 import java.util.Iterator;
8 import java.util.List; 9 import java.util.List;
  10 +import java.util.Map;
  11 +import java.util.regex.Matcher;
  12 +import java.util.regex.Pattern;
9 13
10 import org.apache.poi.hssf.usermodel.HSSFCell; 14 import org.apache.poi.hssf.usermodel.HSSFCell;
11 import org.apache.poi.hssf.usermodel.HSSFCellStyle; 15 import org.apache.poi.hssf.usermodel.HSSFCellStyle;
@@ -65,8 +69,8 @@ public class ReportUtils { @@ -65,8 +69,8 @@ public class ReportUtils {
65 continue; 69 continue;
66 } 70 }
67 // 取得每列的内容,如果列内容是$key$格式,则替换内容 71 // 取得每列的内容,如果列内容是$key$格式,则替换内容
68 - key = cell.getStringCellValue();  
69 - if (key.indexOf("$") != -1 || key.indexOf("#list#") != -1) { 72 + key = getCellValue(cell);
  73 + if (key != null && (key.indexOf("$") != -1 || key.indexOf("#list#") != -1)) {
70 // * 列中内容有#list#,则表示该行为模板行,需要以该行为模板 74 // * 列中内容有#list#,则表示该行为模板行,需要以该行为模板
71 // * 例如:模板行格式 #list#0_0 $Car.id$ 75 // * 例如:模板行格式 #list#0_0 $Car.id$
72 // * 第一个0表示需要在list中取iterator的索引值 76 // * 第一个0表示需要在list中取iterator的索引值
@@ -86,8 +90,8 @@ public class ReportUtils { @@ -86,8 +90,8 @@ public class ReportUtils {
86 i += rowCount; 90 i += rowCount;
87 break; 91 break;
88 } else { 92 } else {
89 - // 直接填充数据的列,从对象数组中取得值  
90 - getValueAndSetCellValue(cell, key, tArray); 93 + // 直接填充数据的列,从对象数组中取得值,这里的数组不传值
  94 + getValueAndSetCellValue(cell, key, tArray, new String[]{""});
91 } 95 }
92 } 96 }
93 97
@@ -104,6 +108,47 @@ public class ReportUtils { @@ -104,6 +108,47 @@ public class ReportUtils {
104 } 108 }
105 } 109 }
106 110
  111 + public String getCellValue(HSSFCell cell) {
  112 + int cellType = 0;
  113 + String result = "";
  114 + double d;
  115 + if(cell != null) {
  116 + // 获取列数据类型
  117 + cellType = cell.getCellType();
  118 + // 不同列数据类型,取值方法不同
  119 + switch(cellType) {
  120 + // String
  121 + case HSSFCell.CELL_TYPE_STRING:
  122 + result = cell.getStringCellValue().toString();
  123 + break;
  124 + // numeric类型,excel中,日期格式会转成数字格式存储
  125 + case HSSFCell.CELL_TYPE_NUMERIC:
  126 + result = cell.getNumericCellValue()+"";
  127 + break;
  128 + // 公式类型
  129 + case HSSFCell.CELL_TYPE_FORMULA:
  130 + result = cell.getCellFormula() + "";
  131 + break;
  132 + // boolean类型
  133 + case HSSFCell.CELL_TYPE_BOOLEAN:
  134 + result = cell.getBooleanCellValue() + "";
  135 + break;
  136 + // 空格
  137 + case HSSFCell.CELL_TYPE_BLANK:
  138 + result = null;
  139 + break;
  140 + // 错误值
  141 + case HSSFCell.CELL_TYPE_ERROR:
  142 + result = null;
  143 + break;
  144 + default :
  145 + System.out.println("其它");
  146 + break;
  147 + }
  148 + }
  149 + return result;
  150 + }
  151 +
107 /** 152 /**
108 * 根据iterator,以及模板中的标识,填充模板 153 * 根据iterator,以及模板中的标识,填充模板
109 * 154 *
@@ -130,6 +175,7 @@ public class ReportUtils { @@ -130,6 +175,7 @@ public class ReportUtils {
130 HSSFRow newRow = null; 175 HSSFRow newRow = null;
131 int tmpCellNum = 0; 176 int tmpCellNum = 0;
132 int k = 0; 177 int k = 0;
  178 + int listIndex = 0;
133 // 取得模板行 179 // 取得模板行
134 HSSFRow orgRow = sheet.getRow(index); 180 HSSFRow orgRow = sheet.getRow(index);
135 try { 181 try {
@@ -152,7 +198,7 @@ public class ReportUtils { @@ -152,7 +198,7 @@ public class ReportUtils {
152 tmpCellNum = newRow.getLastCellNum(); 198 tmpCellNum = newRow.getLastCellNum();
153 for (int l = 0; l < tmpCellNum; l++) { 199 for (int l = 0; l < tmpCellNum; l++) {
154 cell = newRow.getCell(l); 200 cell = newRow.getCell(l);
155 - key = cell.getStringCellValue(); 201 + key = getCellValue(cell);
156 /** 202 /**
157 * 如果单无格内容为#list#,表示该行是模板行 #list#0_0 203 * 如果单无格内容为#list#,表示该行是模板行 #list#0_0
158 * 第一个0表示需要在list中取iterator的索引值 204 * 第一个0表示需要在list中取iterator的索引值
@@ -168,8 +214,10 @@ public class ReportUtils { @@ -168,8 +214,10 @@ public class ReportUtils {
168 if (key.trim().indexOf(" ") != -1) { 214 if (key.trim().indexOf(" ") != -1) {
169 key = key.split(" ")[1]; 215 key = key.split(" ")[1];
170 } 216 }
171 - getValueAndSetCellValue(cell, key, obj); 217 + getValueAndSetCellValue(cell, key, obj,new String[]{listIndex+""});
172 } 218 }
  219 + // list的数量
  220 + listIndex ++;
173 } 221 }
174 } catch (Exception e) { 222 } catch (Exception e) {
175 e.printStackTrace(); 223 e.printStackTrace();
@@ -180,42 +228,58 @@ public class ReportUtils { @@ -180,42 +228,58 @@ public class ReportUtils {
180 /** 228 /**
181 * 取到相应的值并填入相应的列中 229 * 取到相应的值并填入相应的列中
182 * 230 *
183 - * @param cell  
184 - * 列  
185 - * @param key  
186 - * 列内容  
187 - * @param obj  
188 - * 数据源对象 231 + * @param cell 列
  232 + * @param key 列内容
  233 + * @param obj 数据源对象
  234 + * @param args 其他参数 数组
  235 + * 数组内容:
  236 + * 0位:在list中的第几条数据
189 */ 237 */
190 - private void getValueAndSetCellValue(HSSFCell cell, String key, Object obj) { 238 + private void getValueAndSetCellValue(HSSFCell cell, String key, Object obj, String[] args) {
191 try { 239 try {
192 // 保有存单元格的内容 240 // 保有存单元格的内容
193 String cellValue = key = key.replace("\\n", ""); 241 String cellValue = key = key.replace("\\n", "");
194 String tmpKey; 242 String tmpKey;
195 - // 循环截取两个$中间的内容,反射出值  
196 - while (key.indexOf("$") != -1) {  
197 - key = key.substring(key.indexOf("$") + 1);  
198 - // 取两个$中间的内容  
199 - tmpKey = key.substring(0, key.indexOf("$"));  
200 - key = key.substring(key.indexOf("$") + 1);  
201 - // 如果内容是如下格式Cars.id,则从obj中值得相应的对象的值  
202 - if (tmpKey.indexOf(".") != -1) {  
203 - String className = tmpKey.substring(0, tmpKey.indexOf("."));  
204 - // 取得类的全限定名  
205 - String classWholeName = packaegName + className;  
206 - String fieldName = tmpKey.substring(tmpKey.indexOf(".") + 1);  
207 - // 如果obj是数组,循环判断哪个对象是对应的  
208 - if (obj instanceof Object[]) {  
209 - Object[] objs = (Object[]) obj;  
210 - for (int k = 0; k < objs.length; k++) {  
211 - if (objs[k].getClass().getName().equals(classWholeName)) {  
212 - cellValue = cellValue.replace("$" + tmpKey  
213 - + "$", getKeyValue(objs[k], fieldName)  
214 - + ""); 243 + // 判断单元格内容是否是公式
  244 + if(cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA){
  245 + Pattern p=Pattern.compile("(\\d+)"); //
  246 + Matcher m=p.matcher(key);
  247 + if(m.find()){
  248 + cellValue = key.replace(m.group(1),
  249 + Integer.valueOf(m.group(1))+Integer.valueOf(args[0])+"");
  250 + cell.setCellFormula(cellValue);
  251 + return;
  252 + }
  253 + }else{//其他格式
  254 +
  255 + // 循环截取两个$中间的内容,反射出值
  256 + while (key.indexOf("$") != -1) {
  257 + key = key.substring(key.indexOf("$") + 1);
  258 + // 取两个$中间的内容
  259 + tmpKey = key.substring(0, key.indexOf("$"));
  260 + key = key.substring(key.indexOf("$") + 1);
  261 + // 如果内容是如下格式Cars.id,则从obj中值得相应的对象的值
  262 + if (tmpKey.indexOf(".") != -1) {
  263 + String className = tmpKey.substring(0, tmpKey.indexOf("."));
  264 + // 取得类的全限定名
  265 + String classWholeName = packaegName + className;
  266 + String fieldName = tmpKey.substring(tmpKey.indexOf(".") + 1);
  267 + // 如果obj是数组,循环判断哪个对象是对应的
  268 + if (obj instanceof Object[]) {
  269 + Object[] objs = (Object[]) obj;
  270 + for (int k = 0; k < objs.length; k++) {
  271 + if (objs[k].getClass().getName().equals(classWholeName)) {
  272 + cellValue = cellValue.replace("$" + tmpKey
  273 + + "$", getKeyValue(objs[k], fieldName)
  274 + + "");
  275 + } else if(objs[k].getClass().getName().equals("java.util.HashMap")){
  276 + Map<String,Object> map = (HashMap<String,Object>)objs[k];
  277 + cellValue = cellValue.replace("$" + tmpKey + "$",map.get(fieldName)+"");
  278 + }
215 } 279 }
  280 + } else if (obj.getClass().getName().equals(classWholeName)) {
  281 + cellValue = cellValue.replace("$" + tmpKey + "$",getKeyValue(obj, fieldName) + "");
216 } 282 }
217 - } else if (obj.getClass().getName().equals(classWholeName)) {  
218 - cellValue = cellValue.replace("$" + tmpKey + "$",getKeyValue(obj, fieldName) + "");  
219 } 283 }
220 } 284 }
221 } 285 }
@@ -266,16 +330,16 @@ public class ReportUtils { @@ -266,16 +330,16 @@ public class ReportUtils {
266 srr.setXlName("abc11"); 330 srr.setXlName("abc11");
267 331
268 ScheduleRealInfo sr = new ScheduleRealInfo(); 332 ScheduleRealInfo sr = new ScheduleRealInfo();
269 - sr.setId((long) 22);  
270 - sr.setXlName("abc22"); 333 + sr.setZdsj("06:10");
  334 + sr.setZdsjActual("06:25");
271 dataList.add(sr); 335 dataList.add(sr);
272 sr = new ScheduleRealInfo(); 336 sr = new ScheduleRealInfo();
273 - sr.setId((long) 33);  
274 - sr.setXlName("abc33"); 337 + sr.setZdsj("06:20");
  338 + sr.setZdsjActual("");
275 dataList.add(sr); 339 dataList.add(sr);
276 list.add(dataList.iterator()); 340 list.add(dataList.iterator());
277 341
278 - ee.excelReplace(list, new Object[] { srr }, "D:\\55.xls", 342 + ee.excelReplace(list, new Object[] { srr }, "D:\\waybill.xls",
279 "D:\\22.xls"); 343 "D:\\22.xls");
280 System.out.println("ok"); 344 System.out.println("ok");
281 } catch (Exception e) { 345 } catch (Exception e) {
src/main/resources/static/pages/forms/mould/waybill.xls
No preview for this file type