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

I am creating an UI where I ask the user to upload an existing excel file and I am getting this error. I have done some searching, and tried to use a POIFileSystem object before passing in the FileInputStream but that didn't. I am getting this error of the line that creates the workbook.

This my code:

public static Cell readExcelFile(byte[] byteFile){
    if (file == null){
        System.out.println("file is empty");
    } else {
        InputStream input = new ByteArrayInputStream(byteFile);
        POIFileSytem fsPOI = new POIFileSytem(input);
        HSSFWorkbook wb = new HSSFWorkbook(fsPOI);
      //continues to read the file
                If you open the file in Excel, and do a Save As, what type does Excel think it really is?
– Gagravarr
                Aug 6, 2022 at 12:22
                @magicmn the error still occurs when I can try that, but thank you for your suggestion I appreciate it
– justchillen
                Aug 8, 2022 at 1:09

I found an answer! So I as you see from the code above I passed my file as a byte array. Prior to that in my POST request I upload the file as a string and decode it from base 64 and into an byte array, that were the error occured. It wasn't decode correctly. I printed out what the String version of the file from the POST endpoint and that consist of "data:application/vmd.ms-excel;base64,OMBR4K(this the file data encoded in base 64 but Im not going to type all of it). Basically it wasn't decoding correctly because of everything before the comma was a regular text header. I spilt the string and saved everything after the comma in another string, and converted that into a byte array and decoded. Then my file was acceptable and I was able the create the workbook, and I also had to use .getSheetAt(int) instead of .getSheet("string")

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.