
net.ymate.platform.commons.ExcelFileAnalysisHelper Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2007-2019 the original author or authors.
*
* 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 net.ymate.platform.commons;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import java.io.*;
import java.nio.file.Files;
import java.util.List;
import java.util.stream.IntStream;
/**
* Excel文件数据导入助手类
*
* @author 刘镇 ([email protected]) on 2018/5/25 上午6:37
*/
public class ExcelFileAnalysisHelper implements Closeable {
private final Workbook workbook;
private final String[] sheetNames;
public static ExcelFileAnalysisHelper bind(File file) throws IOException {
try (InputStream inputStream = Files.newInputStream(file.toPath())) {
return new ExcelFileAnalysisHelper(inputStream);
}
}
public static ExcelFileAnalysisHelper bind(InputStream inputStream) throws IOException {
return new ExcelFileAnalysisHelper(inputStream);
}
private ExcelFileAnalysisHelper(InputStream inputStream) throws IOException {
workbook = WorkbookFactory.create(inputStream);
sheetNames = new String[workbook.getNumberOfSheets()];
//
IntStream.range(0, sheetNames.length).forEachOrdered(idx -> sheetNames[idx] = workbook.getSheetName(idx));
}
/**
* @return 返回SHEET名称集合
*/
public String[] getSheetNames() {
return sheetNames;
}
public List openSheet(int sheetIdx, ISheetHandler handler) throws Exception {
return handler.handle(workbook.getSheetAt(sheetIdx));
}
public List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy