MS's spreadsheet (Excel) is an important member of Office, which is a common format for saving statistics. As a office document, the exchange of electronic documents to be involved, Excel is a very common file format, printing and management in the enterprise. In a Java application, part of the data generates an Excel format is an important means of seamless connection with other systems. In the open source world, there are two more affected APIs available, one is POI, one is Jexcelapi. Among them, Jexcelapi is a Korean programmer, although there is no POI's low bloodline, but in the process of the author, it feels simple and convenient, and it is very good for Chinese support, and the function is also relatively powerful. Its download address is: http://www.andykhan.com/jexcelapi/ The current highest version is 2.4. The features of the author's website have the following description: ● Support all versions of Excel 95-2000 ● Generate Excel 2000 standard format ● Support font, numbers, date operation ● Can modify cell properties ● Support images and charts should say that the above features It is already able to meet our needs. The most critical is that this API is pure Java, does not rely on the Windows system, even if it is running under Linux, it can also process the Excel file correctly. It is also necessary to explain that this API supports the support of graphics and charts, and only recognizes the PNG format. The construction environment will be unpacking the files after downloading, get JXL.jar, put it in ClassPath, and complete it. Basic operation 1. Creating a file to generate an Excel file called "Test Data .xls", where the first worksheet is named "First Page", the rough effect is as follows: Codexls.java: // Generate Excel-based import java.io. *; import jxl *;. import jxl.write *;. public class CreateXLS {public static void main (String args []) {try {// open the file WritableWorkbook book = Workbook.createWorkbook ( New file ("Test .xls")); // Generate a worksheet named "First Page", parameter 0 indicates this is the first page Writablesheet Sheet = BOOK.CREATESHEET ("First", 0); / / In the constructor of the Label object, the list element is the first column first row (0, 0) // and the cell content as Test label label = new label (0, 0, "test"); // Define good cells to add Sheet.Addcell (Label) in the worksheet; / * Generate a set of cells that must use Number's full packet path, otherwise the slogan of speech is the second column, the first line, The value is 789.123 * / jxl.write.Number Number = new jxl.write.Number (1, 0, 789.123); Sheet.Addcell (Number); // Write data and close file book.write (); book.close () After Catch (Exception E) {system.out.println (e);}}} After compiling execution, an Excel file will be generated in the current location.
Third, read the document as an example, make a simple read operation, the program code is as follows: // Read the Excel class import java.io. *; import jxl. *; Public class readxls { Public static void main (string args []) {Try {workbook book = workbook.getworkbook (new file ("test .xls")); // Get the first worksheet object Sheet Sheet = BOOK.GETSHEET (0); // Get the first line of the first line of cell cell cell1 = sheet.getcell (0, 0); string results = cell1.getContents (); system.out.println (result); book.close ();} catch (Exception E) {system.out.println (e);}}} program execution result: Test 4, modifying files Utilize Jexcelapi to modify excel files, modify the Excel file, except for opening files Other operations and create Excel are the same. The following example is to add a worksheet to the Excel file we have generated: // Modify the Excel class, add a worksheet Import java.io. *; import jxl. *; Import jxl.write. *; Public class updatexls {public static void main (string args []) {Try {// Excel gets fileworkbook wb = workbook.getWorkbook (New file ("test .xls")); // Open a copy of a file, and specify data writing back Go to the original file Writableworkbook book = workbook.createworkbook (new file ("test .xls"), wb); // Add a worksheet Writablesheet Sheet = BOOK.CREATESHEET ("Page II, 1); Sheet.Addcell (New Label (0, 0, "Test Data"))))); book.write (); book.close ();} catch (exception e) {system.out.println (e);}}} As shown in the figure: Advanced Operation 1. Data format does not involve complex data types in Excel, which is better to process strings, numbers, and dates. 1. The formatting of strings formatted strings involves elements such as fonts, thickness, and font numbers, which are primarily responsible for WritableFont and WritablecellFormat classes. Suppose we use the following statement when generating a string-containing cell, to make it easy to describe, we will add numbers for each line of command: WritableFont Font1 = New WritableFont (WritableFont.Times, 16, WritableFont.Bold); 1 WritableCellFormat Format1 = New WritablecellFormat (font1); 2 label label = new label (0, 0, "Data 4 Test", Format1) 3 where 1 specifies the string format: The font is Times, the font size 16, bold display. Writablefont has a very rich structure for use in different situations, and there is a detailed list in JEXCELAPI's Java-DOC, which is no longer listed here.