|
@@ -0,0 +1,220 @@
|
|
|
+package com.miaxis.test;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+import com.miaxis.NbjkApplication;
|
|
|
+import com.miaxis.dict.domain.DictCity;
|
|
|
+import com.miaxis.dict.service.IDictCityService;
|
|
|
+import com.miaxis.exam.domain.ExamInfo;
|
|
|
+import com.miaxis.exam.service.IExamInfoService;
|
|
|
+import com.miaxis.wx.service.IWxGzhService;
|
|
|
+import org.apache.poi.ss.usermodel.*;
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
+import org.junit.Test;
|
|
|
+import org.junit.runner.RunWith;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.boot.test.context.SpringBootTest;
|
|
|
+import org.springframework.test.context.ActiveProfiles;
|
|
|
+import org.springframework.test.context.junit4.SpringRunner;
|
|
|
+
|
|
|
+@ActiveProfiles("dev")
|
|
|
+@SpringBootTest(classes = NbjkApplication.class)
|
|
|
+@RunWith(SpringRunner.class)
|
|
|
+public class ReadFileNameTest {
|
|
|
+
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IDictCityService dictCityService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IExamInfoService examInfoService;
|
|
|
+
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void test() {
|
|
|
+ String folderPath = "H:/河南省"; // 替换为你的文件夹路径
|
|
|
+ String excelFilePath = "H:/file.xlsx"; // 替换为你的Excel文件路径
|
|
|
+
|
|
|
+ try {
|
|
|
+ Workbook workbook = new XSSFWorkbook();
|
|
|
+ Sheet sheet = workbook.createSheet("Folders");
|
|
|
+
|
|
|
+ // 创建表头
|
|
|
+ Row headerRow = sheet.createRow(0);
|
|
|
+ headerRow.createCell(0).setCellValue("id");
|
|
|
+ headerRow.createCell(1).setCellValue("video_name");
|
|
|
+ headerRow.createCell(2).setCellValue("seq");
|
|
|
+ headerRow.createCell(3).setCellValue("video_cover");
|
|
|
+ headerRow.createCell(4).setCellValue("video_url");
|
|
|
+ headerRow.createCell(5).setCellValue("video_subject");
|
|
|
+ headerRow.createCell(6).setCellValue("state");
|
|
|
+ headerRow.createCell(7).setCellValue("horizontal");
|
|
|
+ headerRow.createCell(8).setCellValue("province_id");
|
|
|
+ headerRow.createCell(9).setCellValue("province");
|
|
|
+ headerRow.createCell(10).setCellValue("city_id");
|
|
|
+ headerRow.createCell(11).setCellValue("city");
|
|
|
+ headerRow.createCell(12).setCellValue("county_id");
|
|
|
+ headerRow.createCell(13).setCellValue("county");
|
|
|
+ headerRow.createCell(14).setCellValue("create_time");
|
|
|
+ headerRow.createCell(15).setCellValue("update_time");
|
|
|
+ headerRow.createCell(16).setCellValue("exam_id");
|
|
|
+ headerRow.createCell(17).setCellValue("exam_name");
|
|
|
+ headerRow.createCell(18).setCellValue("permission");
|
|
|
+
|
|
|
+
|
|
|
+ // 获取根文件夹
|
|
|
+ File rootFolder = new File(folderPath);
|
|
|
+
|
|
|
+ // 递归遍历文件夹
|
|
|
+ traverseFolders(rootFolder, sheet, 1);
|
|
|
+
|
|
|
+ // 将工作簿写入Excel文件
|
|
|
+ FileOutputStream outputStream = new FileOutputStream(excelFilePath);
|
|
|
+ workbook.write(outputStream);
|
|
|
+ workbook.close();
|
|
|
+ outputStream.close();
|
|
|
+
|
|
|
+ System.out.println("Excel文件生成成功!");
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void traverseFolders(File folder, Sheet sheet, int rowIndex) {
|
|
|
+ String province = folder.getName();
|
|
|
+ String provinceId = "41";
|
|
|
+ String city = null;
|
|
|
+ String cityId = null;
|
|
|
+ String county = null;
|
|
|
+ String countyId = null;
|
|
|
+ String exam = null;
|
|
|
+
|
|
|
+ String video = null;
|
|
|
+ if (folder.isDirectory()) {
|
|
|
+ File[] files = folder.listFiles();
|
|
|
+ for (int i = 0; i < files.length; i++) { //市级
|
|
|
+ // System.out.println(files[i].getName());
|
|
|
+ city = files[i].getName();
|
|
|
+ List<DictCity> dictCityList = dictCityService.getDictCityByName(city);
|
|
|
+
|
|
|
+ for (int j = 0; j < dictCityList.size(); j++) {
|
|
|
+ cityId = dictCityList.get(j).getCode();
|
|
|
+ if(cityId.startsWith("41")){
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if(files[i].isDirectory()) {
|
|
|
+ File[] files2 = files[i].listFiles();
|
|
|
+ for (int j = 0; j < files2.length; j++) { //区级
|
|
|
+ // System.out.println(" "+files2[j].getName());
|
|
|
+ county = files2[j].getName();
|
|
|
+ List<DictCity> dictCity2List = dictCityService.getDictCityByName(county);
|
|
|
+
|
|
|
+ for (int x = 0; x < dictCity2List.size(); x++) {
|
|
|
+ countyId = dictCity2List.get(x).getCode();
|
|
|
+ if(countyId.startsWith(cityId)){
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if(files2[j].isDirectory()) {
|
|
|
+ File[] files3 = files2[j].listFiles();
|
|
|
+ for (int k = 0; k < files3.length; k++) { //考场
|
|
|
+ // System.out.println(" "+files3[k].getName());
|
|
|
+ exam = files3[k].getName();
|
|
|
+ ExamInfo examInfo = new ExamInfo();
|
|
|
+ examInfo.setName(exam);
|
|
|
+ ExamInfo examInfo2 = null;
|
|
|
+ List<ExamInfo> list = examInfoService.selectOldExamInfoList(examInfo);
|
|
|
+ if(list!=null && list.size()>0) {
|
|
|
+ examInfo2 = list.get(0);
|
|
|
+ }
|
|
|
+ if(files3[k].isDirectory()){
|
|
|
+ File[] files4 = files3[k].listFiles();
|
|
|
+ for (int l = 0; l < files4.length; l++) {
|
|
|
+ // System.out.println(" "+files4[l].getName());
|
|
|
+ video = files4[l].getName();
|
|
|
+ video = video.substring(0,video.indexOf("."));
|
|
|
+ //写入excel
|
|
|
+ Row row = sheet.createRow(rowIndex);
|
|
|
+ row.createCell(0).setCellValue("");
|
|
|
+ row.createCell(1).setCellValue(video);
|
|
|
+ row.createCell(2).setCellValue(l+1);
|
|
|
+ row.createCell(3).setCellValue("");
|
|
|
+ row.createCell(4).setCellValue("");
|
|
|
+ row.createCell(5).setCellValue("3");
|
|
|
+ row.createCell(6).setCellValue("1");
|
|
|
+ row.createCell(7).setCellValue("1");
|
|
|
+ row.createCell(8).setCellValue(provinceId);
|
|
|
+ row.createCell(9).setCellValue(province);
|
|
|
+ row.createCell(10).setCellValue(cityId);
|
|
|
+ row.createCell(11).setCellValue(city);
|
|
|
+ row.createCell(12).setCellValue(countyId);
|
|
|
+ row.createCell(13).setCellValue(county);
|
|
|
+ row.createCell(14).setCellValue("");
|
|
|
+ row.createCell(15).setCellValue("");
|
|
|
+ if(examInfo2!=null) {
|
|
|
+ row.createCell(16).setCellValue(examInfo2.getId());
|
|
|
+ row.createCell(17).setCellValue(examInfo2.getName());
|
|
|
+ } else {
|
|
|
+ row.createCell(16).setCellValue("");
|
|
|
+ row.createCell(17).setCellValue("");
|
|
|
+ }
|
|
|
+ if (video.indexOf("试看")>=0) {
|
|
|
+ row.createCell(18).setCellValue("1");
|
|
|
+ } else {
|
|
|
+ row.createCell(18).setCellValue("2");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ System.out.println(province+" "+city+" "+county+" "+exam+" "+video);
|
|
|
+ rowIndex++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+// if (files != null) {
|
|
|
+// for (File file : files) {
|
|
|
+// if (file.isDirectory()) {
|
|
|
+// // 获取文件夹路径
|
|
|
+// String folderPath = file.getPath();
|
|
|
+//
|
|
|
+// // 分割文件夹路径
|
|
|
+// String[] folderNames = folderPath.split("\\\\");
|
|
|
+// int numLevels = folderNames.length;
|
|
|
+//
|
|
|
+// // 创建新行
|
|
|
+// Row row = sheet.createRow(rowIndex);
|
|
|
+// for (int i = 0; i < numLevels; i++) {
|
|
|
+// row.createCell(i).setCellValue(folderNames[i]);
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 获取mp4文件
|
|
|
+// File[] mp4Files = file.listFiles((dir, name) -> name.toLowerCase().endsWith(".mp4"));
|
|
|
+// if (mp4Files != null && mp4Files.length > 0) {
|
|
|
+// String mp4FileName = mp4Files[0].getName();
|
|
|
+// row.createCell(numLevels).setCellValue(mp4FileName);
|
|
|
+// }
|
|
|
+//
|
|
|
+// rowIndex++;
|
|
|
+//
|
|
|
+// // 递归遍历下一级文件夹
|
|
|
+// traverseFolders(file, sheet, rowIndex);
|
|
|
+// }
|
|
|
+// }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+
|