我正在尝试创建一种方法来填充 excel 表是否已填满,但我不知道当 excel 表已填满时如何实现代码。有人能帮助我吗?这是代码:
我正在使用 API JXL
public void fillData(JTable table, File file) {
try {
boolean controladorExcel = false; //Excel controler, if is filled up or not
Workbook workbookPreenchido = Workbook.getWorkbook(new File(file.getName())); //workbook is filled up
Sheet sheet = workbookPreenchido.getSheet(0);
Cell a1 = sheet.getCell(0, 0);
String as1 = a1.getContents();
if(as1 == null){
controladorExcel = true; //Excel table is blank
}else{
controladorExcel = false; //Excel table is filled up
}
workbookPreenchido.close();
if (controladorExcel == true) { //If the excel table is blank
WritableWorkbook workbookVazio = Workbook.createWorkbook(file); //workbook is blank
WritableSheet sheet1 = workbookVazio.createSheet("First Sheet", 0);
TableModel model = table.getModel();
for (int i = 0; i < model.getColumnCount(); i++) {
Label column = new Label(i, 0, model.getColumnName(i));
sheet1.addCell(column);
}
int j = 0;
for (int i = 0; i < model.getRowCount(); i++) {
for (j = 0; j < model.getColumnCount(); j++) {
Label row = new Label(j, i + 1,
model.getValueAt(i, j).toString());
sheet1.addCell(row);
}
}
workbookVazio.write();
workbookVazio.close();
}else{ //If the excel table is filled up
}
} catch (Exception ex) {
ex.printStackTrace();
}
}