国民体质测定标准手册及标准解析成JSON文件计算分数,java解析excel文件

Posted 穆雄雄

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了国民体质测定标准手册及标准解析成JSON文件计算分数,java解析excel文件相关的知识,希望对你有一定的参考价值。

大家好,我是雄雄,欢迎关注微信公众号:雄雄的小课堂

前言

现在是:2022年6月14日10:07:27

最近在做体质测评的功能,需要依据《国民体质测定标准手册及标准》,根据用户的个人信息,从而计算出各个指标的的分;大致思路就是先将国家提供的标准,整理成JSON文件,然后用java代码调用JSON数据,从而计算出得分。

涉及技能点

  1. java解析excel文件。
  2. 封装json格式的数据。

实现思路

  1. 将国家提供的标准表格文件复制到excel电子表格中。
  2. 解析excel表格,然后生成json文件。
  3. 根据个人信息,从JSON中查询对应的分数

代码实现

  1. 直接将标准表格复制到excel中,复制完成之后简单的修改修改,不然解析起来麻烦,如下所示:

  2. 解析excel的代码如下:

	/**
	 * 20-29 岁成年人身高标准体重评分表(男)
	 */
	public static void adultsWithHeightWeightStandardScoreSheet() 
		String excelPath = System.getProperty("user.dir")
			+ "/src/main/excelFile/women-50-59height_and_weight_score.xlsx";
		try 
			//String encoding = "GBK";
			File excel = new File(excelPath);
			//判断文件是否存在
			if (excel.isFile() && excel.exists()) 
				//.是特殊字符,需要转义!!!!!
				String[] split = excel.getName().split("\\\\.");
				Workbook wb;
				//根据文件后缀(xls/xlsx)进行判断
				if ("xls".equals(split[1])) 
					//文件流对象
					FileInputStream fis = new FileInputStream(excel);
					wb = new HSSFWorkbook(fis);
				 else if ("xlsx".equals(split[1])) 
					wb = new XSSFWorkbook(excel);
				 else 
					System.out.println("文件类型错误!");
					return;
				

				//开始解析
				//读取sheet 0
				Sheet sheet = wb.getSheetAt(0);

				//第一行
				int firstRowIndex = sheet.getFirstRowNum();
				//最后一行
				int lastRowIndex = sheet.getLastRowNum();
				System.out.println("一共有这么多行:"+lastRowIndex);
				System.out.println("**************开始执行**************");
				List<ScoreExcelVO>  scoreExcelVOList = new ArrayList<>();
				JSONArray jsonArrayResult = new JSONArray();
				//遍历行
				for (int rIndex = firstRowIndex; rIndex < lastRowIndex; rIndex++) 
					Row row = sheet.getRow(rIndex);
					if (row != null) 
						//身高
						Cell cell_height = row.getCell(0);
						String height = cell_height.toString();

						//1分
						Cell cell_one_score1 = row.getCell(1);
						String one_score1 = cell_one_score1.toString();

						//3分
						Cell cell_three_score = row.getCell(2);
						String three_score = cell_three_score.toString();

						//5分
						Cell cell_five_score = row.getCell(3);
						String five_score = cell_five_score.toString();

						//3分
						Cell cell_three_score2 = row.getCell(4);
						String three_score2 = cell_three_score2.toString();

						//1分
						Cell cell_one_score2 = row.getCell(5);
						String one_score2 = cell_one_score2.toString();

						//将值放在实体类中
						ScoreExcelVO scoreExcelVO = new ScoreExcelVO();
						scoreExcelVO.setHeight(height);
						scoreExcelVO.setFirstOneScore(one_score1);
						scoreExcelVO.setFirstThreeScore(three_score);
						scoreExcelVO.setFiveScore(five_score);
						scoreExcelVO.setSecondOneScore(one_score2);
						scoreExcelVO.setSecondThreeScore(three_score2);
						scoreExcelVOList.add(scoreExcelVO);
					
				

				JSONArray array = new JSONArray();
				//将集合转换成JSON格式的数据
				//JSONArray scoreExcelVOArray = JSONArray.parseArray(JSON.toJSONString(scoreExcelVOList));
				scoreExcelVOList.forEach(scoreExcelVO->
					JSONObject obj = new JSONObject();
					obj.put(scoreExcelVO.getHeight(),scoreExcelVO);
					array.add(obj);
				);
				String str = "";
				for(Object o : array)
					String old = o.toString().substring(1,o.toString().length()-1);
					str+=old+",";
				
				//将最后一个逗号截取掉
				str = str.substring(0,str.length()-1);
				str = ""+str+"";
				System.out.println(str);
			 else 
				System.out.println("找不到指定的文件");
			
		 catch (Exception e) 
			e.printStackTrace();
		
	

通过在main方法中调用,生成的json数据如下:

	
  "144.0-144.9": 
    "firstOneScore": "<36.6",
    "firstThreeScore": "36.6-37.6",
    "fiveScore": "37.7-48.2",
    "height": "144.0-144.9",
    "secondOneScore": ">52.3",
    "secondThreeScore": "48.3-52.3"
  ,
  "145.0-145.9": 
    "firstOneScore": "<37.1",
    "firstThreeScore": "37.1-38.1",
    "fiveScore": "38.2-49.0",
    "height": "145.0-145.9",
    "secondOneScore": ">53.0",
    "secondThreeScore": "49.1-53.0"
  ,
  "146.0-146.9": 
    "firstOneScore": "<37.7",
    "firstThreeScore": "37.7-38.6",
    "fiveScore": "38.7-49.8",
    "height": "146.0-146.9",
    "secondOneScore": ">53.8",
    "secondThreeScore": "49.9-53.8"
  ,
  "147.0-147.9": 
    "firstOneScore": "<38.3",
    "firstThreeScore": "38.3-39.2",
    "fiveScore": "39.3-50.6",
    "height": "147.0-147.9",
    "secondOneScore": ">54.6",
    "secondThreeScore": "50.7-54.6"
  ,
  "148.0-148.9": 
    "firstOneScore": "<38.9",
    "firstThreeScore": "38.9-39.7",
    "fiveScore": "39.8-51.4",
    "height": "148.0-148.9",
    "secondOneScore": ">55.4",
    "secondThreeScore": "51.5-55.4"
  ,
  "149.0-149.9": 
    "firstOneScore": "<39.9",
    "firstThreeScore": "39.9-40.4",
    "fiveScore": "40.5-52.1",
    "height": "149.0-149.9",
    "secondOneScore": ">56.2",
    "secondThreeScore": "52.2-56.2"
  ,
  "150.0-150.9": 
    "firstOneScore": "<40.5",
    "firstThreeScore": "40.5-41.1",
    "fiveScore": "41.2-52.9",
    "height": "150.0-150.9",
    "secondOneScore": ">57.1",
    "secondThreeScore": "53.0-57.1"
  ,
  "151.0-151.9": 
    "firstOneScore": "<41.0",
    "firstThreeScore": "41.0-41.7",
    "fiveScore": "41.8-53.8",
    "height": "151.0-151.9",
    "secondOneScore": ">58.0",
    "secondThreeScore": "53.9-58.0"
  ,
  "152.0-152.9": 
    "firstOneScore": "<41.6",
    "firstThreeScore": "41.6-42.4",
    "fiveScore": "42.5-54.6",
    "height": "152.0-152.9",
    "secondOneScore": ">59.0",
    "secondThreeScore": "54.7-59.0"
  ,
  "153.0-153.9": 
    "firstOneScore": "<42.2",
    "firstThreeScore": "42.2-43.2",
    "fiveScore": "43.3-55.6",
    "height": "153.0-153.9",
    "secondOneScore": ">59.8",
    "secondThreeScore": "55.7-59.8"
  ,
  "154.0-154.9": 
    "firstOneScore": "<42.8",
    "firstThreeScore": "42.8-44.0",
    "fiveScore": "44.1-56.7",
    "height": "154.0-154.9",
    "secondOneScore": ">60.9",
    "secondThreeScore": "56.8-60.9"
  ,
  "155.0-155.9": 
    "firstOneScore": "<43.4",
    "firstThreeScore": "43.4-44.7",
    "fiveScore": "44.8-57.8",
    "height": "155.0-155.9",
    "secondOneScore": ">61.9",
    "secondThreeScore": "57.9-61.9"
  ,
  "156.0-156.9": 
    "firstOneScore": "<44.0",
    "firstThreeScore": "44.0-45.4",
    "fiveScore": "45.5-58.8",
    "height": "156.0-156.9",
    "secondOneScore": ">62.9",
    "secondThreeScore": "58.9-62.9"
  ,
  "157.0-157.9": 
    "firstOneScore": "<44.5",
    "firstThreeScore": "44.5-46.0",
    "fiveScore": "46.1-59.7",
    "height": "157.0-157.9",
    "secondOneScore": ">64.0",
    "secondThreeScore": "59.8-64.0"
  ,
  "158.0-158.9": 
    "firstOneScore": "<45.0",
    "firstThreeScore": "45.0-46.9",
    "fiveScore": "47.0-61.8",
    "height": "158.0-158.9",
    "secondOneScore": ">65.1",
    "secondThreeScore": "61.9-65.1"
  ,
  "159.0-159.9": 
    "firstOneScore": "<45.5",
    "firstThreeScore": "45.5-47.6",
    "fiveScore": "47.7-61.9",
    "height": "159.0-159.9",
    "secondOneScore": ">66.1",
    "secondThreeScore": "62.0-66.1"
  ,
  "160.0-160.9": 
    "firstOneScore": "<46.0",
    "firstThreeScore": "46.0-48.5",
    "fiveScore": "48.6-62.9",
    "height": "160.0-160.9",
    "secondOneScore": ">67.2",
    "secondThreeScore": "63.0-67.2"
  ,
  "161.0-161.9": 
    "firstOneScore": "<46.7",
    "firstThreeScore": "46.7-49.2",
    "fiveScore": "49.3-63.8",
    "height": "161.0-161.9",
    "secondOneScore": ">68.2",
    "secondThreeScore": "63.9-68.2"
  ,
  "162.0-162.9": 
    "firstOneScore": "<47.3",
    "firstThreeScore": "47.3-50.1",
    "fiveScore": "50.2-64.9",
    "height": "162.0-162.9",
    "secondOneScore": ">69.0",
    "secondThreeScore": "65.0-69.0"
  ,
  "163.0-163.9": 
    "firstOneScore": "<47.8",
    "firstThreeScore": "47.8-51.0",
    "fiveScore": "51.1-65.9",
    "height": "163.0-163.9",
    "secondOneScore": ">70.1",
    "secondThreeScore": "66.0-70.1"
  ,
  "164.0-164.9": 
    "firstOneScore": "<48.4",
    "firstThreeScore": "48.4-51.6",
    "fiveScore": "51.7-67.0",
    "height": "164.0-164.9",
    "secondOneScore": ">71.0",
    "secondThreeScore": "67.1-71.0"
  ,
  "165.0-165.9": 
    "firstOneScore": "<48.9",
    "firstThreeScore": "48.9-52.2",
    "fiveScore": "52.3-67.8",
    "height": "165.0-165.9",
    "secondOneScore": ">72.1",
    "secondThreeScore": "67.9-72.1"
  ,
  "166.0-166.9": 
    "firstOneScore": "<49.4",
    "firstThreeScore": "49.4-53.0",
    "fiveScore": "53.1-68.7",
    "height": "166.0-166.9",
    "secondOneScore": ">72.9",
    "secondThreeScore": "68.8-72.9"
  ,
  "167.0-167.9": 
    "firstOneScore": "<49.9",
    "firstThreeScore": "49.9-53.6",
    "fiveScore": "53.7-69.6",
    "height": "167.0-167.9",
    "secondOneScore": ">73.8",
    "secondThreeScore": "69.7-73.8"
  ,
  "168.0-168.9": 
    "firstOneScore": "<50.5",
    "firstThreeScore": "50.0-54.3",
    "fiveScore": "54.4-70.4",
    "height": "168.0-168.9",
    "secondOneScore": ">75.0",
    "secondThreeScore": "70.5-75.0"
  ,
  "169.0-169.9": 
    "firstOneScore": "<51.2",
    "firstThreeScore": "51.2-55.0",
    "fiveScore": "55.1-71.2",
    "height": "169.0-169.9",
    "secondOneScore": ">75.9",
    "secondThreeScore": "71.3-75.9"
  ,
  "170.0-170.9": 
    "firstOneScore": "<52.0",
    "firstThreeScore": "52.0-55.7",
    "fiveScore": "55.8-72.1",
    "height": "170.0-170.9",
    "secondOneScore": ">76.8",
    "secondThreeScore": "72.2-76.8"
  ,
  "171.0-171.9": 
    "firstOneScore": "<52.7",
    "firstThreeScore": "52.7-56.6",
    "fiveScore": "56.7-73.1",
    "height": "171.0-171.9",
    "secondOneScore": ">77.9",
    "secondThreeScore": "73.2-77.9"
  ,
  "172.0-172.9": 
    "firstOneScore": "<53.5",
    "firstThreeScore": "53.5-57.5",
    "fiveScore": 计算机体系结构

剔除“异常值”的一般原则是什么?

python 标准模块之json 模块

[Python3]JSON解析

MP3格式音频文件结构解析

Openssl库之RSA格式解析