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