Problem with reading files in Java

Asked

Viewed 638 times

1

I have a module in an application in Java reading and processing files .xls, .xlsx and .csv.

It is a web application, where the user uploads a file with some of these formats, via Rest, and when the file is on the server, my Java application processes it.

This process consists of reading the file, scanning field by field, or column by column, taking the data, to insert it into the database. So far I have no problem. I managed to make my application work correctly, using the APACHE-POI.

The big problem is that one of the files that the user needs to import for my application to process is generated by an SAP. And it is saved as .xls, however at the time my application tries to read the file it brings me the exception:

java.io.IOException: Invalid header signature; read 0x0020000A000DFEFF, expected 0xE11AB1A1E011CFD0 - Your file appears not to be a valid OLE2 document

And from what I’ve researched, that exception says that my file .xls, not a file . xls.

I’ve been researching, and it’s like SAP exports a file .csv, however it sends with the extension .xls. In case I open the file and give a save as .xls, or .csv, the process works correctly.

An alternative solution I thought was, if it is a .xls, and this exception occurs in particular, I send the file to the method that reads the .csv and he sues. Literally worked, I can "read" the file, but I can’t extract the data because apparently the file does not have the field delimiter defined.

[UPDATE]

Inspect via debug I managed to get the correct field by setting the delimiter as Tab. According to the image, at debug time, the value of the field[i] is empty, and inside it I have "value", which is really the value I need. But I have no idea how to take this figure, someone can help me?

  • 1

    Your approach is correct. You can open the file. csv in a text editor and check if the delimiter is positioned correctly in the file. In . csv files, the delimiter is comma.

  • @Bernardobotelho Thanks for the help, but open the file (xls, as it comes) in the text editor, and has no delimiter. This is my big problem. If it is totally impossible this, unfortunately I will have to appeal to the user to open the file and go on saving as, rs, but this would not be cool.

  • 1

    Maybe the delimiter is spaces or tabs?

  • @Bernadorbotelho If the delimiter is the tab, can you tell me how I can set it? I tried for space and it didn’t work. One thing a little bad, is that this way, if any record the field is empty, the other fields come 'one house' before. Business is really very complicated.

  • 1

    It will depend on how you are reading the file. If you are using split, use it like this: .split file("t").

  • @Bernardobotelho did a better test, and I saw that I managed to get the fields, using tab, thanks, but now I could see the problem, why the empty fields are coming. I updated the question inserted a print of the Inspect.

  • If it is a CSV itself, you’d better figure out which is the delimiter (inspecting in Notepad++) and opening with superCSV or something like.

Show 2 more comments

1 answer

1


To solve, in this particular case this file, which is actually a . csv, but with the extension . xls, I used a method already developed before for reading csv files and it worked. Thus able to extract the data from the file and insert it into a database.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.