Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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
*
* 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 tech.tablesaw.io.xlsx;
import static org.apache.poi.ss.usermodel.CellType.FORMULA;
import static org.apache.poi.ss.usermodel.CellType.NUMERIC;
import static org.apache.poi.ss.usermodel.CellType.STRING;
import com.google.common.collect.Iterables;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import javax.annotation.concurrent.Immutable;
import org.apache.poi.ss.format.CellDateFormatter;
import org.apache.poi.ss.format.CellGeneralFormatter;
import org.apache.poi.ss.format.CellNumberFormatter;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import tech.tablesaw.api.ColumnType;
import tech.tablesaw.api.DoubleColumn;
import tech.tablesaw.api.LongColumn;
import tech.tablesaw.api.Table;
import tech.tablesaw.columns.Column;
import tech.tablesaw.io.DataReader;
import tech.tablesaw.io.ReaderRegistry;
import tech.tablesaw.io.Source;
@Immutable
public class XlsxReader implements DataReader {
private static final XlsxReader INSTANCE = new XlsxReader();
static {
register(Table.defaultReaderRegistry);
}
public static void register(ReaderRegistry registry) {
registry.registerExtension("xlsx", INSTANCE);
registry.registerMimeType(
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", INSTANCE);
registry.registerOptions(XlsxReadOptions.class, INSTANCE);
}
@Override
public Table read(XlsxReadOptions options) throws IOException {
List
tables = readMultiple(options, true);
if (options.sheetIndex() != null) {
int index = options.sheetIndex();
if (index < 0 || index >= tables.size()) {
throw new IndexOutOfBoundsException(
String.format("Sheet index %d outside bounds. %d sheets found.", index, tables.size()));
}
Table table = tables.get(index);
if (table == null) {
throw new IllegalArgumentException(
String.format("No table found at sheet index %d.", index));
}
return table;
}
// since no specific sheetIndex asked, return first table
return tables.stream()
.filter(t -> t != null)
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("No tables found."));
}
public List
readMultiple(XlsxReadOptions options) throws IOException {
return readMultiple(options, false);
}
/**
* Read at most a table from every sheet.
*
* @param includeNulls include nulls for sheets without a table
* @return a list of tables, at most one for every sheet
*/
protected List