Packages that need to be imported: jxl.jar
The code copy is as follows:
public void readTO() {
Workbook wb = null;
WritableWorkbook wwb = null;
try {
File is = new File(System.getProperty("user.dir") + "//in.xls");
File os = new File(System.getProperty("user.dir") + "//out.xls");
if (!os.isFile())// If the specified file does not exist, create a new file
os.createNewFile();
wb = Workbook.getWorkbook(is);// Get the data source in.xls workbook object
wwb = Workbook.createWorkbook(os, wb);// Append data to the original workbook out.xls
// wwb = Workbook.createWorkbook(os);//Different from the previous line of code, create a new write workbook
if (wb != null && wwb != null) {
WritableSheet sheet = wwb.getSheet(0);// Get out.xls first sheet
WritableCell cell = sheet.getWritableCell(2, 4);// Get out.xls to write data cell
Sheet[] sheets = wb.getSheets();// Get sheets of data source in.xls
Cell[] cells = sheets[0].getRow(1);// Get the second line of the first sheet in.xls
if (cell.getType() == CellType.LABEL) {
Label l = (Label) cell;
l.setString(cells[1].getContents());// Write the second cell to the third column of out.xls, line 5
}
wwb.write();
System.out.println("The workbook writes data successfully!");
}
wwb.close();// Close
} catch (Exception e) {
e.printStackTrace();
} finally {
wb.close();
}
}