If you use Apache POI
from Java:
To open an existing sheet or to create a new sheet respectively:
final File file = "/tmp/sheet.xls";
final HSSFWorkbook workbook;
if (file.exists() == false) {
System.out.println("Creating a new workbook '" + file + "'");
workbook = new HSSFWorkbook();
} else {
System.out.println("Appending to existing workbook '" + file + "'");
final InputStream is = new FileInputStream(file);
try {
workbook = new HSSFWorkbook(is);
} finally {
is.close();
}
}
To check whether a sheet exists in order to create a unique sheet name, you could use something like this:
int sheetIndex = 1;
while (workbook.getSheet("Sheet " + sheetIndex) != null) {
sheetIndex++;
}
then you can add a sheet by calling createSheet
:
HSSFSheet sheet = workbook.createSheet("Sheet " + sheetIndex);
In this case the sheet names are "Sheet 1", "Sheet 2", etc.