简体   繁体   中英

Apache POI read txt file as an excel file

Couldn't find many examples of this exact use case. Basically I have two directories, that should contain the same files, a mix of .xlsx files and .txt files. I have a bunch of code written that compares the .xlsx files fine and can also return the coordinates of any mismatches it finds (row, column).

My code seems to have a problem doing the same with a text file, I can open this fine manually via excel, it's a tab delimited text file.

It doesn't like when I try to convert it to an XSSFWorkbook, where File excelFile1 is my text file:

FileInputStream controlFile = new FileInputStream(this.excelFile1);
XSSFWorkbook controlWorkBook = new XSSFWorkbook(controlFile);

The exception I get is:

org.apache.poi.openxml4j.exceptions.NotOfficeXmlFileException: No valid entries or contents found, this is not a valid OOXML (Office Open XML) file

I imagine this is because it is a .txt file and not a .xlsx file? Is there any way around this without writing separate logic for text files? Is there a way to treat the .txt file the same way I would a .xlsx file?

Thanks!

A CSV text file is not an Excel file and cannot be manipulated with POI.

You will have to write new code to use a CSV reader (such as OpenCSV or Apache Commons CSV) for those cases, and implement a model of the file in memory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM