
com.landawn.abacus.poi.ExcelUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of abacus-common Show documentation
Show all versions of abacus-common Show documentation
A general programming library in Java/Android. It's easy to learn and simple to use with concise and powerful APIs.
The newest version!
/*
* Copyright (c) 2015, Haiyang Li.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.landawn.abacus.poi;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Writer;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
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.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.landawn.abacus.exception.UncheckedException;
import com.landawn.abacus.type.Type;
import com.landawn.abacus.util.BufferedCSVWriter;
import com.landawn.abacus.util.CSVUtil;
import com.landawn.abacus.util.Charsets;
import com.landawn.abacus.util.DataSet;
import com.landawn.abacus.util.IOUtil;
import com.landawn.abacus.util.MutableInt;
import com.landawn.abacus.util.N;
import com.landawn.abacus.util.Objectory;
import com.landawn.abacus.util.RowDataSet;
import com.landawn.abacus.util.Strings;
import com.landawn.abacus.util.WD;
import com.landawn.abacus.util.function.TriConsumer;
import com.landawn.abacus.util.stream.Stream;
import lombok.Builder;
import lombok.Data;
public final class ExcelUtil {
public static final Function CELL_GETTER = cell -> switch (cell.getCellType()) {
case STRING -> cell.getStringCellValue();
case NUMERIC -> cell.getNumericCellValue();
case BOOLEAN -> cell.getBooleanCellValue();
case FORMULA -> cell.getCellFormula();
case BLANK -> "";
default -> throw new RuntimeException("Unsupported cell type: " + cell.getCellType());
};
public static final Function CELL2STRING = cell -> switch (cell.getCellType()) {
case STRING -> cell.getStringCellValue();
case NUMERIC -> String.valueOf(cell.getNumericCellValue());
case BOOLEAN -> String.valueOf(cell.getBooleanCellValue());
case FORMULA -> cell.getCellFormula();
case BLANK -> "";
default -> throw new RuntimeException("Unsupported cell type: " + cell.getCellType());
};
private ExcelUtil() {
// prevent instantiation
}
/**
* Reads the specified sheet from the given Excel file and converts each row in the specified sheet to a row in the returned DataSet by using the provided row extractor.
* The first row of the sheet is used as column names for the returned DataSet.
*
* @param excelFile the Excel file
* @return a list of strings representing the rows in the first sheet
*/
public static DataSet loadSheet(final File excelFile) {
return loadSheet(excelFile, 0, RowExtractors.DEFAULT);
}
/**
* Reads the specified sheet from the given Excel file and converts each row in the specified sheet to a row in the returned DataSet by using the provided row extractor.
* The first row of the sheet is used as column names for the returned DataSet.
*
* @param excelFile the Excel file to read
* @param sheetIndex the index of the sheet to read, starting from 0.
* @param rowExtractor converts each row in the specified sheet to a row in the returned DataSet.
* The first parameter is the column names, the second parameter is the row, and the third parameter is an array to store the extracted values.
* @return a list of objects of type T representing the rows in the specified sheet
* @throws UncheckedException if an I/O error occurs while reading the file
*/
public static DataSet loadSheet(final File excelFile, final int sheetIndex,
final TriConsumer super String[], ? super Row, ? super Object[]> rowExtractor) {
try (InputStream is = new FileInputStream(excelFile); //
Workbook workbook = new XSSFWorkbook(is)) {
return loadSheet(workbook.getSheetAt(sheetIndex), rowExtractor);
} catch (IOException e) {
throw new UncheckedException(e);
}
}
/**
* Reads the specified sheet from the given Excel file and converts each row in the specified sheet to a row in the returned DataSet by using the provided row extractor.
* The first row of the sheet is used as column names for the returned DataSet.
*
* @param excelFile the Excel file to read
* @param sheetName the name of the sheet to read
* @param rowExtractor converts each row in the specified sheet to a row in the returned DataSet.
* The first parameter is the column names, the second parameter is the row, and the third parameter is an array to store the extracted values.
* @return a list of objects of type T representing the rows in the specified sheet
* @throws UncheckedException if an I/O error occurs while reading the file
*/
public static DataSet loadSheet(final File excelFile, final String sheetName,
final TriConsumer super String[], ? super Row, ? super Object[]> rowExtractor) {
try (InputStream is = new FileInputStream(excelFile); //
Workbook workbook = new XSSFWorkbook(is)) {
Sheet sheet = workbook.getSheet(sheetName);
return loadSheet(sheet, rowExtractor);
} catch (IOException e) {
throw new UncheckedException(e);
}
}
private static DataSet loadSheet(final Sheet sheet, final TriConsumer super String[], ? super Row, ? super Object[]> rowExtractor) {
final Iterator rowIter = sheet.rowIterator();
if (!rowIter.hasNext()) {
return DataSet.empty();
}
final Row headerRow = rowIter.next();
final int columnCount = headerRow.getPhysicalNumberOfCells();
final String[] headers = new String[columnCount];
for (int i = 0; i < columnCount; i++) {
headers[i] = CELL2STRING.apply(headerRow.getCell(i));
}
final List> columnList = new ArrayList<>(columnCount);
for (int i = 0; i < columnCount; i++) {
columnList.add(new ArrayList<>());
}
final Object[] output = new Object[columnCount];
while (rowIter.hasNext()) {
rowExtractor.accept(headers, rowIter.next(), output);
for (int i = 0; i < columnCount; i++) {
columnList.get(i).add(output[i]);
}
}
final List columnNameList = new ArrayList<>(List.of(headers));
return new RowDataSet(columnNameList, columnList);
}
/**
* Read the first sheet of the Excel file and return the rows as a list of strings.
*
* @param excelFile the Excel file
* @return a list of strings representing the rows in the first sheet
*/
public static List> readSheet(final File excelFile) {
return readSheet(excelFile, 0, false, RowMappers.DEFAULT);
}
/**
* Reads the specified sheet from the given Excel file and maps each row to an object of type T using the provided row mapper.
*
* @param the type of the objects to be returned
* @param excelFile the Excel file to read
* @param sheetIndex the index of the sheet to read, starting from 0.
* @param skipFirstRow whether to skip the first row of the sheet
* @param rowMapper a function to map each row to an object of type T
* @return a list of objects of type T representing the rows in the specified sheet
* @throws UncheckedException if an I/O error occurs while reading the file
*/
public static List readSheet(final File excelFile, final int sheetIndex, final boolean skipFirstRow,
final Function super Row, ? extends T> rowMapper) {
try (InputStream is = new FileInputStream(excelFile); //
Workbook workbook = new XSSFWorkbook(is)) {
return readSheet(workbook.getSheetAt(sheetIndex), skipFirstRow, rowMapper);
} catch (IOException e) {
throw new UncheckedException(e);
}
}
/**
* Reads the specified sheet from the given Excel file and maps each row to an object of type T using the provided row mapper.
*
* @param the type of the objects to be returned
* @param excelFile the Excel file to read
* @param sheetName the name of the sheet to read
* @param skipFirstRow whether to skip the first row of the sheet
* @param rowMapper a function to map each row to an object of type T
* @return a list of objects of type T representing the rows in the specified sheet
* @throws UncheckedException if an I/O error occurs while reading the file
*/
public static List readSheet(final File excelFile, final String sheetName, final boolean skipFirstRow,
final Function super Row, ? extends T> rowMapper) {
try (InputStream is = new FileInputStream(excelFile); //
Workbook workbook = new XSSFWorkbook(is)) {
Sheet sheet = workbook.getSheet(sheetName);
return readSheet(sheet, skipFirstRow, rowMapper);
} catch (IOException e) {
throw new UncheckedException(e);
}
}
private static List readSheet(final Sheet sheet, final boolean skipFirstRow, final Function super Row, ? extends T> rowMapper) {
final List rowList = new ArrayList<>();
final Iterator rowIter = sheet.rowIterator();
if (skipFirstRow && rowIter.hasNext()) {
rowIter.next(); // skip the first row
}
while (rowIter.hasNext()) {
rowList.add(rowMapper.apply(rowIter.next()));
}
return rowList;
}
/**
* Returns a stream of rows from the specified sheet in the given Excel file.
*
* @param excelFile the Excel file to read
* @param sheetIndex the index of the sheet to read
* @param skipFirstRow whether to skip the first row of the sheet
* @return a stream of rows from the specified sheet
* @throws UncheckedException if an I/O error occurs while reading the file
*/
public static Stream streamSheet(final File excelFile, final int sheetIndex, final boolean skipFirstRow) {
try (InputStream is = new FileInputStream(excelFile); //
Workbook workbook = new XSSFWorkbook(is)) {
final Sheet sheet = workbook.getSheetAt(sheetIndex);
return Stream.of(sheet.rowIterator()).skip(skipFirstRow ? 1 : 0);
} catch (IOException e) {
throw new UncheckedException(e);
}
}
/**
* Returns a stream of rows from the specified sheet in the given Excel file.
*
* @param excelFile the Excel file to read
* @param sheetName the name of the sheet to read
* @param skipFirstRow whether to skip the first row of the sheet
* @return a stream of rows from the specified sheet
* @throws UncheckedException if an I/O error occurs while reading the file
*/
public static Stream streamSheet(final File excelFile, final String sheetName, final boolean skipFirstRow) {
try (InputStream is = new FileInputStream(excelFile); //
Workbook workbook = new XSSFWorkbook(is)) {
Sheet sheet = workbook.getSheet(sheetName);
return Stream.of(sheet.rowIterator()).skip(skipFirstRow ? 1 : 0);
} catch (IOException e) {
throw new UncheckedException(e);
}
}
/**
* Writes the specified data to an Excel file with the given sheet name and headers.
* @param sheetName the name of the sheet to create
* @param headers the headers for the columns
* @param rows the data to write to the sheet
* @param outputExcelFile the Excel file to write to
*/
public static void writeSheet(final String sheetName, final List | |
© 2015 - 2025 Weber Informatics LLC | Privacy Policy