Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Multiple Sheet in jExcel API

I need to create multiple excel sheet in Java using jExcel API if one sheet is full (65536 rows). Suppose if one sheet got full then in the next sheet it should start writing automatically from where it left off in the first sheet. I am just stuck on putting the logic to create it dynamically whenever one sheet is full. Below is the code that I have done so far.

public void write() throws IOException, WriteException {
    File file = new File(inputFile);
    WorkbookSettings wbSettings = new WorkbookSettings();

    wbSettings.setLocale(new Locale("en", "EN"));

    WritableWorkbook workbook = Workbook.createWorkbook(file, wbSettings);

    writingToExcel(workbook);
}


//Logic to create sheet dyanmically if one is full should be done here I guess?
private void writingToExcel(WritableWorkbook workbook) {

    workbook.createSheet("Report", 0);
    WritableSheet excelSheet = workbook.getSheet(0);
    try {
        createLabel(excelSheet);
        createContent(excelSheet);
    } catch (WriteException e) {
        e.printStackTrace();
    } finally {
        try {
            workbook.write();
            workbook.close();   
        } catch (IOException e) {
            e.printStackTrace();
        } catch (WriteException e) {
            e.printStackTrace();
        }

    }
}

private void createLabel(WritableSheet sheet) throws WriteException {

    WritableFont times10pt = new WritableFont(WritableFont.TIMES, 10);
    times = new WritableCellFormat(times10pt);
    times.setWrap(true);

    WritableFont times10ptBoldUnderline = new WritableFont(WritableFont.TIMES, 10, WritableFont.BOLD, false,UnderlineStyle.SINGLE);
    timesBoldUnderline = new WritableCellFormat(times10ptBoldUnderline);
    timesBoldUnderline.setWrap(true);

    CellView cv = new CellView();
    cv.setFormat(times);
    cv.setFormat(timesBoldUnderline);
    cv.setAutosize(true);

    // Write a few headers
    addCaption(sheet, 0, 0, "Header 1");
    addCaption(sheet, 1, 0, "This is another header");

}

private void createContent(WritableSheet sheet) throws WriteException,
        RowsExceededException {

    for (int i = 1; i < 70000; i++) {
        addNumber(sheet, 0, i, i + 10);
        addNumber(sheet, 1, i, i * i);
    }
}

private void addCaption(WritableSheet sheet, int column, int row, String s)
        throws RowsExceededException, WriteException {
    Label label;
    label = new Label(column, row, s, timesBoldUnderline);
    sheet.addCell(label);
}

private void addNumber(WritableSheet sheet, int column, int row,
        Integer integer) throws WriteException, RowsExceededException {
    Number number;
    number = new Number(column, row, integer, times);
    sheet.addCell(number);
}

I am not sure how to add that logic here in my code.

Any suggestions will be of great help?

Or in any case, can anyone provide me a simple example in which if one sheet is full, it should start writing automatically into different sheet (Second sheet)?

like image 628
arsenal Avatar asked Oct 05 '22 09:10

arsenal


1 Answers

I made changes to the following 2 methods:-

private void writingToExcel(WritableWorkbook workbook) {
    try {
        // don't create a sheet now, instead, pass it in the workbook
        createContent(workbook);
    }
    catch (WriteException e) {
        e.printStackTrace();
    }
    finally {
        try {
            workbook.write();
            workbook.close();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        catch (WriteException e) {
            e.printStackTrace();
        }

    }
}

// instead of taking a sheet, take a workbook because we cannot ensure if the sheet can fit all the content at this point
private void createContent(WritableWorkbook workbook) throws WriteException {

    int excelSheetIndex = 0;
    int rowIndex = 0;
    WritableSheet excelSheet = null;

    for (int i = 1; i < 70000; i++) {

        // if the sheet has hit the cap, then create a new sheet, new label row and reset the row count
        if (excelSheet == null || excelSheet.getRows() == 65536) {
            excelSheet = workbook.createSheet("Report " + excelSheetIndex, excelSheetIndex++);
            createLabel(excelSheet);
            rowIndex = 0;
        }

        // instead of using i for row, use rowIndex
        addNumber(excelSheet, 0, rowIndex, i + 10);
        addNumber(excelSheet, 1, rowIndex, i * i);

        // increment the sheet row
        rowIndex++;
    }
}
like image 131
limc Avatar answered Oct 10 '22 03:10

limc