本文整理汇总了Java中org.apache.poi.ss.usermodel.WorkbookFactory.create方法的典型用法代码示例。如果您正苦于以下问题:Java WorkbookFactory.create方法的具体用法?Java WorkbookFactory.create怎么用?Java WorkbookFactory.create使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.poi.ss.usermodel.WorkbookFactory
的用法示例。
在下文中一共展示了WorkbookFactory.create方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: readExcel
import org.apache.poi.ss.usermodel.WorkbookFactory; //导入方法依赖的package包/类
public static List<String[]> readExcel(InputStream is, int sheetIndex) throws Exception {
Workbook workbook = WorkbookFactory.create(is);
Sheet sheet = workbook.getSheetAt(sheetIndex);
List<String[]> data = new ArrayList<>();
for (int i = sheet.getFirstRowNum(); i <= sheet.getLastRowNum(); i++) {
Row row = sheet.getRow(i);
if (row == null) continue;
int last = row.getLastCellNum();
String[] rowData = new String[last];
for (int j = 0; j < last; j++) {
Cell cell = row.getCell(j);
rowData[j] = cell == null ? null : getCellString(cell);
}
data.add(rowData);
}
return data;
}
示例2: loadSheetForTest
import org.apache.poi.ss.usermodel.WorkbookFactory; //导入方法依赖的package包/类
/**
* テスト用のシートの取得
* <p>修正したりするためのシート
*/
private List<Sheet> loadSheetForTest(final File file) throws InvalidFormatException, IOException {
List<Sheet> list = new ArrayList<>();
try(InputStream in = new FileInputStream(file)) {
Workbook workbook = WorkbookFactory.create(in);
final int sheetNum = workbook.getNumberOfSheets();
for(int i=0; i < sheetNum; i++) {
final Sheet sheet = workbook.getSheetAt(i);
final String sheetName = sheet.getSheetName();
if(!sheetName.startsWith("テスト")) {
continue;
}
list.add(sheet);
}
}
return list;
}
示例3: fillTrie
import org.apache.poi.ss.usermodel.WorkbookFactory; //导入方法依赖的package包/类
@Override
protected void fillTrie(Logger logger, Trie<List<String>> trie, Corpus corpus) throws IOException, ModuleException {
Iterator<InputStream> inputStreams = xlsFile.getInputStreams();
while (inputStreams.hasNext()) {
try (InputStream is = inputStreams.next()) {
Workbook wb = WorkbookFactory.create(is);
for (int sheetNumber : sheets) {
Sheet sheet = wb.getSheetAt(sheetNumber);
fillSheetEntries(trie, sheet);
}
}
catch (EncryptedDocumentException|InvalidFormatException e) {
rethrow(e);
}
}
}
示例4: readWorkBook
import org.apache.poi.ss.usermodel.WorkbookFactory; //导入方法依赖的package包/类
public static void readWorkBook() throws Exception {
// poi��ȡexcel
// ����Ҫ������ļ���������
final InputStream inp = new FileInputStream("d:\\workbooks.xlsx");
// �������������������� ��������������
final Workbook wb = WorkbookFactory.create(inp);
for (final Sheet sheet : wb) {
System.out.println(sheet.getSheetName());
for (final Row row : sheet) {
for (final Cell cell : row) {
System.out.print(cell.toString() + " ");
}
System.out.println();
}
}
// �ر�������
inp.close();
}
示例5: importCombination
import org.apache.poi.ss.usermodel.WorkbookFactory; //导入方法依赖的package包/类
public static CombinationSolver<String, String> importCombination(File file) throws Exception {
if (file.getName().endsWith(".csv")) {
try (Reader reader = new FileReader(file);
TableCSVReader csvReader = new TableCSVReader(reader)) {
csvReader.setUseHeader(true);
return importCombination(csvReader);
}
} else if (file.getName().endsWith(".xls") || file.getName().endsWith(".xlsx")) {
Workbook workbook = WorkbookFactory.create(file);
Sheet sheet = workbook.getSheetAt(0);
try (ExcelSheetReader sheetReader = new ExcelSheetReader(sheet)) {
sheetReader.setUseHeader(true);
return importCombination(sheetReader);
}
} else {
throw new IllegalArgumentException("Unsupported file");
}
}
示例6: getWorkbook
import org.apache.poi.ss.usermodel.WorkbookFactory; //导入方法依赖的package包/类
/**
* Get Workbook
*
* @param completeFilePath
* @param fileExtension
* @return workbook
* @throws ParserException
*/
private Workbook getWorkbook(File completeFilePath, String fileExtension)
throws ParserException {
/* Open & Read File */
try (FileInputStream inputStream = new FileInputStream(completeFilePath)) {
/* Get Type Based On File Extension */
return fileExtension.equals(Constants.EXT_XLSX) ? (XSSFWorkbook) WorkbookFactory
.create(inputStream) : fileExtension
.equals(Constants.EXT_XLS) ? (HSSFWorkbook) WorkbookFactory
.create(inputStream) : null;
} catch (Exception ex) {
/* Catch, Log & Throw Exception */
LOGGER.error("Error Occured While Parsing File!", ex);
throw new ParserException(exceptionFactory.create(
ExceptionType.PARSER, "PAR003",
completeFilePath.getAbsolutePath()), ex);
}
}
示例7: needsNewTracker
import org.apache.poi.ss.usermodel.WorkbookFactory; //导入方法依赖的package包/类
public static boolean needsNewTracker() throws Exception {
final String FILE = workbook.getAbsolutePath();
InputStream inp = new FileInputStream(FILE);
Workbook wb = WorkbookFactory.create(inp);
Sheet sheet = wb.getSheetAt(0);
Row row = sheet.getRow(1);
Cell cell = row.getCell(1);
Date firstCellContents = cell.getDateCellValue();
Calendar firstDayCalendar = Calendar.getInstance();
firstDayCalendar.setTime(firstCellContents);
int diff = daysBetweenFirstPST(firstDayCalendar);
if (diff < 14) {
return false;
} else {
return true;
}
}
示例8: scan
import org.apache.poi.ss.usermodel.WorkbookFactory; //导入方法依赖的package包/类
@Override
public void scan(File file, String filename, SheetSelection selection, IExcelImportFileVisitor<Workbook, Sheet, Row, Cell, CellReference> visitor) throws TableImportException {
Validate.notNull(file, "file must not be null");
Validate.notNull(visitor, "visitor must not be null");
ApachePoiImportNavigator navigator = new ApachePoiImportNavigator(filename);
try (InputStream stream = new TFileInputStream(file)) {
Workbook workbook = WorkbookFactory.create(stream);
for (int index = 0 ; index < workbook.getNumberOfSheets() ; ++index) {
Sheet sheet = workbook.getSheetAt(index);
if (navigator.tableHasContent(sheet) && SELECTIONS_PREDICATES.get(selection).apply(sheet)) {
visitor.visitSheet(navigator, workbook, sheet);
}
}
} catch (InvalidFormatException | IOException | IllegalArgumentException e) {
throw new TableImportFileException(e, navigator.getLocation(null, null, null));
}
}
示例9: loadSheetByName
import org.apache.poi.ss.usermodel.WorkbookFactory; //导入方法依赖的package包/类
/**
* シート名を指定して取得する
* @param file
* @param name
*/
private Sheet loadSheetByName(final File file, final String name) throws InvalidFormatException, IOException {
try(InputStream in = new FileInputStream(file)) {
Workbook workbook = WorkbookFactory.create(in);
final int sheetNum = workbook.getNumberOfSheets();
for(int i=0; i < sheetNum; i++) {
final Sheet sheet = workbook.getSheetAt(i);
final String sheetName = sheet.getSheetName();
if(sheetName.equals(name)) {
return sheet;
}
}
}
throw new IllegalStateException("not found sheet : " + name);
}
示例10: saveTest_HeadersOnly
import org.apache.poi.ss.usermodel.WorkbookFactory; //导入方法依赖的package包/类
@Test
public void saveTest_HeadersOnly() throws InvalidFormatException, IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
// save it
Ssio.save(ITRecord.getHeaderMap(), null, outputStream);
byte[] spreadsheet = outputStream.toByteArray();
// do a save for human eye check
FileUtils.writeByteArrayToFile(createFile("saveTest_HeadersOnly"), spreadsheet);
// then parse it
Workbook workbook = WorkbookFactory.create(new ByteArrayInputStream(spreadsheet));
/*** do assertions ***/
Sheet sheet = workbook.getSheetAt(0);
Row headerRow = sheet.getRow(0);
// size
Assert.assertEquals(0, sheet.getLastRowNum());
Assert.assertEquals(ITRecord.getHeaderMap().size(), headerRow.getLastCellNum());
}
示例11: testDownloadXlsWithHeader
import org.apache.poi.ss.usermodel.WorkbookFactory; //导入方法依赖的package包/类
@Test
public void testDownloadXlsWithHeader() throws InvalidFormatException, IOException {
// 準備
MockHttpServletResponse response = new MockHttpServletResponse();
// 実行
tableDownloadOperation.downloadXls(response, "test_{0}.xls", new LocalDateTime(2015, 1, 23, 12, 34, 56),
asList("HEAD0"), createCommonClause(), createOrderByClause(), constant("TEST00"));
// 検証
assertEquals("application/vnd.ms-excel", response.getContentType());
assertEquals("application/vnd.ms-excel", response.getHeader("Content-Type"));
assertEquals("attachment; filename=\"test_20150123123456.xls\"", response.getHeader("Content-Disposition"));
try (InputStream in = new ByteArrayInputStream(response.getContentAsByteArray());
Workbook workbook = WorkbookFactory.create(in);
ExcelReader reader = new ExcelReader(workbook)) {
String[] record;
record = reader.read();
assertEquals(1, record.length);
assertEquals("HEAD0", record[0]);
record = reader.read();
assertEquals(1, record.length);
assertEquals("TEST00", record[0]);
assertNull(reader.read());
}
}
示例12: testDoConvert2
import org.apache.poi.ss.usermodel.WorkbookFactory; //导入方法依赖的package包/类
@Test
public void testDoConvert2() throws Exception {
Path temporaryOutput = Files.createTempFile("excel2csv", ".xlsx");
Converter.builder().copyAllSheets(true).build().doConvert(Arrays.asList(
new File(temporaryDirectory.toFile(), "multisheet.xls"),
new File(temporaryDirectory.toFile(), "multisheet.xlsx")
), temporaryOutput.toFile());
Workbook workbook = WorkbookFactory.create(temporaryOutput.toFile());
for (String one : new String[]{"iris", "CO2", "DNase"}) {
for (String suffix : new String[]{"", "-1"}) {
try (TableReader reader = new ExcelSheetReader(workbook.getSheet(one+suffix))) {
Excel2CSVTest.assertObjects(reference.get(one), reader.readAll());
}
}
}
}
示例13: open
import org.apache.poi.ss.usermodel.WorkbookFactory; //导入方法依赖的package包/类
private Workbook open(File f) throws IOException
{
try
{
return WorkbookFactory.create(f);
}
catch (InvalidFormatException e)
{
throw new IOException("File broken", e);
}
}
示例14: run
import org.apache.poi.ss.usermodel.WorkbookFactory; //导入方法依赖的package包/类
@Override
public void run() {
synchronized(lock) {
try {
wb = WorkbookFactory.create(file);
} catch (Exception ex2) {
this.ex = ex2;
}
}
}
示例15: openWorkbook
import org.apache.poi.ss.usermodel.WorkbookFactory; //导入方法依赖的package包/类
/**
* Open an Excel workbook ready for conversion.
*
* @param file An instance of the File class that encapsulates a handle
* to a valid Excel workbook. Note that the workbook can be in
* either binary (.xls) or SpreadsheetML (.xlsx) format.
* @throws java.io.FileNotFoundException Thrown if the file cannot be located.
* @throws java.io.IOException Thrown if a problem occurs in the file system.
* @throws org.apache.poi.openxml4j.exceptions.InvalidFormatException Thrown
* if invalid xml is found whilst parsing an input SpreadsheetML
* file.
*/
private void openWorkbook(File file) throws FileNotFoundException,
IOException, InvalidFormatException {
FileInputStream fis = null;
try {
System.out.println("Opening workbook [" + file.getName() + "]");
fis = new FileInputStream(file);
// Open the workbook and then create the FormulaEvaluator and
// DataFormatter instances that will be needed to, respectively,
// force evaluation of formulae found in cells and create a
// formatted String encapsulating the cells contents.
this.workbook = WorkbookFactory.create(fis);
this.evaluator = this.workbook.getCreationHelper().createFormulaEvaluator();
this.formatter = new DataFormatter(true);
}
finally {
if(fis != null) {
fis.close();
}
}
}