Import the jxl package before running the code. The following code is for reference only:
Test the Excel file (I want to obtain the Excel content as the province, county, township, village, group, and pH values ):
The exceltest01 class code is as follows:
// Read the Excel class import Java. io. bufferedwriter; import Java. io. file; import Java. io. filewriter; import jxl. cell; import jxl. sheet; import jxl. workbook; public class exceltest01 {public static void main (string ARGs []) {try {system. out. println ("begini"); workbook book = workbook. getworkbook (new file ("a.xls"); // obtain the first worksheet object sheet = book. getsheet (0); // get the cell try {file = new file ("C:/iai.txt") in the first row of the first column; If (file. exists () {file. delete ();} file. createnewfile (); bufferedwriter output = new bufferedwriter (New filewriter (File); For (INT I = 2; I <4896; I ++) {Cell cell1 = sheet. getcell (23, I); // phcell cell2 = sheet. getcell (4, I); // save cell cell3 = sheet. getcell (7, I); // County cell cell4 = sheet. getcell (8, I); // township cell cell5 = sheet. getcell (9, I); // village cell cell6 = sheet. getcell (10, I); // group string pH = cell1.getcontents (); string Sheng = cell2.getcontents (); string Xian = cell3.getcontents (); string Xiang = cell4.getcontents (); string Cun = cell5.getcontents (); string zu = cell6.getcontents (); system. out. println ("nth" + (I + 1) + "row" + Sheng + Xian + Xiang + Cun + zu + "\ t" + pH); output. write ("th" + (I + 1) + "row" + Sheng + Xian + Xiang + Cun + zu + "\ t" + Ph + "\ n "); output. newline ();} output. close ();} catch (exception ex) {system. out. println (Ex);} book. close (); system. out. println ("end");} catch (exception e) {system. out. println (e );}}}
Shows the running result:
The TXT text is generated as follows: