
com.github.romualdrousseau.any2json.loader.excel.xlsx.XlsxDocument Maven / Gradle / Ivy
package com.github.romualdrousseau.any2json.loader.excel.xlsx;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.List;
import com.github.romualdrousseau.any2json.Document;
import com.github.romualdrousseau.any2json.Sheet;
import com.github.romualdrousseau.any2json.base.BaseDocument;
import com.github.romualdrousseau.any2json.base.BaseSheet;
import com.github.romualdrousseau.shuju.strings.StringUtils;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackageAccess;
import org.apache.poi.poifs.crypt.Decryptor;
import org.apache.poi.poifs.crypt.EncryptionInfo;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.xssf.eventusermodel.XSSFReader;
import org.apache.poi.xssf.eventusermodel.XSSFReader.SheetIterator;
public class XlsxDocument extends BaseDocument {
private static List EXTENSIONS = List.of(".xls", ".xlsx", ".xlsm");
private final ArrayList sheets = new ArrayList();
private OPCPackage opcPackage;
@Override
public boolean open(final File excelFile, final String encoding, final String password) {
if (excelFile == null) {
throw new IllegalArgumentException();
}
this.sheets.clear();
if (EXTENSIONS.stream().filter(x -> excelFile.getName().toLowerCase().endsWith(x)).findAny().isEmpty()) {
return false;
}
try {
if (!StringUtils.isBlank(password)) {
final var poifs = new POIFSFileSystem(excelFile);
final var info = new EncryptionInfo(poifs);
final var decrypt = Decryptor.getInstance(info);
decrypt.verifyPassword(password);
this.opcPackage = OPCPackage.open(decrypt.getDataStream(poifs));
} else {
this.opcPackage = OPCPackage.open(excelFile.getAbsolutePath(), PackageAccess.READ);
}
final var reader = new XSSFReader(this.opcPackage);
final var sharedStrings = reader.getSharedStringsTable();
final var styles = reader.getStylesTable();
final var it = (SheetIterator) reader.getSheetsData();
while (it.hasNext()) {
final InputStream sheetData = it.next();
this.sheets.add(new XlsxSheet(it.getSheetName(), sheetData, sharedStrings, styles));
}
return this.sheets.size() > 0;
} catch (IllegalArgumentException | IOException | OpenXML4JException | GeneralSecurityException e) {
this.close();
return false;
}
}
@Override
public void close() {
this.sheets.forEach(XlsxSheet::close);
this.sheets.clear();
if (this.opcPackage != null) {
this.opcPackage.revert();
this.opcPackage = null;
}
super.close();
}
@Override
public int getNumberOfSheets() {
return this.sheets.size();
}
@Override
public String getSheetNameAt(final int i) {
return this.sheets.get(i).getName();
}
@Override
public Sheet getSheetAt(final int i) {
return new BaseSheet(this, this.sheets.get(i).getName(), this.sheets.get(i).ensureDataLoaded());
}
@Override
public void updateParsersAndClassifiers() {
if (this.getHints().contains(Document.Hint.INTELLI_TAG)) {
this.getHints().add(Document.Hint.INTELLI_LAYOUT);
}
super.updateParsersAndClassifiers();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy