/** * ディレクトリを探索し、ファイル情報をリストに格納する。 * * @param path */ public void walk(String path) throws Exception { File root = new File(path); File[] list = root.listFiles(); if (list == null) return; for (File f : list) { if (f.isDirectory()) { if (isExcludeDir(f)) { continue; } walk(f.getAbsolutePath()); } else { String fileName = f.getName(); if (!"結果データ.xls".equals(fileName)) { continue; } TreeSet<String> sheetNameList = getSheetNameList(f); EmulFileSheetInfo fileInfo = new EmulFileSheetInfo(); fileInfo.setSheetNameList(sheetNameList); fileInfo.setPath(f.getAbsolutePath()); fileInfo.setMethodName(f.getParentFile().getName()); fileInfo.setClassName(f.getParentFile().getParentFile().getName()); fileInfoList.add(fileInfo); System.out.println("File:" + f.getAbsoluteFile()); } } }
/** * ファイル情報をExcelへ出力する * * @param outputFilePath 出力ファイルパス */ public void makeXls(String outputFilePath) { FileOutputStream out = null; try { // 新規ワークブックを作成する HSSFWorkbook wb = new HSSFWorkbook(); out = new FileOutputStream(outputFilePath); // 新規ワークシートを作成する HSSFSheet sheet = wb.createSheet(); // 作成したシート名を変更 // 日本語シート名を指定する場合には // エンコーディング設定は必須 wb.setSheetName(0, "sheet1"); // 行オブジェクトの作成(行番号は0スタート) HSSFRow header = sheet.createRow(0); // セルオブジェクトの作成(セル番号は0スタート) // 引数はshort型でキャストしなければならない点に注意 HSSFCell cell1 = header.createCell(0); HSSFCell cell2 = header.createCell(1); HSSFCell cell3 = header.createCell(2); // セルに値を設定する cell1.setCellValue("テストクラス"); cell2.setCellValue("テストメソッド"); cell3.setCellValue("シート名"); for (int i = 1; i < fileInfoList.size(); i++) { EmulFileSheetInfo fileInfo = fileInfoList.get(i); HSSFRow dataRow = sheet.createRow(i); HSSFCell c_class = dataRow.createCell(0); HSSFCell c_method = dataRow.createCell(1); HSSFCell c_sheet = dataRow.createCell(2); c_class.setCellValue(fileInfo.getClassName()); c_method.setCellValue(fileInfo.getMethodName()); c_sheet.setCellValue(toCsv(fileInfo.getSheetNameList())); } // 作成したワークブックを保存する wb.write(out); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } finally { try { if (out != null) { out.close(); } } catch (IOException e) { // TODO 自動生成された catch ブロック e.printStackTrace(); } } }