Java Operations EXCEL

Source: Internet
Author: User

Today, a friend wrote a program to deal with Excel content, here to record their own learning process. Mainly is the Excel table content to do the classification and the statistics, uses the computer to do this kind of repetitive mechanical work to be good. First, we need to download a Java operation open source plugin for Excel, I am using jexcelapi (download link: http://120.52.73.49/nchc.dl.sourceforge.net/project/jexcelapi/ Jexcelapi/2.6.6/jexcelapi_2_6_6.zip). For decompression, if you use an IDE such as Eclipse, you need to add the Jxl.jar file to the Project->properties->java Build Path. The development environment is now complete. Here's how to do it:

1, defines the data type, because each column data type is different, here takes the class data type to store, uses the object array to store each row's content.

classrecord{Private intnum; Private CharTy;  Public voidSetnum (String s1) { This. num=Integer.parseint (S1); }     Public voidSetty (String s2) { This. ty= (Char) s2.getbytes () [0]; }         Public intGetnum () {returnnum; }          Public CharGetTy () {returnTy; }} record a[]=NewRecord[row]; for(inti=0;i<row;i++) A[i]=NewRecord ();
View Code

Note that the procedure for each element of an array of objects is initialized in the code, otherwise the java.lang.NullPointerException exception is thrown.

2, connect to Excel, get workspace, we used the workbook and sheet class, in the previously imported JXL package inside.

// Create a container that reads Excel Workbook book = Workbook.getworkbook (new File ("G:/test123.xls")); // read the first page Sheet Sheet = book.getsheet (0); // read spreadsheet and number of columns and rows int col = sheet.getcolumns (); int row = Sheet.getrows ();
View Code

3. Read the contents of each cell. After reading the cell's data into an array, the next step is to do the actual work according to the specific situation.

 for (int i=0;i<row;i++) {    // The first parameter represents a column, the second parameter represents the row    Cell Cell1=sheet.getcell (0, i);    Cell cell2=sheet.getcell (1, i);    A[i].set_num (Cell1.getcontents ());    A[i].set_ty (Cell2.getcontents ());}
View Code

4, according to the requirements to complete the relevant operation, the data will need to write the Excel table, we used the Writableworkbook and writablesheet two classes.

//Create a writable Excel fileWritableworkbook bookw = Workbook.createworkbook (NewFile ("G:/testwtb.xls"));//write to the first page of ExcelWritablesheet SHEETW = Bookw.createsheet ("First sheet", 0); for(inti=0;i<=index;i++) {Number n=NewNumber (0, I,x[i]);    Sheetw.addcell (n); Number N1=NewNumber (1, I,y[i]);    Sheetw.addcell (N1); Number N2=NewNumber (2, I,z[i]);    Sheetw.addcell (N2); Number N3=NewNumber (3, I,xx[i]); Sheetw.addcell (n3);} Bookw.write (); Bookw.close ();
View Code

At this point, all operations are completed.

Java Operations EXCEL

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.