Obtain Excel Data (or part of data) and export it to TXT text format

Source: Internet
Author: User

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:

 

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.