user3872094
user3872094

Reputation: 3351

Confused about creating a sheet

I'm writing a java code using POI, the aim here is to create sheets and add data into it. and the criteria is as below.

I've a text file with a set of keywords like below.

MainOne
MainTwo

And I'm looping the available sheets and creating a sheet based on this key word.

if there is no key word match, I want to add a sheet named no keyword to the workbook. and on each iteration, check if the sheet with keyword is available, if so use it, if not create a sheet.

Main Class

public static void main(String[] args) throws Exception {
        FileInputStream fileInputStream = new FileInputStream(new File("KeyWords.txt"));
        Scanner sc = new Scanner(fileInputStream);
        while (sc.hasNextLine()) {
            String line = sc.nextLine();
            BasicExcel.createACell(line);
        }
        sc.close();
    }

BasicExcel Class

public static void createACell(String keyWord) throws IOException {
        FileInputStream input_document = new FileInputStream(new File("C:\\Test\\new.xls"));
        HSSFWorkbook workbook = new HSSFWorkbook(input_document);
        int noOfSheets = workbook.getNumberOfSheets();
        HSSFSheet sheet = null;
        for (int j = 0; j < noOfSheets; j++) {
            if (!workbook.getSheetName(j).equalsIgnoreCase("No KeyWords")) {
               sheet = workbook.createSheet("No KeyWords");
            } else if (!workbook.getSheetName(j).equalsIgnoreCase(keyWord)) {
               sheet = workbook.createSheet(keyWord);
            } else {
               sheet = workbook.getSheet(keyWord);
            }
        }
        input_document.close();
        FileOutputStream out = new FileOutputStream(new File("C:\\Test\\new.xls"));
        try {
            workbook.write(out);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            out.close();
            workbook.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

when I run this program, I'm getting the below Exception.

java.lang.IllegalArgumentException: The workbook already contains a sheet named 'Insurance' at org.apache.poi.hssf.usermodel.HSSFWorkbook.createSheet(HSSFWorkbook.java:877) at BasicExcel.createACell(BasicExcel.java:22) at EmailTestWithScanner.readEmails(EmailTestWithScanner.java:115) at EmailTestWithScanner.main(EmailTestWithScanner.java:137) java.lang.IllegalArgumentException: The workbook already contains a sheet named 'MMS' at org.apache.poi.hssf.usermodel.HSSFWorkbook.createSheet(HSSFWorkbook.java:877) at BasicExcel.createACell(BasicExcel.java:22) at EmailTestWithScanner.readEmails(EmailTestWithScanner.java:115) at EmailTestWithScanner.main(EmailTestWithScanner.java:137)

please let me know where am I going wrong and how can I fix it.

Thanks

Upvotes: 0

Views: 961

Answers (1)

Nicolas Filotto
Nicolas Filotto

Reputation: 45005

You should check first if a sheet with the same name already exists using getSheetIndex(String name).

If it returns -1, it would mean that it doesn't exist yet so you would be able to call createSheet(String sheetname) safely otherwise you would be able to use getSheetAt(int index) to retrieve the corresponding HSSFSheet instance.

Here is the pseudo code that shows the idea:

int index = workbook.getSheetIndex("No KeyWords");
HSSFSheet sheet;
if (index == -1) {
    // It doesn't exist yet so let's create it
    sheet = workbook.createSheet("No KeyWords");
} else {
    // It already exists so let's get it
    sheet = workbook.getSheetAt(index);
}

Upvotes: 1

Related Questions