Activate Excel file A1 cell of each sheet in Java

■ Cause

In various development sites, it is common that the A1 cell of the sheet is used when the Excel file is first opened when creating an artifact. It takes time to do it manually, and I think that the more files there are, the more omissions will be made to cell A1. I think it would be convenient to use Apache's POI to set all files under the specified folder or in cell A1 of each sheet of the specified file, so I made a simple program. (Can be set to any cell by modifying A1CELL_ROW and A1CELL_COL)

■ Environment  OS:Windows JRE version: 1.8 POI version: 4.1.2

■ Source code package xxx.xxxxx.xxxx; // Change to your liking

import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.file.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream;

import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory;

public class A1CellJump {

//エクセルファイルを置いているフォルダー private static String INPUT_DIR = ".\"; private final int A1CELL_ROW = 0; private final int A1CELL_COL = 0; private final String EXCEL = ".xlsx";

public static void main(String[] args) throws IOException {

	A1CellJump tt = new A1CellJump();
	if (args.length > 0) {
		INPUT_DIR = args[0];
	}

	List<String> fileList = tt.getFileList(INPUT_DIR);
	for (int i = 0; i < fileList.size(); i++) {

		tt.jumpA1Cell(fileList.get(i));
	}

}

/**

// Use WorkbookFactory when editing an existing excel file wb = WorkbookFactory.create(in);

	} catch (Exception e) {
		e.printStackTrace();
	}

// Get all sheet names. List list = getSheetList(wb);

// Move to cell A1 for each sheet for (int i = 0; i < list.size(); i++) { String sheetName = list.get(i); System.out.print ("\ t sheet:" + sheetName);

// Activate cell A1 of the specified sheet. activeA1Cell(wb.getSheet(sheetName)); }

	FileOutputStream out = null;

	try {

// Specify the Excel file to change out = new FileOutputStream(fileName);

// write wb.write(out); } catch (Exception e) { e.printStackTrace(); } finally { try {out.close();} catch (Exception e) {} try {wb.close();} catch (Exception e) {}

	}

}

/**

} ■ Results Case 1: There is a value in cell A1 (Sheet1 in BBB.xlsx) Case 2: No value in cell A1 (Sheet2 in BBB.xlsx) Case 3: A1 cell is a merged cell, no value (Sheet3 in BBB.xlsx) Case 4: A1 cell is a merged cell and has a value (Sheet4 in BBB.xlsx)

-------- Output message to console ----------------- ....\AAA.xlsx ....\BBB.xlsx Configuration file: .... \ AAA.xlsx Sheet: Sheet1 OK Configuration file: .... \ BBB.xlsx Sheet: Sheet1 OK Sheet: Sheet2 OK Sheet: Sheet3 OK Sheet: Sheet4 OK

Recommended Posts

Activate Excel file A1 cell of each sheet in Java
Get the public URL of a private Flickr file in Java
[Java] Returns a Japanese name file in filename of HTTP header
Java creates a pie chart in Excel
The story of forgetting to close a file in Java and failing
Sample program that returns the hash value of a file in Java
Read a string in a PDF file with Java
Measure the size of a folder in Java
A bat file that uses Java in windows
A quick review of Java learned in class
A quick review of Java learned in class part4
[Java] Get the file in the jar regardless of the environment
A quick review of Java learned in class part3
How to convert a file to a byte array in Java
A quick review of Java learned in class part2
Java11: Run Java code in a single file as is
Summary of file reading method for each Java file format
[Swift 5] Recognize ON/OFF of Switch in custom cell for each cell
A quick explanation of the five types of static in Java
Save Java PDF in Excel
Java merge & unmerge Excel cell
Importing Excel data in Java 2
[Java] Create a temporary file
Find a subset in Java
Import Excel data in Java
Implementation of gzip in java
Importing Excel data in Java 3
I tried to make a client of RESAS-API in Java
Implementation of tri-tree in Java
Let's create a TODO application in Java 4 Implementation of posting function
Let's create a TODO application in Java 6 Implementation of search function
Output the difference between each field of two objects in Java
Handle business logic for a set of Entity in Java class
Let's create a TODO application in Java 8 Implementation of editing function
Cast an array of Strings to a List of Integers in Java
Let's create a TODO application in Java 1 Brief explanation of MVC
Read items containing commas in a CSV file without splitting (Java)
Let's create a TODO application in Java 5 Switch the display of TODO
How to get the length of an audio file in java
Think of a Java update strategy
3 Implement a simple interpreter in Java
Read Java properties file in C #
I created a PDF in Java.
Upload a file using Java HttpURLConnection
Sort a List of Java objects
Run a batch file from Java
Unzip the zip file in Java
Log output to file in Java
A simple sample callback in Java
List of members added in Java 9
EXCEL file update sample with JAVA
A brief description of JAVA dependencies
About file copy processing in Java
Get stuck in a Java primer
List of types added in Java 9
Implementation of like function in Java
Dynamically increase the number of elements in a Java 2D array (multidimensional array)
To create a Zip file while grouping database search results in Java
Memo: [Java] If a file is in the monitored directory, process it.
How to find out the Java version of a compiled class file
How to get the absolute path of a directory running in Java