首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在java中的excel上创建多张工作表

在java中的excel上创建多张工作表
EN

Stack Overflow用户
提问于 2014-11-25 18:53:25
回答 2查看 17.8K关注 0票数 1

我正在尝试在一个excel文件上创建两个不同的工作表。但是只有得到第一个sheet.How,我才能得到第二张表吗?我哪里弄错了?请帮帮忙

代码如下:

代码语言:javascript
复制
        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();
EN

回答 2

Stack Overflow用户

发布于 2014-11-25 19:03:34

代码语言:javascript
复制
    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();
票数 1
EN

Stack Overflow用户

发布于 2017-01-31 16:46:24

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27124733

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档