Skip to content

NIFI-14426: Add support for HSSF format in ExcelReader processor #9874

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,12 @@ public RecordSchema getSchema(Map<String, String> variables, InputStream content
final int firstRow = rawFirstRow == null ? NumberUtils.toInt(ExcelReader.STARTING_ROW.getDefaultValue()) : rawFirstRow;
final int zeroBasedFirstRow = ExcelReader.getZeroBasedIndex(firstRow);
final String password = context.getProperty(ExcelReader.PASSWORD).getValue();
final InputFileType inputFileType = context.getProperty(ExcelReader.INPUT_FILE_TYPE).asAllowableValue(InputFileType.class);
final ExcelRecordReaderConfiguration configuration = new ExcelRecordReaderConfiguration.Builder()
.withRequiredSheets(requiredSheets)
.withFirstRow(zeroBasedFirstRow)
.withPassword(password)
.withInputFileType(inputFileType)
.build();

final RowIterator rowIterator = new RowIterator(contentStream, configuration, logger);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@
import java.util.List;
import java.util.Map;

@Tags({"excel", "spreadsheet", "xlsx", "parse", "record", "row", "reader", "values", "cell"})
@Tags({"excel", "spreadsheet", "xls", "xlsx", "parse", "record", "row", "reader", "values", "cell"})
@CapabilityDescription("Parses a Microsoft Excel document returning each row in each sheet as a separate record. "
+ "This reader allows for inferring a schema from all the required sheets "
+ "or providing an explicit schema for interpreting the values."
+ "See Controller Service's Usage for further documentation. "
+ "This reader is currently only capable of processing .xlsx "
+ "(XSSF 2007 OOXML file format) Excel documents and not older .xls (HSSF '97(-2007) file format) documents.")
+ "This reader is capable of processing both password and non password protected .xlsx (XSSF 2007 OOXML file format) "
+ "and older .xls (HSSF '97(-2007) file format) Excel documents.")
public class ExcelReader extends SchemaRegistryService implements RecordReaderFactory {

public static final PropertyDescriptor REQUIRED_SHEETS = new PropertyDescriptor
Expand All @@ -85,6 +85,15 @@ public class ExcelReader extends SchemaRegistryService implements RecordReaderFa
.addValidator(StandardValidators.POSITIVE_INTEGER_VALIDATOR)
.build();

public static final PropertyDescriptor INPUT_FILE_TYPE = new PropertyDescriptor
.Builder().name("Input File Type")
.displayName("Input File Type")
.description("Specifies type of Excel input file.")
.required(true)
.allowableValues(InputFileType.class)
.defaultValue(InputFileType.XLSX)
.build();

public static final PropertyDescriptor PROTECTION_TYPE = new PropertyDescriptor
.Builder().name("Protection Type")
.displayName("Protection Type")
Expand Down Expand Up @@ -128,6 +137,7 @@ public RecordReader createRecordReader(final Map<String, String> variables, fina
final List<String> requiredSheets = getRequiredSheets(variables);
final int firstRow = getStartingRow(variables);
final String password = configurationContext.getProperty(PASSWORD).getValue();
final InputFileType inputFileType = configurationContext.getProperty(INPUT_FILE_TYPE).asAllowableValue(InputFileType.class);
final ExcelRecordReaderConfiguration configuration = new ExcelRecordReaderConfiguration.Builder()
.withDateFormat(dateFormat)
.withRequiredSheets(requiredSheets)
Expand All @@ -136,6 +146,7 @@ public RecordReader createRecordReader(final Map<String, String> variables, fina
.withTimeFormat(timeFormat)
.withTimestampFormat(timestampFormat)
.withPassword(password)
.withInputFileType(inputFileType)
.build();

return new ExcelRecordReader(configuration, in, logger);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public ExcelRecordReader(ExcelRecordReaderConfiguration configuration, InputStre
try {
this.rowIterator = new RowIterator(inputStream, configuration, logger);
} catch (RuntimeException e) {
throw new MalformedRecordException("Read initial Record from Excel XLSX failed", e);
throw new MalformedRecordException("Read initial Record from Excel file failed", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class ExcelRecordReaderConfiguration {
private String timestampFormat;
private String password;
private boolean avoidTempFiles;
private InputFileType inputFileType;

private ExcelRecordReaderConfiguration() {
}
Expand Down Expand Up @@ -66,6 +67,10 @@ public boolean isAvoidTempFiles() {
return avoidTempFiles;
}

public InputFileType getInputFileType() {
return inputFileType;
}

public static final class Builder {
private RecordSchema schema;
private List<String> requiredSheets;
Expand All @@ -75,6 +80,7 @@ public static final class Builder {
private String timestampFormat;
private String password;
private boolean avoidTempFiles;
private InputFileType inputFileType;

public Builder withSchema(RecordSchema schema) {
this.schema = schema;
Expand Down Expand Up @@ -116,6 +122,11 @@ public Builder withAvoidTempFiles(boolean avoidTempFiles) {
return this;
}

public Builder withInputFileType(InputFileType inputFileType) {
this.inputFileType = inputFileType;
return this;
}

public ExcelRecordReaderConfiguration build() {
ExcelRecordReaderConfiguration excelRecordReaderConfiguration = new ExcelRecordReaderConfiguration();
excelRecordReaderConfiguration.schema = this.schema;
Expand All @@ -126,6 +137,7 @@ public ExcelRecordReaderConfiguration build() {
excelRecordReaderConfiguration.firstRow = this.firstRow;
excelRecordReaderConfiguration.password = password;
excelRecordReaderConfiguration.avoidTempFiles = avoidTempFiles;
excelRecordReaderConfiguration.inputFileType = inputFileType;

return excelRecordReaderConfiguration;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ public ExcelRecordSource(final InputStream in, final PropertyContext context, fi
final int firstRow = rawFirstRow == null ? NumberUtils.toInt(ExcelReader.STARTING_ROW.getDefaultValue()) : rawFirstRow;
final int zeroBasedFirstRow = ExcelReader.getZeroBasedIndex(firstRow);
final String password = context.getProperty(ExcelReader.PASSWORD).getValue();
final InputFileType inputFileType = context.getProperty(ExcelReader.INPUT_FILE_TYPE).asAllowableValue(InputFileType.class);
final ExcelRecordReaderConfiguration configuration = new ExcelRecordReaderConfiguration.Builder()
.withRequiredSheets(requiredSheets)
.withFirstRow(zeroBasedFirstRow)
.withPassword(password)
.withInputFileType(inputFileType)
.build();
this.rowIterator = new RowIterator(in, configuration, logger);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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
*
* http://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 org.apache.nifi.excel;

import org.apache.nifi.components.DescribedValue;

public enum InputFileType implements DescribedValue {
XLS("XLS 1997-2007 file format"),
XLSX("XLSX 2007-present OOXML file format");

InputFileType(String description) {
this.description = description;
}

private final String description;

@Override
public String getValue() {
return name();
}

@Override
public String getDisplayName() {
return name();
}

@Override
public String getDescription() {
return description;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import com.github.pjfanning.xlsx.StreamingReader;
import org.apache.nifi.logging.ComponentLog;
import org.apache.nifi.processor.exception.ProcessException;
import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
Expand All @@ -42,13 +44,26 @@ class RowIterator implements Iterator<Row>, Closeable {
private Row currentRow;

RowIterator(final InputStream in, final ExcelRecordReaderConfiguration configuration, final ComponentLog logger) {
this.workbook = StreamingReader.builder()
.rowCacheSize(100)
.bufferSize(4096)
.password(configuration.getPassword())
.setAvoidTempFiles(configuration.isAvoidTempFiles())
.setReadSharedFormulas(true) // NOTE: If not set to true, then data with shared formulas fail.
.open(in);
if (configuration.getInputFileType() == InputFileType.XLSX) {
this.workbook = StreamingReader.builder()
.rowCacheSize(100)
.bufferSize(4096)
.password(configuration.getPassword())
.setAvoidTempFiles(configuration.isAvoidTempFiles())
.setReadSharedFormulas(true) // NOTE: If not set to true, then data with shared formulas fail.
.open(in);
} else {
// Providing the password to the HSSFWorkbook is done by setting a thread variable managed by
// Biff8EncryptionKey. After the workbook is created, the thread variable can be cleared.
Biff8EncryptionKey.setCurrentUserPassword(configuration.getPassword());
try {
this.workbook = new HSSFWorkbook(in);
} catch (final IOException e) {
throw new ProcessException("Failed to open XLS file", e);
} finally {
Biff8EncryptionKey.setCurrentUserPassword(null);
}
}

final List<String> requiredSheets = configuration.getRequiredSheets();
if (requiredSheets == null || requiredSheets.isEmpty()) {
Expand Down
Loading