java – 在jExcel API中创建多个工作表

前端之家收集整理的这篇文章主要介绍了java – 在jExcel API中创建多个工作表前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

如果一个工作表已满(65536行),我需要使用jExcel API在Java中创建多个Excel工作表.假设如果一张纸已满,则在下一张纸中,它应该从第一张纸中停止的位置开始自动写入.当一张纸已满时,我只是坚持使用逻辑来动态创建它.下面是我到目前为止所做的代码.

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,"Header 1");
    addCaption(sheet,1,"This is another header");

}

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

    for (int i = 1; i < 70000; i++) {
        addNumber(sheet,i,i + 10);
        addNumber(sheet,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,Integer integer) throws WriteException,RowsExceededException {
    Number number;
    number = new Number(column,integer,times);
    sheet.addCell(number);
}

我不知道如何在我的代码添加该逻辑.

任何建议都会有很大帮助吗?

或者在任何情况下,任何人都可以给我一个简单的例子,如果一张纸已满,它应该开始自动写入不同的纸张(第二张)?

最佳答案
我对以下两种方法进行了更改: –

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,rowIndex,i + 10);
        addNumber(excelSheet,i * i);

        // increment the sheet row
        rowIndex++;
    }
}

猜你在找的Java相关文章