1. Read
//reads each row of data in Excel specified sheet, storing it in a two-dimensional array, including the first row Public StaticString[][] Getsheetdata (xssfsheet sheet)throwsIOException {string[][] Testarray=Newstring[sheet.getphysicalnumberofrows () []; for(intRowId =0;rowid<sheet.getphysicalnumberofrows (); rowid++) {Xssfrow row=Sheet.getrow (rowId); List<String> testsetlist =NewArraylist<string>(); for(intColumn=0;column<row.getphysicalnumberofcells (); column++) {Row.getcell (column). Setcelltype (cell.cell_type_string); Testsetlist.add (Row.getcell (column). Getstringcellvalue ()); } Testarray[rowid]=(string[]) testsetlist. ToArray (Newstring[testsetlist.size ()]); } returnTestarray; }
2. Printing
// print two-dimensional arrays Public Static void throws ioexception{ for (int i =0; i<testarray.length;i++ ) { for (int j=0; j<testarray[i].length;j++) { print (Testarray[i][j]+ "| |" ); } println (); } }
3. Call
Public Static voidMain (string[] args)throwsIOException {//TODO Auto-generated method stubsFile File=NewFile ("Testdata\\testdata.xlsx"); FileInputStream FIS=Newfileinputstream (file); @SuppressWarnings ("Resource") Xssfworkbook WB=NewXssfworkbook (NewBufferedinputstream (FIS)); Printdoublearray (Getsheetdata (Wb.getsheetat (0))); }
4. Effects
Java reads Excel to specify rows of data in sheet, storing two-dimensional arrays, including the first row, and printing