开发者

Read excel file from URL using POI

开发者 https://www.devze.com 2023-03-06 21:34 出处:网络
I want to read an excel file from a URL using Apache POI in java. I used the code like this : String urlStr = \"<url>\";

I want to read an excel file from a URL using Apache POI in java. I used the code like this :

String urlStr = "<url>";
URL url = new URL(urlStr);
URLConnection uc = url.openConnection();
HSSFWorkbook wb = new HSSFWorkbook(uc.getInputStream())

This is giving me an exception :: java.io.IOException: Unable to read entire header; 0 bytes read; 开发者_JAVA技巧expected 512 bytes Any idea why this is happening? Am I doing something wrong in converting the url to stream?

I had downloaded the excel to the local disk and created the POI object using FileInputStream and it works fine. Hence I think there is no problem with the excel. It should be some problem with the code reading the URL stream.


You should move to a newer version of POI. Older versions of POI were more sensitive to buffering on the input stream than newer ones were. I believe that with 3.7 it ought to be fully fixed.

If you can, move to POI 3.8 beta 2, or 3.7 Final. You may find 3.2 an improvement over 2.5, but they're both quite old releases so I can't be sure.

If you must stick with a very old version, you'll need to do some buffering yourself. Make sure at least the first 4 blocks (2048 bytes) are available before calling POI.


Try Googling Unable to read entire header; 0 bytes read; expected 512 bytes

Lots of hits mention POI and one in particular mentioned something about reading older Excel (95) files. Tried opening a different file through an url?

0

精彩评论

暂无评论...
验证码 换一张
取 消