java 在java中在excel上创建多个工作表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27124733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:16:49  来源:igfitidea点击:

create multiple sheets on excel in java

javaexcel

提问by Shahrin

I'm trying to create two different sheets on a single excel file . But only getting the first sheet.How can I get the second sheet ? where did I mistake ? Please help

我正在尝试在一个 excel 文件上创建两个不同的工作表。但只得到第一张纸。我怎样才能得到第二张纸?我哪里弄错了?请帮忙

Below is the code :

下面是代码:

        WorkbookSettings ws = new WorkbookSettings();
        ws.setLocale(new Locale("en", "EN"));
        WritableWorkbook workbook = Workbook.createWorkbook(new File(filename), ws);
        WritableSheet s = workbook.createSheet("Summary", 0);

        // set font ,border,alignment
        cf.setWrap(true);

        Label l = null;
        l = new Label(0, 0, "Ticket Product", cf);
        s.addCell(l);
        ............................................
        ............................................
        // code to load data on the first sheet


        int columns = s.getColumns();
        for (i = 0; i < columns; i++) 
        {
            //write on excel columnwise
        }
        workbook.write();

        s = workbook.createSheet("Details", 1);

        // set font ,border,alignment
        cf.setWrap(true);

        l = null;
        l = new Label(0, 0, "Ticket Product", cf);
        s.addCell(l);
        ............................................
        ............................................
        //code to load data on the second sheet

        columns = s.getColumns();
        for (i = 0; i < columns; i++) 
        {
          //the same loop as before to write columnwise
        }

        workbook.write();
        workbook.close();

回答by Mahaveer Jangir

You have to use workbook.setSheetOrder(sheetName,pos), and you can have the logic to increment the posvariable as required.

您必须使用workbook.setSheetOrder(sheetName,pos),并且您可以拥有根据需要增加pos变量的逻辑。

回答by Hard Developer

    WorkbookSettings ws = new WorkbookSettings();
    ws.setLocale(new Locale("en", "EN"));
    WritableWorkbook workbook = Workbook.createWorkbook(new File(filename), ws);
    WritableSheet sheet1 = workbook.createSheet("Summary", 0);//Create the first sheet
    WritableSheet sheet2 = workbook.createSheet("Details", 1);//Create the second sheet
    // set font ,border,alignment
    cf.setWrap(true);

    Label l = null;
    l = new Label(0, 0, "Ticket Product", cf);
    sheet1.addCell(l);
    ............................................
    ............................................
    // code to load data on the first sheet


    int columns = sheet1.getColumns();
    for (i = 0; i < columns; i++) 
    {
        //write on excel columnwise in sheet 1
    }

   int columnsheet2 = sheet2.getColumns();
    for (i = 0; i < columns; i++) 
    {
        //write on excel columnwise in sheet 2
    }
    workbook.write();

    s = workbook.createSheet("Details", 1);
    //here is the mistake you are using the same 's' object.

    // set font ,border,alignment
    cf.setWrap(true);`enter code here`

    l = null;
    l = new Label(0, 0, "Ticket Product", cf);
    sheet1.addCell(l);
    ............................................
    ............................................
    //code to load data on the second sheet

    columns = s.getColumns();
    for (i = 0; i < columns; i++) 
    {
      //the same loop as before to write columnwise
    }

    workbook.write();
    workbook.close();