POI: complete example of reading and writing Excel

Source: Internet
Author: User

Process: (similar to JXL, just reading logic a little different)

The two main differences with JXL:

1, read and write a slightly different way:

(1) POI read:

[Java] view plain copy print? <span style= "White-space:pre" > </span> fileinputstream input = new FileInputStream (New File (Xls_read_  address));         Read the file path Xssfworkbook wb = new Xssfworkbook (new Bufferedinputstream (input)); Poi writes: Which line to locate first, and then write the contents of each cell in the loop of the line;

(2) JXL read:

[Java] view plain copy print? <span style= "White-space:pre" > </span> Workbook book = Workbook.getworkbook (New File (xls_read_address),    Workbooksettings); JXL write: Arbitrary positioning row number, write more flexible, of course, write the entire table, or with poi like to traverse.

2. The data types are judged in a slightly different way:

(1) Personally feel that the data type processing, poi than JXL convenient and practical, poi in dealing with the value of numeric types is very easy to go wrong (without text), you can personally try.

(2) JXL processing is very good, like the general operation of the text only need to use the line.

----------------------------------------------------------------------------------

1, with POI reading Excel table (incoming Excel address, etc.):

(1) Reading Excel, all the "worksheets" in Excel are encapsulated into arraylist<arraylist> ls;

(2) Reading the worksheet, traversing LS, and encapsulating all the data of each worksheet into the arraylist<sring[]> ls_a;

(3) Reading each row, traversing the ls_a, putting the contents of all columns in each row, encapsulating the array sring[] s;

(4) The contents of the cell to operate, such as replacement value, set width, and so on.

(5) After the operation, assign value to the corresponding s[i], that is, rewrite the contents of the s[i], then encapsulated into the ls_a, and finally the ls_a encapsulated into LS;

(6) Close flow: Input.close ();


2, with JXL to write Excel table (incoming arraylist<arraylist>, new Excel address, etc.):

(1) using arraylist<arraylist> ls to receive the above transmission value, traversing LS;

(2) using arraylist<sring[]> ls_a to receive the traversed Ls.get (i), and then traversing the ls_a;

(3) using string[] s to receive the ls_a;

(4) with [Java] view plain copy print?                                           <span style= "White-space:pre" > </span>xssfcell cell = Row.createcell (cols); Cell.setcelltype (xssfcell.cell_type_string);//Text Format cell.setcellvalue (S[cols]);//write The content writes each s[cols] to the cols+1 column.

(5) Close stream: [Java] view plain copy print?           <span style= "White-space:pre" > </span>wb.write (Output); Output.close ();

----------------------------------------------------------------------------------

Scenario: Processing 7000 row data per minute

Operating environment: Tomcat 6.0.36 +JDK 1.6 + IE9

----------------------------------------------------------------------------------

1. It is recommended to build a Web project rather than a Java project; The advantage is that it can be used both as Web Access and as a Java engineering test;

2, under the SRC build a package, two Java files, such as: Config.java;

3, Description: Config.java, all operations are completed here;

4, add JXL package, by the way if you use the database, add the database package, such as Oracle in this case;

5. The information to be modified before testing is:

(1) The package is located in the path, class name, and so on according to the prompts to modify;

(2) Database information configuration, this should be manually modified, if there is no database, it is more simple, this example of the call to the database code directly removed.

(3) Various methods of incoming parameters and the modification of outgoing parameters: such as Excel file path, etc.

6, the source code is as follows (the source code has called to a Dataconvert class, this class is a basic data type conversion package class, you can write a method to replace on the line):

(1) Config.java [Java] view plain copy print? Package&n

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.