0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

JavaでExcelから検索

Last updated at Posted at 2023-09-13
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>mygroup</groupId>
	<artifactId>excelgrep</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<properties>
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi</artifactId>
			<version>5.2.3</version>
		</dependency>
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-ooxml</artifactId>
			<version>5.2.3</version>
		</dependency>
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-core</artifactId>
			<version>2.20.0</version>
		</dependency>
	</dependencies>
</project>
Main.java
package excelgrep;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class Main {
	public static void main(String[] args) throws Exception {
		// 検索開始フォルダ
		String[] dirPaths = { "C:\\dir1", "C:\\dir2" };
		// 検索語
		String[] words = { "検索語1", "\"" };
		// 除外サブフォルダ名
		String[] excludeSubDirNames = { "除外サブフォルダ1", "除外サブフォルダ2" };
		// 除外シート名
		String[] excludeSheetNames = { "除外シート1", "除外シート2" };

		System.out.println("開始します。");
		System.out.println("ファイル\tシート\tセル\t内容");
		List<String> wordsList = new ArrayList<String>(Arrays.asList(words));
		List<String> excludeSubDirNamesList = new ArrayList<String>(Arrays.asList(excludeSubDirNames));
		List<String> excludeSheetNamesList = new ArrayList<String>(Arrays.asList(excludeSheetNames));
		for (String dirPath : dirPaths) {
			func(new File(dirPath), wordsList, excludeSubDirNamesList, excludeSheetNamesList);
		}
		System.out.println("終了します。");
	}

	private static void func(File dir, List<String> wordsList, List<String> excludeSubDirNamesList, List<String> excludeSheetNamesList) throws Exception {
		File[] fileOrSubDirs = dir.listFiles();
		if (fileOrSubDirs != null) {
			for (File fileOrSubDir : fileOrSubDirs) {
				// 除外サブフォルダでなければ再帰的に処理
				if (fileOrSubDir.isDirectory() && !excludeSubDirNamesList.contains(fileOrSubDir.getName())) {
					func(fileOrSubDir, wordsList, excludeSubDirNamesList, excludeSheetNamesList);
				}
				// 拡張子が.xlsxなファイルから検索
				if (fileOrSubDir.isFile() && fileOrSubDir.getName().endsWith(".xlsx")) {
					try (XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(fileOrSubDir))) {
						// 除外シート以外のシートから検索
						workbook.forEach(sheet -> {
							if (!excludeSheetNamesList.contains(sheet.getSheetName())) {
								sheet.forEach(row -> row.forEach(cell -> {
									if (cell.getCellType() == CellType.STRING) {
										String cellValue = cell.getStringCellValue();
										wordsList.forEach(word -> {
											if (cellValue.contains(word)) {
												// 検索結果はExcelに貼り付けやすい書式で出力
												System.out.printf("%s\t%s\t%s\t\"%s\"%n", fileOrSubDir.getAbsoluteFile(), sheet.getSheetName(), cell.getAddress().toString(),
														cellValue.replaceAll("\"", "\"\""));
											}
										});
									}
									// TODO STRING以外や数式やオブジェクト内からも検索
								}));
							}
						});
					} catch (Exception e) {
						System.err.println(e.toString());
					}
				}
			}
		}
	}
}

https://www.zunouissiki.com/create-excel-grep-tool-by-apache-poi/
他参考にした、多謝

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?