添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Here I want to extract the data from .xlsx file and for that I already add the poi jar and created the reference of fileInputStream

package demo;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class DemoExcel {
    public static void main(String[] args) throws Exception{
        File excel = new File("C:\\Users\\Devaditya\\Documents\\Book1.xlsx");
        FileInputStream fis = null;
        fis = new FileInputStream(excel);
        System.out.println(fis.toString());
        HSSFWorkbook wb = new HSSFWorkbook(fis);
        System.out.println(wb.toString());
        HSSFSheet sh = wb.getSheet("Data");
        System.out.println(sh.toString());

Here i am getting the error:-

Exception in thread "main" java.io.IOException: Invalid header signature; read 0, expected -2226271756974174256
    at org.apache.poi.poifs.storage.HeaderBlockReader.<init>(HeaderBlockReader.java:88)
    at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:83)
    at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:210)
    at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:191)
    at demo.DemoExcel.main(DemoExcel.java:23)

This is the new XML Excel format, known by the extension .xlsx.

So, you are using the wrong class.

In fact it would be better not to use a specific class at all, let POI work out what you have. Use a WorkbookFactory:

final Workbook workbook = WorkbookFactory.create(excel);

This is:

  • programming to the interface.
  • robust to changes in the type of workbook read, as long a POI supports it
  • faster and more efficient. POI can read the File piecemeal, when it needs to rather than having to slurp the whole workbook into memory.
  • Doesn't have the memory leak that you have when you don't close() the FileInputStream.
  • Just in case anyone did as me, I'll add that WorkbookFactory is not in poi library, but in poi-ooxml. I spent an hour figuring that one out. – jumps4fun Jan 16, 2017 at 14:44

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.