JXL This is a Korean writing Java Operations Excel tool, in the source world, there are two sets of more influential APIs to use. One is poi, one is jexcelapi. The function is relatively weaker than the POI. However, JEXCELAPI support for Chinese is very good, the API is pure Java, and does not depend on the Windows system, even if executed under Linux. It is the same as it can handle Excel files correctly. It is also important to note that the API's support for graphs and charts is limited and only the PNG format is recognized.
Use for example the following:
Build a good development environment
Unpack the downloaded file, get Jxl.jar, and put it in classpath. The installation is complete.
Basic operations
First, create a file
An Excel file named "Test.xls" is intended to be generated. The first worksheet is named
"First page", the approximate effect is as follows:
The package test;//generates an Excel class import Java.io.file;import JXL. Workbook;import Jxl.write.label;import Jxl.write.writablesheet;import Jxl.write.writableworkbook;public class createexcel {public static void main (String args[]) {try {//Open file Writableworkbook Book = Workbook.createworkbook (New File ("Test.xls")); Generate a worksheet named "first page", 0 means this is the first page writablesheet sheet = book.createsheet ("First page", 0); The named cell position in the Label object's construction is the first column in the first row (0,0)//And the cell contents are test label label = new label (0, 0, "test"); Add a defined cell to the worksheet Sheet.addcell (label); /* * Generate a cell that holds numbers must use the full package path of number, otherwise the syntax ambiguity cell position is the second column. The first line, the value is 789.123 */jxl.write.Number number = new Jxl.write.Number (1, 0, 555.12541); Sheet.addcell (number); Write data and close file Book.write (); Book.close (); } catch (Exception e) {System.out.println (e); } }}
After the compilation is run. An Excel file is generated.
Third, read the file
Take the Excel file we just created as an example, to do a simple read operation, the program code such as the following:
The package test;//reads the class import Java.io.file;import JXL of Excel. Cell;import JXL. Sheet;import JXL. Workbook;public class Readexcel {public static void main (String args[]) {try {Workbook book = Work Book.getworkbook (New File ("Test.xls")); Get the first sheet object Sheet Sheet = book.getsheet (0); Get the first row of cells cell cell1 = Sheet.getcell (0, 0); String result = Cell1.getcontents (); SYSTEM.OUT.PRINTLN (result); Book.close (); } catch (Exception e) {System.out.println (e); }}} program run result: Test Four, change the file using Jexcelapi can change the existing Excel file, when changing the Excel file, in addition to open the file in a different way. Other operations are the same as creating Excel. The following example adds a worksheet to the Excel file that we have generated: package Test;import java.io.file;import JXL. Workbook;import Jxl.write.label;import Jxl.write.writablesheet;import Jxl.write.writableworkbook;public class updateexcel {public static void main (String args[]) {try {//Excel get file Workbook wb = Wo Rkbook.getworkbook (NEW File ("Test.xls")); Open a copy of a file. And the specified data is written back to the original file Writableworkbook book = workbook.createworkbook (New file ("Test.xls"), WB); Join a worksheet Writablesheet sheet = book.createsheet ("Second page", 1); Sheet.addcell (New Label (0, 0, "second-page test Data")); Book.write (); Book.close (); } catch (Exception e) {System.out.println (e); } }}
Other operations
First, data formatting
Complex data types are not involved in Excel. It is possible to handle strings, numbers and dates well enough for general applications.
1. String formatting
The formatting of strings involves elements such as font, weight, font size, and so on. These functions are mainly composed of Writablefont and
Writablecellformat class to be responsible. If we are generating a cell containing a string, use the following statement, for example,
For the convenience of narration, we add a number for each line of the command:
Writablefont font1= New Writablefont (Writablefont.times,16,writablefont.bold); ①writablecellformat format1=new Writablecellformat (font1); ②label label=new Label (0,0, "Data 4 test", FORMAT1) ③
① Specifies the string format: the font is times. Font size 16. Bold display. Writablefont has a lot of
Constructs a child. For use in different situations. There are specific lists in the Java-doc of Jexcelapi. No longer listed here.
The ② code uses the Writablecellformat class, which is important to specify the cells ' various
attribute, there are many other descriptive narratives in subsequent cell formatting.
The constructor for the label class is used at the ③. Specifies that the string is given that format.
In the Writablecellformat class. Another very important way is to specify the alignment of the data, than the policy
The above example can specify:
The horizontal alignment is specified as the center format1.setalignment (Jxl.format.Alignment.CENTRE); The vertical alignment is specified as the center format1.setverticalalignment (Jxl.format.VerticalAlignment.CENTRE);
Second, cell operation
A very important part of Excel is the operation of cells. For example, row height, column width, cell merging, and so on. Fortunately Jexcelapi
These support are provided.
These operations are relatively straightforward. Here are just a few of the relevant APIs.
1. Merging cells
Writablesheet.mergecells (int m,int n,int p,int q); The function is all merges from (M,n) to (p,q) cell, for example: Writablesheet sheet=book.createsheet ("first page", 0); Merges all cells Sheet.mergecells (0,0,5,0) from the first row of the first column to the first row of column sixth;
A merger can be both horizontal and vertical. Merged cells cannot be merged again. Otherwise, an exception is triggered.
2, Row height and column width
Writablesheet.setrowview (int i,int height); The function is to specify the height of line i+1, for example://Set the height of the first row to Sheet.setrowview (0,200); Writablesheet.setcolumnview (int i,int width); The function is to specify the width of column i+1, for example://To set the width of the first column to Sheet.setcolumnview (0,30);
JEXCELAPI also has other functions, such as inserting pictures and so on, which are no longer introduced here, readers can explore themselves.
In: Suppose you read an Excel and need to know how many rows and columns it has, such as the following:
Workbook book = Workbook.getworkbook (New File ("Test 1.xls")); Get the first sheet object Sheet Sheet = book.getsheet (0); Gets the first column of the first row of cells int columnum = Sheet.getcolumns ();//Gets the number of columns int rownum = Sheet.getrows ();//Gets the number of rows System.out.println (columnum); System.out.println (rownum); for (int i = 0; i < rownum; i++)//loop read/write {for (int j = 0; J < Columnum; J + +) { Cell cell1 = Sheet.get Cell (J, i); String result = Cell1.getcontents (); System.out.print (result); System.out.print ("\ t"); } System.out.println (); } Book.close ();
Reference:
Http://www.cnblogs.com/sunzhenxing19860608/archive/2010/12/27/1918128.html
Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.
Java JXL operation xls form