|
@@ -26,20 +26,20 @@ import java.util.List;
|
|
/**
|
|
/**
|
|
* idea直接运行test,从底码表读取数据保存到pgsql
|
|
* idea直接运行test,从底码表读取数据保存到pgsql
|
|
* 1,需要手动修改必要参数 例如:
|
|
* 1,需要手动修改必要参数 例如:
|
|
- //文件路径
|
|
|
|
- String fileName = "C:\\Users\\24410\\Desktop\\data.xlsx";
|
|
|
|
- //测点code
|
|
|
|
- String code = "QSDJL.NX_GD_QSF_DD_P1_L1_001_ZXYG001";
|
|
|
|
- //开始日期
|
|
|
|
- String StringDate = "2023/6/1";
|
|
|
|
- //第几个sheet表
|
|
|
|
- int sheetValue = 1;
|
|
|
|
- //开始行
|
|
|
|
- int startRow = 2;
|
|
|
|
- //结束行
|
|
|
|
- int endRow = 29;
|
|
|
|
- //数据列
|
|
|
|
- int valueColumn = 3;
|
|
|
|
|
|
+ * //文件路径
|
|
|
|
+ * String fileName = "C:\\Users\\24410\\Desktop\\data.xlsx";
|
|
|
|
+ * //测点code
|
|
|
|
+ * String code = "QSDJL.NX_GD_QSF_DD_P1_L1_001_ZXYG001";
|
|
|
|
+ * //开始日期
|
|
|
|
+ * String StringDate = "2023/6/1";
|
|
|
|
+ * //第几个sheet表
|
|
|
|
+ * int sheetValue = 1;
|
|
|
|
+ * //开始行
|
|
|
|
+ * int startRow = 2;
|
|
|
|
+ * //结束行
|
|
|
|
+ * int endRow = 29;
|
|
|
|
+ * //数据列
|
|
|
|
+ * int valueColumn = 3;
|
|
*/
|
|
*/
|
|
@SpringBootTest
|
|
@SpringBootTest
|
|
public class ReadFromXLSXTest {
|
|
public class ReadFromXLSXTest {
|
|
@@ -53,100 +53,122 @@ public class ReadFromXLSXTest {
|
|
@Test
|
|
@Test
|
|
public void readDataFromFile() throws IOException {
|
|
public void readDataFromFile() throws IOException {
|
|
|
|
|
|
|
|
+ //开始日期
|
|
|
|
+ String StringDate = "2023/8/1";
|
|
//文件路径
|
|
//文件路径
|
|
String fileName = "C:\\Users\\24410\\Desktop\\data.xlsx";
|
|
String fileName = "C:\\Users\\24410\\Desktop\\data.xlsx";
|
|
|
|
+ String fileName1 = "C:\\Users\\24410\\Desktop\\无标题.xlsx";
|
|
|
|
+
|
|
//测点code
|
|
//测点code
|
|
- String code = "QSDJL.NX_GD_QSF_DD_P1_L1_001_ZXYG001";
|
|
|
|
- //开始日期
|
|
|
|
- String StringDate = "2023/6/1";
|
|
|
|
|
|
+ String code;
|
|
|
|
+
|
|
//第几个sheet表
|
|
//第几个sheet表
|
|
- int sheetValue = 1;
|
|
|
|
- //开始行
|
|
|
|
- int startRow = 2;
|
|
|
|
|
|
+ int sheetValue = 9;
|
|
//结束行
|
|
//结束行
|
|
- int endRow = 29;
|
|
|
|
|
|
+ int endRow = 68;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ //开始行
|
|
|
|
+ int startRow = 7;
|
|
//数据列
|
|
//数据列
|
|
- int valueColumn = 3;
|
|
|
|
-
|
|
|
|
- //根据code获取测点信息
|
|
|
|
- MeterPoint meterPoint = meterPointService.getOne(new QueryWrapper<MeterPoint>().eq("nem_code", code));
|
|
|
|
- //时间转为DateTime格式
|
|
|
|
- DateTime dateTime = DateUtil.parse(StringDate);
|
|
|
|
- //集合存所有记录
|
|
|
|
- List<MeterInfoBottomcode> records = new ArrayList<>();
|
|
|
|
|
|
+ int valueColumn;
|
|
|
|
+
|
|
|
|
+
|
|
//文件输入流
|
|
//文件输入流
|
|
- FileInputStream fileInputStream = new FileInputStream(fileName);
|
|
|
|
|
|
+ FileInputStream fileInputStream1 = new FileInputStream(fileName1);
|
|
//XSSFWorkbook格式的文件输入流
|
|
//XSSFWorkbook格式的文件输入流
|
|
- Workbook workbook = new XSSFWorkbook(fileInputStream);
|
|
|
|
|
|
+ Workbook workbook1 = new XSSFWorkbook(fileInputStream1);
|
|
//第几个sheet表,从0开始
|
|
//第几个sheet表,从0开始
|
|
- Sheet sheet = workbook.getSheetAt(sheetValue - 1);
|
|
|
|
|
|
+ Sheet sheet1 = workbook1.getSheetAt(0);
|
|
//开始天数,遍历累加以便推移时间
|
|
//开始天数,遍历累加以便推移时间
|
|
- int day = 0;
|
|
|
|
//遍历每行
|
|
//遍历每行
|
|
- for (int i = startRow-1; i < endRow; i += 2) {
|
|
|
|
- //每天时间
|
|
|
|
- DateTime dateTime1 = DateUtil.offsetDay(dateTime, day);
|
|
|
|
- //创建对象
|
|
|
|
- MeterInfoBottomcode record = new MeterInfoBottomcode();
|
|
|
|
- //对象赋值,开始时间,截止时间
|
|
|
|
- record.setStartTime(dateTime1.toLocalDateTime());
|
|
|
|
- record.setEndTime(DateUtil.endOfDay(dateTime1).toLocalDateTime());
|
|
|
|
- //对象赋值,名字,编码,场站
|
|
|
|
- record.setName(meterPoint.getName());
|
|
|
|
- record.setCode(meterPoint.getNemCode());
|
|
|
|
- record.setWindpowerstationId(meterPoint.getWindpowerstationId());
|
|
|
|
- //对象赋值,止码
|
|
|
|
- Row stopCodeRow = sheet.getRow(i);
|
|
|
|
- double stopCode = stopCodeRow.getCell(valueColumn - 1).getNumericCellValue();
|
|
|
|
- record.setEndValue(BigDecimal.valueOf(stopCode).setScale(4, RoundingMode.HALF_EVEN));
|
|
|
|
- //对象赋值,起码,
|
|
|
|
- Row electricityRow = sheet.getRow(i + 1);
|
|
|
|
- double electricity = electricityRow.getCell(valueColumn - 1).getNumericCellValue();
|
|
|
|
- record.setDayValue(BigDecimal.valueOf(electricity).setScale(4, RoundingMode.HALF_EVEN));
|
|
|
|
- //对象赋值,起码,第一天止码为上一行,其余为上2行
|
|
|
|
|
|
+ for (int j = 1; j <= sheet1.getLastRowNum(); j++) {
|
|
|
|
+ Row row = sheet1.getRow(j);
|
|
|
|
+ code = row.getCell(1).getStringCellValue();
|
|
|
|
+ valueColumn = (int) row.getCell(2).getNumericCellValue() ;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ //根据code获取测点信息
|
|
|
|
+ MeterPoint meterPoint = meterPointService.getOne(new QueryWrapper<MeterPoint>().eq("nem_code", code));
|
|
|
|
+ //时间转为DateTime格式
|
|
|
|
+ DateTime dateTime = DateUtil.parse(StringDate);
|
|
|
|
+ //集合存所有记录
|
|
|
|
+ List<MeterInfoBottomcode> records = new ArrayList<>();
|
|
|
|
+ //文件输入流
|
|
|
|
+ FileInputStream fileInputStream = new FileInputStream(fileName);
|
|
|
|
+ //XSSFWorkbook格式的文件输入流
|
|
|
|
+ Workbook workbook = new XSSFWorkbook(fileInputStream);
|
|
|
|
+ //第几个sheet表,从0开始
|
|
|
|
+ Sheet sheet = workbook.getSheetAt(sheetValue - 1);
|
|
|
|
+ //开始天数,遍历累加以便推移时间
|
|
|
|
+ int day = 0;
|
|
|
|
+ //遍历每行
|
|
|
|
+ for (int i = startRow - 1; i < endRow; i += 2) {
|
|
|
|
+ //每天时间
|
|
|
|
+ DateTime dateTime1 = DateUtil.offsetDay(dateTime, day);
|
|
|
|
+ //创建对象
|
|
|
|
+ MeterInfoBottomcode record = new MeterInfoBottomcode();
|
|
|
|
+ //对象赋值,开始时间,截止时间
|
|
|
|
+ record.setStartTime(dateTime1.toLocalDateTime());
|
|
|
|
+ record.setEndTime(DateUtil.endOfDay(dateTime1).toLocalDateTime());
|
|
|
|
+ //对象赋值,名字,编码,场站
|
|
|
|
+ record.setName(meterPoint.getName());
|
|
|
|
+ record.setCode(meterPoint.getNemCode());
|
|
|
|
+ record.setWindpowerstationId(meterPoint.getWindpowerstationId());
|
|
|
|
+ //对象赋值,止码
|
|
|
|
+ Row stopCodeRow = sheet.getRow(i);
|
|
|
|
+ double stopCode = stopCodeRow.getCell(valueColumn - 1).getNumericCellValue();
|
|
|
|
+ record.setEndValue(BigDecimal.valueOf(stopCode).setScale(4, RoundingMode.HALF_EVEN));
|
|
|
|
+ //对象赋值,起码,
|
|
|
|
+ Row electricityRow = sheet.getRow(i + 1);
|
|
|
|
+ double electricity = electricityRow.getCell(valueColumn - 1).getNumericCellValue();
|
|
|
|
+ record.setDayValue(BigDecimal.valueOf(electricity).setScale(4, RoundingMode.HALF_EVEN));
|
|
|
|
+ //对象赋值,起码,第一天止码为上一行,其余为上2行
|
|
// if (i == startRow-1) {
|
|
// if (i == startRow-1) {
|
|
- //如果时间为当月第一天,起码为上一行,否则为上2行
|
|
|
|
- if (dateTime1.equals(DateUtil.beginOfMonth(dateTime1))) {
|
|
|
|
- Row startCodeRow = sheet.getRow(i - 1);
|
|
|
|
- double startCode = startCodeRow.getCell(valueColumn - 1).getNumericCellValue();
|
|
|
|
- record.setStartValue(BigDecimal.valueOf(startCode).setScale(4, RoundingMode.HALF_EVEN));
|
|
|
|
- } else {
|
|
|
|
- Row startCodeRow = sheet.getRow(i - 2);
|
|
|
|
- double startCode = startCodeRow.getCell(valueColumn - 1).getNumericCellValue();
|
|
|
|
- record.setStartValue(BigDecimal.valueOf(startCode).setScale(4, RoundingMode.HALF_EVEN));
|
|
|
|
|
|
+ //如果时间为当月第一天,起码为上一行,否则为上2行
|
|
|
|
+ if (dateTime1.equals(DateUtil.beginOfMonth(dateTime1))) {
|
|
|
|
+ Row startCodeRow = sheet.getRow(i - 1);
|
|
|
|
+ double startCode = startCodeRow.getCell(valueColumn - 1).getNumericCellValue();
|
|
|
|
+ record.setStartValue(BigDecimal.valueOf(startCode).setScale(4, RoundingMode.HALF_EVEN));
|
|
|
|
+ } else {
|
|
|
|
+ Row startCodeRow = sheet.getRow(i - 2);
|
|
|
|
+ double startCode = startCodeRow.getCell(valueColumn - 1).getNumericCellValue();
|
|
|
|
+ record.setStartValue(BigDecimal.valueOf(startCode).setScale(4, RoundingMode.HALF_EVEN));
|
|
|
|
+ }
|
|
|
|
+ //添加到集合
|
|
|
|
+ records.add(record);
|
|
|
|
+
|
|
|
|
+ day++;
|
|
}
|
|
}
|
|
- //添加到集合
|
|
|
|
- records.add(record);
|
|
|
|
-
|
|
|
|
- day++;
|
|
|
|
- }
|
|
|
|
|
|
|
|
- // 打印每天的数据记录
|
|
|
|
- System.setOut(new PrintStream(System.out, true, "UTF-8"));
|
|
|
|
- for (MeterInfoBottomcode record : records) {
|
|
|
|
- System.out.println("name: " + record.getName());
|
|
|
|
- System.out.println("startTime: " + record.getStartTime());
|
|
|
|
- System.out.println("起码: " + record.getStartValue());
|
|
|
|
- System.out.println("endTime: " + record.getEndTime());
|
|
|
|
- System.out.println("止码: " + record.getEndValue());
|
|
|
|
- System.out.println("dayValue: " + record.getDayValue());
|
|
|
|
- System.out.println("编码: " + record.getCode());
|
|
|
|
- System.out.println("场站: " + record.getWindpowerstationId());
|
|
|
|
- System.out.println();
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-// QueryWrapper<MeterInfoBottomcode> queryWrapper = new QueryWrapper<>();
|
|
|
|
-// queryWrapper
|
|
|
|
-// .eq("start_time", record.getStartTime())
|
|
|
|
-// .eq("code", record.getCode());
|
|
|
|
-// List<MeterInfoBottomcode> list = meterInfoBottomcodeService.list(queryWrapper);
|
|
|
|
-// if (list.size() > 0) {
|
|
|
|
-// record.update(queryWrapper);
|
|
|
|
-// } else {
|
|
|
|
-// record.insert();
|
|
|
|
-// }
|
|
|
|
|
|
+ // 打印每天的数据记录
|
|
|
|
+ System.setOut(new PrintStream(System.out, true, "UTF-8"));
|
|
|
|
+ for (MeterInfoBottomcode record : records) {
|
|
|
|
+ System.out.println("name: " + record.getName());
|
|
|
|
+ System.out.println("startTime: " + record.getStartTime());
|
|
|
|
+ System.out.println("起码: " + record.getStartValue());
|
|
|
|
+ System.out.println("endTime: " + record.getEndTime());
|
|
|
|
+ System.out.println("止码: " + record.getEndValue());
|
|
|
|
+ System.out.println("dayValue: " + record.getDayValue());
|
|
|
|
+ System.out.println("编码: " + record.getCode());
|
|
|
|
+ System.out.println("场站: " + record.getWindpowerstationId());
|
|
|
|
+ System.out.println();
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ QueryWrapper<MeterInfoBottomcode> queryWrapper = new QueryWrapper<>();
|
|
|
|
+ queryWrapper
|
|
|
|
+ .eq("start_time", record.getStartTime())
|
|
|
|
+ .eq("code", record.getCode());
|
|
|
|
+ List<MeterInfoBottomcode> list = meterInfoBottomcodeService.list(queryWrapper);
|
|
|
|
+ if (list.size() > 0) {
|
|
|
|
+ record.update(queryWrapper);
|
|
|
|
+ } else {
|
|
|
|
+ record.insert();
|
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|