简体   繁体   中英

Creating and accessing sheets in excel using java jxl

I'm creating an excel file using jxl. I'd like to name the sheets with dates - 22-03-2012(1st sheet), 23-03-2012(2nd sheet), and so on. Here I'm creating the file and the first sheet (this runs only once):

    File file = new File(inputFile);
    WorkbookSettings wbSettings = new WorkbookSettings();
    wbSettings.setLocale(new Locale("en", "EN"));

    Date now = Calendar.getInstance().getTime();
    DateFormat df = new SimpleDateFormat("MM-dd-yyyy");
    String theDate = df.format(now);
    System.out.println(theDate);

    WritableWorkbook workbook = Workbook.createWorkbook(file, wbSettings);
    workbook.createSheet(theDate, 0);
    // now access it and do some operations
    WritableSheet excelSheet = workbook.getSheet(theDate);
    prepareSheet(excelSheet);
    workbook.write();
    workbook.close();

Now, when I access the file and modify it later on (another class - runs everytime you want to add more data), I need to check whether "it's still today". In other words, if the name of the last sheet is 22-03-2012 and today is 22-03-2012 -> access and modify this sheet, and if the name of the last sheet is different from current date -> create a new sheet. I guess this is a matter of simple "if" statement, but for some reason I can't make it work. Need fresh eyes :) Thanks

use following method to get a specific sheet; if it's null, create a new one:

Sheet getSheet(String name)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM