Create an Excel file 1: Basic Principles: Deliked with read spreadsheets; the first step is to create a writable Workbook object; like this import java.io.file; import java.util.date; import jxl. *; Import jxl.write. *;
...
Writableworkbook workbook = workbook.createworkbook (new file ("output.xls")); the next step is to create Sheets: Writablesheet Sheet = Workbook.createsheet ("first sheet", 0); // created one in the start position The name of the name for First Sheet is to add an element to the Sheet. This is very simple, such as: Add 3.14159 to D5: Number Number = New Number (3, 4, 3.1459); Sheet.Addcell (Number); in this way, you can add any more data, but you want Know the first point, when constructing a cell, the location of the cell in the worksheet is already determined. Once created, the location of the cell cannot be changed, although the content of the cell can be changed. In the second point, the positioning of the cell is the following regularity (COLUMN, ROW), and the subscript starts from 0, for example, A1 is stored in (0, 0), B1 is stored (1, 0) . Finally, don't forget to turn off the open Excel workmail object to release the memory, see the following code snippet: // Write an EXEL worksheet Wwb.write ();
// Turn off the Excel Workwork Object Wwb.close (); example: code (CREATEXLS.JAVA):
/ / Generate Excel's class import java.io. *; Import jxl. *; Import jxl.write. *;
Public class createxls {public static void main (string args []) {Try {// Open file Writableworkbook book = workbook.createworkbook (New File ("zsa.xls"));
// Generate a worksheet named "First Page", parameter 0 indicates this is the first page Writablesheet Sheet = BOOK.CREATESHEET ("First Page", 0);
/ / In the constructor of the Label object, the list element is the first row of first rows (0, 0) // and the cell content for Test label label = new label (0, 0, "zsa");
// Tim the defined cell to Sheet.Addcell (Label) in the worksheet;
/ * Generate a single-saved cell must use Number's full packet path, otherwise the slogan is the second column, the first line, the value is 1000.000 * / jxl.write.Number Number = new jxl.write. Number (1,0,1000.000); Sheet.Addcell (Number);
// Write the data and close the file book.write (); book.close ();
} catch (exception e) {system.out.println (e);}}}
After compiling execution, an Excel file will be generated in the current location.