Use jxl to Operate Excel in Java

Source: Internet
Author: User

Jxl is a Korean-written Java Excel tool. In the Java open-source world, there are two sets of influential APIs available, one is poi, one is jexcelapi (jxl ). Jxl is a little weaker than poi.

However, jexcelapi provides excellent support for Chinese characters. The API is pure Java and does not rely on Windows systems. Even if it runs in Linux, it can process Excel files correctly. It should be noted that jxl has limited support for graphics and charts and only recognizes images in PNG format.

The following describes some common jxl operations, such as reading an Excel file and creating an Excel merge cell.

Jxl and poi are compared based on the collected information on the network.

1. Download the class library package jxl. jar, download the jxl. Jar class library (2.6.9 is used), and add it to classpath. Attached jxl. jar: http://sourceforge.net/projects/jexcelapi/files/jexcelapi/2、 (workbook): corresponds to an Excel worksheet (sheet) in Excel, corresponding to some cells in the Red Circle in the following figure: in Excel, it corresponds to the section in the red circle below. 3. Use jxl to read the Excel file.
Import Java. io. fileinputstream; import Java. io. ioexception; import Java. io. inputstream; import jxl. cell; import jxl. sheet; import jxl. workbook; import jxl. read. biff. biffexception; public class readexcel {public static void main (string [] ARGs) throws biffexception, ioexception {// 1. Construct an Excel file input stream object string sfilepath = "D:/1.xls "; inputstream is = new fileinputstream (sfilepath); // 2. Declare the workbook object workbook RWB = workbook. getworkbook (is); // 3. Obtain the number of workbooks, which corresponds to the number of worksheets in an Excel worksheet RWB. getnumberofsheets (); sheet ofirstsheet = RWB. getsheet (0); // you can use the index to obtain the first worksheet. You can also use RWB. getsheet (sheetname); The sheetname indicates the worksheet name // system. out. println ("worksheet name:" + ofirstsheet. getname (); int rows = ofirstsheet. getrows (); // obtain the total number of rows in the worksheet, int columns = ofirstsheet. getcolumns (); // obtain the total number of columns in the worksheet for (INT I = 0; I <rows; I ++) {for (Int J = 0; j <columns; j ++) {Cell ocell = ofirstsheet. getcell (J, I); // You must note the parameters of the getcell method. The first parameter specifies the column number, and the second parameter specifies the row number system. out. println (ocell. getcontents () + "\ r \ n ");}}}}
4. Write content to an Excel file
Import Java. io. file; import Java. io. ioexception; import jxl. workbook; import jxl. write. label; import jxl. write. writablesheet; import jxl. write. writableworkbook; import jxl. write. writeexception; import jxl. write. biff. rowsexceededexception; public class writeexcel {public static void main (string [] ARGs) throws ioexception, rowsexceededexception, writeexception {// 1. Create a workbook (writableworkbook) object and open an Excel file, if the file does not exist, create the file writableworkbook writebook = workbook. createworkbook (new file ("D: // write.xls"); // 2. Create a worksheet (sheet) object and declare the page on which it belongs to writablesheet firstsheet = writebook. createsheet ("first Workbook", 1); // The first parameter is the name of the workbook, and the second parameter is the number of pages writablesheet secondsheet = writebook. createsheet ("second Workbook", 0); // 3. Create a cell (Label) object. Label label1 = new label (1, 2, "test1 "); // The first parameter specifies the number of columns in the cell, the second parameter specifies the number of rows in the cell, and the third specified string content firstsheet. addcell (label1); label label2 = new label (1, 2, "Test2"); secondsheet. addcell (label2); // 4. Open the stream and start writing the file writebook. write (); // 5. Close the stream writebook. close ();}}
5. Merge Cells
Import Java. io. file; import Java. io. ioexception; import jxl. workbook; import jxl. read. biff. biffexception; import jxl. write. label; import jxl. write. writablesheet; import jxl. write. writableworkbook; import jxl. write. writeexception; import jxl. write. biff. rowsexceededexception; public class mergecell {public static void main (string [] ARGs) throws ioexception, rowsexceededexception, writeexception, biff1_tio N {// 1. Create the writableworkbook object file = new file ("D:/write.xls"); writableworkbook owritablebk = workbook. createworkbook (File); // 2. Create the writablesheet object writablesheet owritablesheet = owritablebk. createsheet ("testsheet1", 0); // 3. Add the cell label label1 = new label (0, 0, "test1"); owritablesheet. addcell (label1); label label30 = new label (3, 1, "I am the merged cell! "); Owritablesheet. addcell (label30); // 4. Merge the cells owritablesheet. mergecells (, 9, 1); // parameter description. The first two parameters are the start cell location to be merged, and the last two parameters are used to specify the end cell location (column and row) owritablebk. write (); owritablebk. close ();}}
Comparing poi and jxl poi does not support writing images (jxl supports only PNG images)
Poi provides better support for formulas, while jxl does not. Therefore, poi is good for financial software.
Memory overflow occurs when the data volume is large:
Use poi: memory overflow is reported when about 2800 messages are run, and memory overflow is reported when jxl to 3000 messages are used.
Excel reading speed: poi is better than jxl
Data insertion rate: jxl is better than poi

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.