赞
踩
思路:
解析excel,读取vin,利用demo json用在线工具生成实体类,组装成一个集合,序列化输出到txt文件中。
程序:
package com.mtons.mblog.reach; import com.alibaba.fastjson.JSON; import com.mtons.mblog.BootApplication; import com.mtons.mblog.base.utils.BeanParseException; import com.mtons.mblog.base.utils.ExcelUtil; import lombok.extern.slf4j.Slf4j; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.io.File; import java.io.IOException; import java.io.PrintStream; import java.util.ArrayList; import java.util.List; @RunWith(SpringRunner.class) @SpringBootTest(classes = BootApplication.class) @Slf4j public class TestCalc { /** * 可以用来读取数据 * @throws IOException * @throws */ @Test public void testAtMethod() throws IOException, BeanParseException { try { Root root = new Root(); List<BatteryDataDTOList> batteryDataDTOSet = new ArrayList<>(); //BatteryDataDTOList batteryDataDTOList = new BatteryDataDTOList(); //把读到的数据写到这个文件中 //E:\xxx 没有这个文件夹会报错 PrintStream out = new PrintStream("D:\\reach\\reach.txt"); //一下代码所有的system.out.println() 输出到xxx.txt文件中 System.setOut(out); //文件夹路径 excel文件 String[][] strings = ExcelUtil.readFromFile(new File("D:\\yyyy", "vin_import.xlsx"), 9, 2000); //r是行数 for (int r = 1; r < strings.length; r++) { //这里0 1 2 列数 //vin String vin = strings[r][0]; //电池包编码 String batteryPackageCode = strings[r][1]; //电池模块编码 String batteryModuleCode = strings[r][2]; //电池单体编码 电蕊编码 String batteryCellCode = strings[r][3]; BatteryDataDTOList batteryDataDTOList = new BatteryDataDTOList(); batteryDataDTOList.setVin(vin); batteryDataDTOList.setBatteryPackageCode(batteryPackageCode); batteryDataDTOList.setBatteryModuleCode(batteryModuleCode); batteryDataDTOList.setBatteryCellCode(batteryCellCode); batteryDataDTOList.setBatteryType("4"); batteryDataDTOList.setBatteryCodeType("1"); batteryDataDTOList.setBatteryPackageCompany("江苏时代新能源科技有限公司"); batteryDataDTOList.setBatteryPackageVoltage("346"); batteryDataDTOList.setBatteryPackageCapacity("177"); if(vin.equals("LHGJ11882M8000015")){ batteryDataDTOList.setBatteryPackageDate("2020-04-23"); }else if(vin.equals("LHGJ11886M8000020")){ batteryDataDTOList.setBatteryPackageDate("2020-05-09"); }else if(vin.equals("LHGJ1188XM8000019")){ batteryDataDTOList.setBatteryPackageDate("2020-04-22"); }else if(vin.equals("LHGJ11880M8000028")){ batteryDataDTOList.setBatteryPackageDate("2020-06-07"); }else if(vin.equals("LHGJ11882M8000029")){ batteryDataDTOList.setBatteryPackageDate("2020-06-07"); } batteryDataDTOList.setBatteryModuleVoltage("14.44"); batteryDataDTOList.setBatteryModuleCapacity("177"); batteryDataDTOList.setBatteryCellCompany("江苏时代新能源科技有限公司"); batteryDataDTOList.setBatteryCellVoltage("3.61"); batteryDataDTOList.setBatteryCellCapacity("177"); batteryDataDTOSet.add(batteryDataDTOList); } root.setBatteryDataDTOList(batteryDataDTOSet); System.out.println(JSON.toJSONString(root)); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) throws IOException, BeanParseException { new TestCalc().testAtMethod(); } }
输出(部分数据):
{ "batteryDataDTOList": [{ "batteryCellCapacity": "177", "batteryCellCode": "001CE280000002A4W0702153", "batteryCellCompany": "江苏时代新能源科技有限公司", "batteryCellVoltage": "3.61", "batteryCodeType": "1", "batteryModuleCapacity": "177", "batteryModuleCode": "001MEAHA000002A660000028", "batteryModuleVoltage": "14.44", "batteryPackageCapacity": "177", "batteryPackageCode": "001PEAEF000002A670300002", "batteryPackageCompany": "江苏时代新能源科技有限公司", "batteryPackageDate": "2020-06-07", "batteryPackageVoltage": "346", "batteryType": "4", "vin": "LHGJ11880M8000028" }, { "batteryCellCapacity": "177", "batteryCellCode": "001CE280000002A4W0702058", "batteryCellCompany": "江苏时代新能源科技有限公司", "batteryCellVoltage": "3.61", "batteryCodeType": "1", "batteryModuleCapacity": "177", "batteryModuleCode": "001MEAHA000002A660000028", "batteryModuleVoltage": "14.44", "batteryPackageCapacity": "177", "batteryPackageCode": "001PEAEF000002A670300002", "batteryPackageCompany": "江苏时代新能源科技有限公司", "batteryPackageDate": "2020-06-07", "batteryPackageVoltage": "346", "batteryType": "4", "vin": "LHGJ11880M8000028" }] }
返回:
{
"code": "0",
"message": "OK",
"data": "",
"recordsFiltered": 0,
"recordsTotal": 0
}
参考博客:
https://blog.csdn.net/yunfengfengfeng/article/details/106207159
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。