From 82c1e7ced425f43781b0cd01290fad298f51e623 Mon Sep 17 00:00:00 2001
From: whyclj <1525436766@qq.com>
Date: 星期六, 29 六月 2019 13:27:41 +0800
Subject: [PATCH] Merge branch 'dev_lxw' of http://whyclj@118.89.139.230:10101/r/~whyclxw/sensor.git into dev_lxw

---
 sensor/src/com/fgkj/servlets/EchartPictureDowloadServlet.java |  272 ++++++++++++++++++-----------------------------------
 1 files changed, 94 insertions(+), 178 deletions(-)

diff --git a/sensor/src/com/fgkj/servlets/EchartPictureDowloadServlet.java b/sensor/src/com/fgkj/servlets/EchartPictureDowloadServlet.java
index 042730f..9f10e29 100644
--- a/sensor/src/com/fgkj/servlets/EchartPictureDowloadServlet.java
+++ b/sensor/src/com/fgkj/servlets/EchartPictureDowloadServlet.java
@@ -1,6 +1,9 @@
 package com.fgkj.servlets;
 
+import java.awt.image.BufferedImage;
+import java.io.ByteArrayInputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.UnsupportedEncodingException;
 import java.math.BigDecimal;
@@ -9,6 +12,7 @@
 import java.util.Date;
 import java.util.List;
 
+import javax.imageio.ImageIO;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
@@ -22,6 +26,7 @@
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 
 import com.fgkj.actions.ActionUtil;
+import com.google.gson.JsonSyntaxException;
 import com.google.gson.reflect.TypeToken;
 
 import sun.misc.BASE64Decoder;
@@ -33,6 +38,11 @@
 	protected void service(HttpServletRequest req, HttpServletResponse resp)
 			throws ServletException, IOException {
 		String pageName = req.getParameter("pageName");
+		String eName = req.getParameter("excelName");
+		if(eName != null && eName.length()>0){
+			excelName = eName;
+		}
+		
 		if (ServletUtils.isNotNull(pageName)) {
 			if ("charge-test".equalsIgnoreCase(pageName)) {
 				chargeExcelExprot(req, resp);				//瀵煎嚭鍗曚釜鍥捐〃				
@@ -42,122 +52,35 @@
 				//ExprotReport(req,resp);						//瀵煎嚭鎶ヨ〃
 			}else if("charge-test-monData".equalsIgnoreCase(pageName)){
 				//ExprotLD9MonTestData(req,resp);
+			}else if("normal".equalsIgnoreCase(pageName)){
+				ExprotNormalReport(req, resp);
 			}
 		}
 	}
 	
 	/**
-	 * 鍘嗗彶鏀剧數鏁版嵁椤甸潰涓鍑篖D9璁惧鐨勬斁鐢垫暟鎹�
-	 * @param req
-	 * @param resp
+	 * 瀵煎嚭閫氱敤琛ㄦ牸
 	 */
-	/*private void ExprotLD9MonTestData(HttpServletRequest req,
-			HttpServletResponse resp) {
-		String mon_vol_line = req.getParameter("mon_vol_line");			//鍗曚綋鐢靛帇鎶樼嚎鍥�
-		String group_vol_line = req.getParameter("group_vol_line");		//缁勭/鍦ㄧ嚎鐢靛帇鎶樼嚎鍥�
-		String test_curr_line = req.getParameter("test_curr_line");		//娴嬭瘯鐢垫祦鎶樼嚎鍥�
-		String batt_real_cap = req.getParameter("batt_real_cap");		//鍗曚綋瀹為檯瀹归噺鏌辩姸鍥�
-		
-		String battinf = req.getParameter("battinf");					//鐢垫睜缁勪俊鎭�
-		String mon_test_data = req.getParameter("mon_test_data");		//鍗曚綋娴嬭瘯鏁版嵁
-		String testdatainfo = req.getParameter("testdatainfo");		//鍗曚綋娴嬭瘯鏁版嵁
-		
-		
-		bytes=new ArrayList<byte[]>();
-		
+	public void ExprotNormalReport(HttpServletRequest req, HttpServletResponse resp){
+		String[] picNames = null;
+		String[] picSours = null;
+		String[][] tabData = null;
 		try {
-			//鍗曚綋鐢靛帇鎶樼嚎鍥�
-			if (ServletUtils.isNotNull(mon_vol_line)) {
-				String[] url = mon_vol_line.split(",");
-				bytes.add(new BASE64Decoder().decodeBuffer(url[1]));
-				//System.out.println("1111111111111111111111111");
-			}
-			
-			//缁勭/鍦ㄧ嚎鐢靛帇鎶樼嚎鍥�
-			if (ServletUtils.isNotNull(group_vol_line)) {
-				String[] url = group_vol_line.split(",");
-				bytes.add(new BASE64Decoder().decodeBuffer(url[1]));
-				//System.out.println("2222222222222222222222222");
-			}
-			
-			//鍗曚綋鐢垫祦鏇茬嚎鍥�
-			if (ServletUtils.isNotNull(test_curr_line)) {
-				String[] url = test_curr_line.split(",");
-				bytes.add(new BASE64Decoder().decodeBuffer(url[1]));
-				//System.out.println("3333333333333333333333333333");
-			}
-			
-			//鍗曚綋鐢垫祦鏇茬嚎鍥�
-			if (ServletUtils.isNotNull(batt_real_cap)) {
-				String[] url = batt_real_cap.split(",");
-				bytes.add(new BASE64Decoder().decodeBuffer(url[1]));
-				//System.out.println("3333333333333333333333333333");
-			}
-			BattInf binf = ActionUtil.getGson().fromJson(battinf, BattInf.class);
-			Ld9testdata_inf mondatainfo = ActionUtil.getGson().fromJson(testdatainfo, Ld9testdata_inf.class);
-			List<Ld9testdata> montestdata = ActionUtil.getGson().fromJson(mon_test_data, new TypeToken<List<Ld9testdata>>(){}.getType());
-			createMonTestDataExcel(bytes,binf,montestdata,mondatainfo,resp,req);
-		} catch (IOException e) {
-			e.printStackTrace();
-		}
-		
-	}*/
-	
-	/*//鐢熸垚鍘嗗彶鏁版嵁涓璍D9鍗曚綋鏁版嵁鐨勬姤琛�
-	private void createMonTestDataExcel(List<byte[]> bytes,BattInf binf,List<Ld9testdata> montestdata,Ld9testdata_inf mondatainfo,HttpServletResponse resp,HttpServletRequest req) {
-		try {
+			picNames = req.getParameter("picNames")==null?null:ActionUtil.getGson().fromJson(req.getParameter("picNames"), String[].class);
+			picSours = req.getParameter("picSours")==null?null:ActionUtil.getGson().fromJson(req.getParameter("picSours"), String[].class);
+			tabData  = req.getParameter("tabData")==null?null:ActionUtil.getGson().fromJson(req.getParameter("tabData"), String[][].class);
+//			for(int i =0;i<picNames.length;i++) {
+//				System.out.print(picNames[i]+"\t");
+//				System.out.println(picSours[i]);
+//			}
 			// 鍒涘缓涓�涓伐浣滆杽
 			HSSFWorkbook wb = new HSSFWorkbook();
-			HSSFSheet sheet1 = wb.createSheet("鏁版嵁鎬昏〃");
-			// HSSFRow row = sheet1.createRow(2);
-			HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();
-			int rownum = 1;
-		
-			sheet1.createRow(rownum);
-			sheet1.getRow(rownum).createCell(1).setCellValue("鐢垫睜缁勫悕绉�");
-			sheet1.getRow(rownum).createCell(2).setCellValue(binf.getStationName()+"-"+binf.getBattGroupName());
-			rownum++;
-			
-			sheet1.createRow(rownum);
-			sheet1.getRow(rownum).createCell(1).setCellValue( "鐢垫睜鍝佺墝锛�"+binf.getBattProducer());
-			sheet1.getRow(rownum).createCell(2).setCellValue( "瀹夎鏃ユ湡锛�"+formartDate(binf.getBattInUseDate(),"yyyy-MM-dd"));
-			sheet1.getRow(rownum).createCell(3).setCellValue( "鍗曚綋鏁伴噺锛�"+binf.getMonCount());
-			sheet1.getRow(rownum).createCell(4).setCellValue( "鍗曚綋鐢靛帇锛�"+binf.getMonVolStd()+"V");
-			sheet1.getRow(rownum).createCell(5).setCellValue( "鍗曚綋鏍囩О瀹归噺锛�"+formartDouble(binf.getMonCapStd(),0)+"AH");
-			sheet1.getRow(rownum).createCell(6).setCellValue( "鍗曚綋鏍囩О鍐呴樆锛�"+formartDouble(binf.getMonResStd(),3)+"m惟");
-			sheet1.getRow(rownum).createCell(7).setCellValue( "鍗曚綋鏍囩О鐢靛锛�"+formartDouble(binf.getMonSerStd(),0));
-			
-			rownum++;
-			sheet1.createRow(rownum);
-			sheet1.getRow(rownum).createCell(1).setCellValue( "琚祴鍗曚綋缂栧彿锛�#"+mondatainfo.getMon_num());
-			sheet1.getRow(rownum).createCell(2).setCellValue( "娴嬭瘯绫诲瀷锛�"+binf.getBattGroupName5());
-			sheet1.getRow(rownum).createCell(3).setCellValue( "娴嬭瘯鏃堕棿锛�"+formartDate(mondatainfo.getTest_starttime(),"yyyy-MM-dd HH:mm:ss"));
-			sheet1.getRow(rownum).createCell(4).setCellValue( "娴嬭瘯鐢垫祦锛�"+mondatainfo.getTest_curr()+"A");
-			sheet1.getRow(rownum).createCell(5).setCellValue( "娴嬭瘯鏃堕暱锛�"+formatTestLong(mondatainfo.getTest_timelong()));
-			sheet1.getRow(rownum).createCell(6).setCellValue( "娴嬭瘯瀹归噺锛�"+formartDouble(mondatainfo.getTest_cap(),0)+"AH");
-			sheet1.getRow(rownum).createCell(7).setCellValue( "缁堟鍘熷洜锛�"+binf.getBattGroupName6());
-			
-			rownum++;
-			sheet1.createRow(rownum);
-			sheet1.getRow(rownum).createCell(1).setCellValue( "鎶ュ憡鏃ユ湡锛�");
-			sheet1.getRow(rownum).createCell(2).setCellValue(formartDate(new Date(),"yyyy-MM-dd HH:mm:ss"));
-			rownum+=2;
-			
-			int picnum = 0;
-			String[] picName = new String[]{
-					"鍗曚綋鐢靛帇鏇茬嚎鍥�","绔數鍘嬫洸绾垮浘","鍗曚綋鐢垫祦鏇茬嚎鍥�","鍗曚綋瀹為檯瀹归噺鏌辩姸鍥�"
-			};
-			for(int i=0;i<picName.length;i++){
-				sheet1.createRow(rownum);
-				sheet1.getRow(rownum).createCell(1).setCellValue(picName[i]);
-				rownum++;
-				HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 512, 255,(short) 1, rownum, (short) 10, 25 * (picnum + 1));
-				anchor.setAnchorType(3);
-				patriarch.createPicture(anchor,wb.addPicture(bytes.get(picnum),HSSFWorkbook.PICTURE_TYPE_PNG)).resize(1);
-				picnum ++;rownum+=25;
+			if(picNames != null && picSours != null){
+				ExprotReportPicData(wb,req,resp,picNames,picSours);
 			}
-
-			createNewLD9DataSheet(wb,montestdata);
+			if(tabData != null && tabData.length > 0){
+				ExprotReportTabData(wb,req,resp,tabData);
+			}
 			
 			// 杞爜闃叉涔辩爜
 			resp.addHeader("Content-Disposition", "attachment;filename="
@@ -166,81 +89,73 @@
 			OutputStream out = resp.getOutputStream();
 			wb.write(out);
 			out.close();
-		} catch (UnsupportedEncodingException e) {
+		} catch (JsonSyntaxException | IOException e) {
 			e.printStackTrace();
-		} catch (IOException e) {
-			e.printStackTrace();
-		}
-	}*/
-	
-	
-	/*public void createNewLD9DataSheet(HSSFWorkbook wb,final List<Ld9testdata> montestdata){
-		HSSFSheet sheet = wb.createSheet("鍗曚綋鏁版嵁琛�");
-		if(montestdata != null && montestdata.size()>0){
-				
-			int mon_count = 1;						//鎬诲崟浣撴暟閲�
-			int total_col = montestdata.size();		//鎬昏褰曟潯鏁�
-			
-			
-			List<String> tabTh = new ArrayList<String>(){{
-				this.add("鏃堕棿(HH:MM:SS)");this.add("缁勭鐢靛帇(V)");this.add("鍦ㄧ嚎鐢靛帇(V)");this.add("鐢垫祦(A)");this.add("娴嬭瘯瀹归噺(AH)");this.add("鍗曚綋鐢靛帇"+montestdata.get(0).getTest_monnum()+"(V)");
-			}};
-			//for(int i=1;i<=mon_count;i++){
-			//	tabTh.add("鍗曚綋鐢靛帇"+i+"(V)");
-			//}
-			
-			String[][] datas = new String[total_col][tabTh.size()];
-			
-			//System.out.println("data.length"+datas.length+"\t datas[0].length:"+datas[0].length);
-			
-			for(int j=0;j < datas.length;j++){
-				int currnum = 0;	
-				datas[j][currnum++] = formatTestLong(montestdata.get(j).getTest_timelong());
-				datas[j][currnum++] = formartDouble(montestdata.get(j).getGroup_vol(),1)+"";
-				datas[j][currnum++] = formartDouble(montestdata.get(j).getOnline_vol(),1)+"";
-				datas[j][currnum++] = formartDouble(montestdata.get(j).getTest_curr(),3)+"";
-				datas[j][currnum++] = formartDouble(montestdata.get(j).getTest_cap(),1)+"";
-				
-				
-				datas[j][currnum++] =formartDouble(montestdata.get(j).getMon_vol(),3)+"";
-				
-				//datas[j][currnum++] = "";
-				
-			}	
-			//System.out.println("mon_vol_list"+mon_vol_list.length+"\t mon_tmp_list:"+mon_tmp_list.length);
-			
-			int maxcol = 254;			//璁剧疆鏈�澶у垪鏁�
-			
-			int countReLine = (int) Math.ceil((double)total_col/maxcol);
-			int totalrow = countReLine *(tabTh.size()+1);					//excel鎬昏鏁�
-			for(int i=0;i<totalrow;i++){
-				sheet.createRow(i);
-			}
-			
-			//System.out.println("countReLine:"+countReLine);
-			
-			int currRow = 0;			//褰撳墠琛�
-			int arr_index = 0;			//褰撳墠鍒�
-			for(int i=0;i<countReLine;i++){
-				for(int col = 0;col <= maxcol && col<=datas.length && arr_index<datas.length;col++){					
-					if(col == 0){
-						for(int k=0;k<tabTh.size();k++){
-							sheet.getRow(k+currRow).createCell(col).setCellValue(tabTh.get(k));
-						}
-					}else{
-						for(int k = 0;k<datas[arr_index].length;k++){
-							sheet.getRow(k+currRow).createCell(col).setCellValue(datas[arr_index][k]);
-						}
-						arr_index++;
-					}
-				}
-				currRow += tabTh.size()+1;
-			}		
-			
-			//System.out.println("data.length"+datas.length+"\t datas[0].length:"+datas[0].length);
 		}
 	}
-	*/
+	
+	
+
+	private void ExprotReportTabData(HSSFWorkbook wb, HttpServletRequest req,
+			HttpServletResponse resp, String[][] tabData) {
+		HSSFSheet sheet1 = wb.createSheet("琛ㄦ牸鏁版嵁");
+		int rownum = 0;
+		for(int i=0;i<tabData.length;i++){
+			sheet1.createRow(rownum);
+			for(int j=0;j<tabData[i].length;j++){
+				sheet1.getRow(rownum).createCell(j+1).setCellValue(tabData[i][j]);
+			}
+			rownum++;
+		}
+	}
+
+	//瀵煎嚭澶氫釜鍥剧墖鏁版嵁
+	private void ExprotReportPicData(HSSFWorkbook wb, HttpServletRequest req,
+			HttpServletResponse resp,String[] picName,String[] picSource) {
+		HSSFSheet sheet1 = wb.createSheet("鍥剧墖鏁版嵁");
+		double[] picPX = new double[picSource.length];
+		bytes = new ArrayList<byte[]>();
+		//灏嗗浘鐗囨暟鎹浆鎹㈡垚瀛楄妭鏁扮粍
+		BASE64Decoder base64 = new BASE64Decoder();
+		//Base64瑙g爜  
+		byte[] b;
+		BufferedImage src = null;
+		for(int i = 0 ;i<picSource.length;i++ ){
+			try {
+				bytes.add(base64.decodeBuffer(picSource[i].split(",")[1]));
+				b = base64.decodeBuffer(picSource[i].split(",")[1]);
+				for (int k = 0; k < b.length; ++k) {  
+					if (b[k] < 0) {// 璋冩暣寮傚父鏁版嵁  
+						b[k] += 256;  
+					}  
+				}  
+				InputStream buffin = new ByteArrayInputStream(b);
+				src = ImageIO.read(buffin);
+				int widthYuan = src.getWidth(); // 寰楀埌婧愬浘瀹�
+		        int heightYuan = src.getHeight(); // 寰楀埌婧愬浘楂�
+				//System.out.println("width:"+widthYuan+"\theight:"+heightYuan+"\t鍗曞厓鏍煎:"+swidth+"\t鍗曞厓鏍奸珮"+sheight);
+				picPX[i] = (double)widthYuan/(heightYuan*4);
+			} catch (IOException e) {
+				e.printStackTrace();
+			}
+		}
+		int rownum = 0;
+		//鍒涘缓鍥剧墖
+		if(bytes.size() > 0){			
+			HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();
+			for(int i=0;i<bytes.size();i++){
+				sheet1.createRow(rownum);
+				sheet1.getRow(rownum).createCell(0).setCellValue(i>=picName.length?"":picName[i]);
+				rownum++;
+				
+						
+				HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 512, 255,(short) 0, rownum, (short) Math.ceil(25*picPX[i]), 25 + rownum-1);
+				anchor.setAnchorType(3);
+				patriarch.createPicture(anchor,wb.addPicture(bytes.get(i),HSSFWorkbook.PICTURE_TYPE_PNG));
+				rownum+=26;
+			}
+		}
+	}
 
 	/**
 	 * 瀵煎嚭鎶ヨ〃
@@ -682,4 +597,5 @@
 	public static double formartDouble(double b , int places){
 		return new   BigDecimal(b).setScale(places,   BigDecimal.ROUND_HALF_UP).doubleValue();  
 	}
+	
 }

--
Gitblit v1.9.1