



























package com.bytter.ext.test; import jxl.Workbook; import jxl.WorkbookSettings; import jxl.write.Label; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; public class TestYidong { public static void main(String[] args) throws Exception{ try { // 指定Excel文件路径 String filePath = "C:\\Users\\zuiai\\Desktop\\example.xls"; File file = new File(filePath); // 创建WorkbookSettings对象,可以设置编码等属性 WorkbookSettings wbSettings = new WorkbookSettings(); // 创建WritableWorkbook对象,用于写入数据 WritableWorkbook workbook = Workbook.createWorkbook(file, wbSettings); workbook.createSheet("Sheet1", 0); // 创建工作表Sheet1 WritableSheet sheet = workbook.getSheet(0); // 获取第一张工作表 File folder = new File("E:\\software\\workspace\\ysjh_yunnan\\tdesignsign-dev"); String keyword = "/api/audit/ysjh"; File[] listOfFiles = folder.listFiles(); int index = 0; // 写入数据到第一列和第二列,从第1行开始(Excel中第1行实际上是第0行) Label label1 = new Label(0, index, "文件路径"); // 第1列第1行(0-based index) Label label2 = new Label(1, index, "请求路径"); // 第2列第1行(0-based index) sheet.addCell(label1); // 添加到工作表 sheet.addCell(label2); // 添加到工作表 index++; outs(listOfFiles,keyword,sheet,index); // 写入完成后,关闭workbook以保存数据到文件 workbook.write(); workbook.close(); } catch (Exception e) { e.printStackTrace(); } } private static void outs(File[] listOfFiles, String keyword, WritableSheet sheet, int index) throws Exception{ if (listOfFiles != null) { for (File file : listOfFiles) { if (file.isFile()) { // 确保是文件 if(file.getName().endsWith(".js")){ System.out.println("File: " + file.getAbsolutePath()); try (BufferedReader br = new BufferedReader(new FileReader(file))) { String line; while ((line = br.readLine()) != null) { if (line.contains(keyword)) { // 写入数据到第一列和第二列,从第1行开始(Excel中第1行实际上是第0行) Label label1 = new Label(0, index, file.getAbsolutePath()); // 第1列第1行(0-based index) Label label2 = new Label(1, index, line); // 第2列第1行(0-based index) sheet.addCell(label1); // 添加到工作表 sheet.addCell(label2); // 添加到工作表 index++; } } } catch (IOException e) { e.printStackTrace(); } } }else{ outs(file.listFiles(),keyword,sheet,index); } } } } }
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。