The GetRow () method returns NULL when reading Excel using a POI

Source: Internet
Author: User

The new one, talk about the problems at work today.

Because of the work required, when doing a function to download Excel files, it is required to read the template file on the server to write data and then transfer to the client. I've just been working for a while, and because there are so few technologies that I've been exposed to for some reason, I've used the title on the read template to correlate with the data fetched from the database, and then write, if there's a better way, enlighten me.

After writing the test encountered a problem, that is, the first row of the template header row has data, but the GetRow () method to take the row is null, Meng forced, check for a long time to find out, Baidu also search, and finally with their own written another similar method of comparison, the reason to find out! You cannot use Sxssfworkbook to read Excel data, read data with Xssfworkbook!

The original code is:

File test =NewFile ("D:\\test.xlsx");Try{Xssfworkbook WB=NewXssfworkbook (NewFileInputStream (test)); Sxssfworkbook Workbook=NewSxssfworkbook (WB); Sheet Sheet= Workbook.getsheetat (0);  for(inti = 0; I < 5; i++) {row row= Sheet.getrow (i); if(row==NULL) {System.out.println (i+ ": null"); }  }} Catch(Exception e) {e.printstacktrace ();}

The result of the final console output is:

After you change the sheet object's acquisition to Wb.getsheetat (0), you get the row normally.

File test =NewFile ("D:\\test.xlsx");Try{Xssfworkbook WB=NewXssfworkbook (NewFileInputStream (test));//Sxssfworkbook workbook = new Sxssfworkbook (WB);Sheet Sheet = wb.getsheetat (0);  for(inti = 0; I < 5; i++) {row row=Sheet.getrow (i); if(row==NULL) {System.out.println (i+ ": null"); }    }} Catch(Exception e) {e.printstacktrace ();}
View Code

The result of the output is:

I do not know why this situation occurs, if there are friends who know, hope to guide.

The GetRow () method returns NULL when reading Excel using a POI

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.