Java reads and writes excel files and java reads and writes excel files
Requirement: use Java to read and write excel files
Use jexcelapi to read and write excel files in Java
First download and install jexcelapi
JExcelApi v2.6.12 (1911 kbytes)
Decompress the package and add the jxl. jar file to the Java Build Path.
Prepare the test excel file (D: \ test \ employee .xls)
Java reads excel files
Package com. test. IF. service; import java. io. file; import java. io. IOException; import jxl. cell; import jxl. sheet; import jxl. workbook; import jxl. read. biff. biffException; public class HandleExcelFile {private String excelFilePath = "D: \ test \ employee .xls"; private String [] [] nReadArray; private int nRows, nColumns = 0; Workbook xWorkbook; // convert the content in Excel to a two-dimensional array and return public String [] [] readExcel () {try {xWorkbook = Workbook. getWorkbook (new File (excelFilePath); Sheet sheet = xWorkbook. getSheet (0); nRows = sheet. getRows (); nColumns = sheet. getColumns (); nReadArray = new String [nRows] [nColumns]; for (int I = 1; I <nRows; I ++) {for (int j = 0; j <nColumns; j ++) {Cell cell = sheet. getCell (j, I); // (column, row) String content = cell. getContents (); nReadArray [I] [j] = content ;}} catch (BiffException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} xWorkbook. close (); return nReadArray;} public static void main (String [] args) {HandleExcelFile he = new HandleExcelFile (); String [] [] xLine = he. readExcel (); for (int I = 1; I <xLine. length; I ++) {for (int j = 0; j <xLine [I]. length; j ++) {System. out. print (xLine [I] [j] + "|");} System. out. println ();}}}
Test the read program. The result is
Write excel files in Java