فهرست منبع

车辆基本信息增加导出功能

chenwen 3 سال پیش
والد
کامیت
21d4cd0f10

+ 39 - 0
src/main/java/com/hb/proj/car/controller/CarController.java

@@ -1,6 +1,8 @@
 package com.hb.proj.car.controller;
 package com.hb.proj.car.controller;
 
 
+import java.io.File;
 import java.util.Date;
 import java.util.Date;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Iterator;
 import java.util.Map;
 import java.util.Map;
@@ -10,11 +12,13 @@ import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import javax.servlet.http.HttpServletResponse;
 
 
 import org.apache.commons.lang.StringUtils;
 import org.apache.commons.lang.StringUtils;
+import org.apache.poi.ss.usermodel.Workbook;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 
 
 import com.hb.proj.car.service.CarService;
 import com.hb.proj.car.service.CarService;
+import com.hb.proj.excel.ExcelUtils;
 import com.hb.proj.excel.imp.ExcelDataExtractor;
 import com.hb.proj.excel.imp.ExcelDataExtractor;
 import com.hb.proj.excel.imp.ExcelResult;
 import com.hb.proj.excel.imp.ExcelResult;
 import com.hb.proj.sysdata.service.OrgService;
 import com.hb.proj.sysdata.service.OrgService;
@@ -28,6 +32,7 @@ import com.hb.proj.utils.RptMonthUtil;
 import com.hb.xframework.dao.util.PageModel;
 import com.hb.xframework.dao.util.PageModel;
 import com.hb.xframework.dao.util.UUIDHexGenerator;
 import com.hb.xframework.dao.util.UUIDHexGenerator;
 import com.hb.xframework.util.DateUtil;
 import com.hb.xframework.util.DateUtil;
+import com.hb.xframework.util.JsonParamHandler;
 import com.hb.xframework.util.MapUtils;
 import com.hb.xframework.util.MapUtils;
 import com.hb.xframework.util.SessionThreadLocal;
 import com.hb.xframework.util.SessionThreadLocal;
 import com.hb.xframework.util.SessionUser;
 import com.hb.xframework.util.SessionUser;
@@ -328,4 +333,38 @@ public class CarController {
 
 
 		return new String[]{StringUtils.join(carNums,","),(ignorMsg.length()>0?ignorMsg.toString():null)};
 		return new String[]{StringUtils.join(carNums,","),(ignorMsg.length()>0?ignorMsg.toString():null)};
 	}
 	}
+	
+	/**
+	 * 车辆基本信息导出
+	 * @param request
+	 * @param response
+	 */
+	@RequestMapping("/export")
+	public void export(HttpServletRequest request,HttpServletResponse response){
+		try{
+			MySessionUser su=(MySessionUser)SessionThreadLocal.getSessionUser();
+			Map<String,Object> args=JsonParamHandler.getMapParam(request.getParameter("where"));
+			args.put("showAll", su!=null&&su.isSysAdmin()?"1":null); //系统管理员显示全部
+			
+			PageModel<Map<String,Object>> pagedData=service.queryCar(args,DataAuthUtils.getAuthOrgAssistCode(su),1, 10000);
+			if(pagedData==null||pagedData.getData()==null||pagedData.getData().size()==0){
+				JsonOutUtils.writeToResponse(response, "<script>alert('暂时无数据可导出');window.close();</script>","text/html;charset=UTF-8");
+				return;
+			}
+			String crtMonth=DateUtil.format(new Date(), "yyyy-MM-dd");
+			for(Map<String,Object> car : pagedData.getData()){
+				car.put("realAge", RptMonthUtil.getWorkAge((String)car.get("wkStart"),crtMonth));
+			}
+			
+			String templateName=File.separator+"excel"+File.separator+"carExport.xls";
+			Map<String,Object> context=new HashMap<String,Object>();
+			context.put("ds", pagedData.getData());
+			Workbook wb=CarExportBound.bound(context,ExcelUtils.reader(templateName,request.getSession().getServletContext()));
+			ExcelUtils.exportExcel(response, "车辆基本信息", wb);
+		}
+		catch(Exception e){
+			e.printStackTrace();
+			JsonOutUtils.writeToResponse(response, "<script>alert('导出数据出现错误');window.close();</script>","text/html;charset=UTF-8");
+		}
+	}
 }
 }

+ 81 - 0
src/main/java/com/hb/proj/car/controller/CarExportBound.java

@@ -0,0 +1,81 @@
+package com.hb.proj.car.controller;
+
+import java.util.List;
+import java.util.Map;
+
+import org.apache.poi.hssf.util.HSSFColor.HSSFColorPredefined;
+import org.apache.poi.ss.usermodel.CellStyle;
+import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.Workbook;
+
+import com.hb.proj.excel.ExcelSupport;
+
+/**
+ * 车辆基本信息 导出
+ * @author 
+ *
+ */
+public class CarExportBound {
+
+	@SuppressWarnings("unchecked")
+	public static Workbook bound(Map<String,Object> context,Workbook wb){
+		Sheet sheet=wb.getSheetAt(0);
+		Row  row=null;
+		int parseIndex=1;  //开始解析的行索引,从0开始计数
+		while(!ExcelSupport.isNullRow(sheet, parseIndex))
+		{
+			 row=sheet.getRow(parseIndex);
+			
+			 if(parseIndex==1){
+				 String datasetName=ExcelSupport.getDatasetName(row.getCell(0));
+				 parseIndex=fillDatas((List<Map<String,Object>>)context.get(datasetName),sheet,row);
+				 continue;
+			 }
+			 context.put("r", parseIndex);
+			
+			 parseIndex++;
+		}
+        return wb;
+	}
+	public static int fillDatas(List<Map<String,Object>> dataset,Sheet sheet,Row row){
+		int dataRowStart=row.getRowNum();
+		if(dataset==null||dataset.size()==0){
+			sheet.removeRow(row);  //删除数据,保留行
+			return ++dataRowStart;
+		}
+		ExcelSupport.insertRow(sheet, dataRowStart, dataset.size()-1);  //模板行也显示数据,-1  特殊-2
+		Map<String,List<String>>  mapFields=ExcelSupport.getFields(row);
+		List<String> fields=mapFields.get("fields");
+		int rowIdx=row.getRowNum();
+		CellStyle css=null;
+		Short coloridx=null;
+		for(Map<String,Object> data : dataset){
+			row=sheet.getRow(rowIdx++);
+			for(int f=0,flen=fields.size();f<flen;f++){
+				if("recordNum".equals(fields.get(f))){
+					ExcelSupport.setValue(row.getCell(f),rowIdx-1);
+				}
+				else{
+					if("customNum".equals(fields.get(f))){  //自编号列单独渲染,托管车辆
+						coloridx=null;
+						if("1".equals(String.valueOf(data.get("discardIf")))){
+							coloridx=HSSFColorPredefined.RED.getIndex();
+						}
+						else if("是".equals(data.get("customTag"))){
+							coloridx=HSSFColorPredefined.BLUE.getIndex();
+						}
+						if(coloridx!=null){
+							css=ExcelSupport.createCellStyle(sheet.getWorkbook(),coloridx, (short)12);
+							row.getCell(f).setCellStyle(css);
+						}
+						
+					}
+					ExcelSupport.setValue(row.getCell(f),data.get(fields.get(f)));
+				}
+				
+			}
+		}
+		return rowIdx;
+	}
+}

+ 25 - 0
src/main/java/com/hb/proj/excel/ExcelSupport.java

@@ -11,10 +11,15 @@ import java.util.regex.Pattern;
 
 
 import org.apache.commons.lang.StringUtils;
 import org.apache.commons.lang.StringUtils;
 import org.apache.poi.hssf.usermodel.HSSFDateUtil;
 import org.apache.poi.hssf.usermodel.HSSFDateUtil;
+import org.apache.poi.ss.usermodel.BorderStyle;
 import org.apache.poi.ss.usermodel.Cell;
 import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.CellStyle;
 import org.apache.poi.ss.usermodel.CellType;
 import org.apache.poi.ss.usermodel.CellType;
+import org.apache.poi.ss.usermodel.Font;
 import org.apache.poi.ss.usermodel.Row;
 import org.apache.poi.ss.usermodel.Row;
 import org.apache.poi.ss.usermodel.Sheet;
 import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.VerticalAlignment;
+import org.apache.poi.ss.usermodel.Workbook;
 import org.apache.poi.ss.util.CellRangeAddress;
 import org.apache.poi.ss.util.CellRangeAddress;
 
 
 import com.hb.xframework.sqlmapper.parse.OgnlExecuter;
 import com.hb.xframework.sqlmapper.parse.OgnlExecuter;
@@ -51,6 +56,26 @@ public class ExcelSupport {
 		return cellValue;
 		return cellValue;
 	}
 	}
 	
 	
+	
+	public static CellStyle createCellStyle(Workbook workbook,short color,short size){
+		// 设置字体
+		CellStyle cellStyle = workbook.createCellStyle();
+		Font redFont = workbook.createFont();
+		//颜色
+		redFont.setColor(color);
+		//设置字体大小
+		redFont.setFontHeightInPoints(size);
+		//字体
+		//redFont.setFontName("宋体");
+		cellStyle.setFont(redFont);
+		
+		cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
+		
+		cellStyle.setBorderBottom(BorderStyle.THIN);
+		 
+		return cellStyle;
+	}
+	
 	/**
 	/**
 	 * 设置单元格的值
 	 * 设置单元格的值
 	 * @param cell
 	 * @param cell

BIN
src/main/webapp/excel/carExport.xls


+ 2 - 0
src/main/webapp/page/car/car.html

@@ -56,6 +56,7 @@
 														      <button type="button" class="layui-btn" lay-filter="querybtn">开始检索</button>
 														      <button type="button" class="layui-btn" lay-filter="querybtn">开始检索</button>
 															  <button type="button" class="layui-btn layui-btn-danger" lay-filter="clearbtn">清空</button>
 															  <button type="button" class="layui-btn layui-btn-danger" lay-filter="clearbtn">清空</button>
 															  <button type="button" class="layui-btn layui-btn-normal" lay-filter="addbtn" id="addbtn">登记车辆</button>
 															  <button type="button" class="layui-btn layui-btn-normal" lay-filter="addbtn" id="addbtn">登记车辆</button>
+															  <button type="button" class="layui-btn layui-btn-normal" lay-filter="exportbtn" id="exportbtn">导出</button>
 															
 															
 															  
 															  
 														  </div>	
 														  </div>	
@@ -80,6 +81,7 @@
 			  </div>
 			  </div>
 			</div>
 			</div>
 		</div>
 		</div>
+		<iframe name="expfrm" style="display:none;"></iframe>
 		<script src="../../js/layui/layui.all.js"></script>
 		<script src="../../js/layui/layui.all.js"></script>
 		<script src="../../js/layui/global.js"></script>
 		<script src="../../js/layui/global.js"></script>
 		<script src="../js/car.js"></script>
 		<script src="../js/car.js"></script>

+ 7 - 0
src/main/webapp/page/js/car.js

@@ -110,6 +110,13 @@ layui.use(['element','mylayer','newtable','laydate','myform','page'], function (
 					$(".my-query-form").on("click",".layui-btn[lay-filter='importTemp']",function(evn){
 					$(".my-query-form").on("click",".layui-btn[lay-filter='importTemp']",function(evn){
 						window.open("../../excel/车辆基本信息导入模板.xls");
 						window.open("../../excel/车辆基本信息导入模板.xls");
 					});	
 					});	
+					
+					$(".my-query-form").on("click",".layui-btn[lay-filter='exportbtn']",function(evn){
+						layer.msg("开始导出数据");
+						//var args=form.val("my-query-form");
+						window.frames['expfrm'].location='car/export.do?where='+encodeURIComponent(JSON.stringify(form.val("my-query-form")));
+						
+					});
 				
 				
 					$("#addbtn").on("click",function(){
 					$("#addbtn").on("click",function(){
 						layer.open({
 						layer.open({