WorkbookFactory.create(的inputStream)

时间:2016-04-22 05:19:47

标签: java excel

以下是我用xlsxxls格式

阅读文件的代码
File customerTemplateFileObj = new File(customerTemplateFullPath);
InputStream inputStream = new FileInputStream(customerTemplateFileObj);

Workbook myWorkBook = null;
try {

    ***myWorkBook = WorkbookFactory.create(inputStream);***
} 
catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

int totalSheets = myWorkBook.getNumberOfSheets();

我的代码适用于xls格式,但xlsx停止在

myWorkBook = WorkbookFactory.create(inputStream);

没有任何异常。

1 个答案:

答案 0 :(得分:1)

要在Java中读取xlsx文件,请使用poi jar中的XSSFWorkbook类

package com.ssaurel.samples.excel;

import java.io.File; 

import java.io.FileInputStream;

import java.io.IOException;

import java.util.Iterator;


import org.apache.poi.ss.usermodel.Cell;

import org.apache.poi.ss.usermodel.Row;

import org.apache.poi.xssf.usermodel.XSSFSheet;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;


public class ReadExcel {

  public static void main(String[] args) throws IOException {
    File excelFile = new File("contacts.xlsx");
    FileInputStream fis = new FileInputStream(excelFile);

    // we create an XSSF Workbook object for our XLSX Excel File
    XSSFWorkbook workbook = new XSSFWorkbook(fis);

    // ...

  }

}