jxl is a very practical Java library dedicated to manipulating Excel tables. It allows us to create, read, and modify Excel files easily. Next, I will tell you in detail how to use jxl in Excel table operation processing.
1. Introduce the jxl library
First, you have to add the jxl library to your project. If you are using a Maven project, add the following dependencies to the file:
<dependency> <groupId></groupId> <artifactId>jxl</artifactId> <version>2.6.12</version> </dependency>
2. Create an Excel file
Next, let's see how to create a simple Excel file using jxl.
import ; import ; import ; import ; import ; public class CreateExcelExample { public static void main(String[] args) { try { // Create a writable workbook object WritableWorkbook workbook = (new File("")); // Create a worksheet with parameters as the worksheet name and worksheet index WritableSheet sheet = ("Sheet1", 0); // Add data in the specified cell of the worksheet Label label = new Label(0, 0, "Hello, jxl!"); (label); // Write data and close the workbook (); (); ("Excel file was created successfully!"); } catch (Exception e) { (); ("Excel file creation failed:" + ()); } } }
Code explanation:
WritableWorkbook workbook = (new File(""));: Create a writable workbook object, specifying the generated Excel file name as .
WritableSheet sheet = ("Sheet1", 0);: Create a worksheet named Sheet1 with an index of 0, indicating the first worksheet.
Label label = new Label(0, 0, "Hello, jxl!");: Create a label object with the parameters of column index, row index and cell content respectively.
(label);: Add a label to the specified cell of the worksheet.
(); ();: Write data to a file and close the workbook.
3. Read Excel files
Below is a sample code for reading Excel files using jxl.
import ; import ; import ; import ; public class ReadExcelExample { public static void main(String[] args) { try { // Create a workbook object to read data from the specified file Workbook workbook = (new File("")); // Get the first worksheet Sheet sheet = (0); // Get the number of rows and columns of the worksheet int rows = (); int cols = (); // Iterate through the cells of the worksheet and output the content for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { Cell cell = (j, i); (() + "\t"); } (); } // Close the workbook (); } catch (Exception e) { (); ("Excel file reading failed:" + ()); } } }
Code explanation:
Workbook workbook = (new File(""));: Creates a workbook object to read data from the file.
Sheet sheet = (0);: Get the first worksheet.
int rows = (); int cols = ();: Get the number of rows and columns of the worksheet.
Cell cell = (j, i);: Get the contents of the specified cell.
();: Close the workbook.
4. Modify Excel files
jxl can also be used to modify existing Excel files. Here is a simple example to modify the contents of the specified cell to a new value.
import ; import ; import ; import ; import ; import ; public class ModifyExcelExample { public static void main(String[] args) { try { // Open an existing workbook Workbook existingWorkbook = (new File("")); // Create a writable workbook object based on an existing workbook WritableWorkbook workbook = (new File("modified_example.xls"), existingWorkbook); // Get the first worksheet WritableSheet sheet = (0); // Modify the content of the specified cell WritableCell cell = (0, 0); if (cell instanceof Label) { Label label = (Label) cell; ("Modified Content"); } // Write data and close the workbook (); (); (); ("Excel file modification was successful!"); } catch (Exception e) { (); ("Excel file modification failed:" + ()); } } }
Code explanation:
Workbook existingWorkbook = (new File(""));: Open an existing Excel file.
WritableWorkbook workbook = (new File("modified_example.xls"), existingWorkbook);: Create a writable workbook object based on an existing workbook, and specify the modified file name as modified_example.xls.
WritableCell cell = (0, 0);: Gets the writable object of the specified cell.
("Modified Content");: Modify the content of the cell.
(); (); (); ();: Write data and close the workbook.
This is the end of this article about Java using jxl library to easily play Excel table operations. For more related Java jxl operation Excel content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!