【问题标题】:Apache POI - Expand All / Collapse AllApache POI - 全部展开/全部折叠
【发布时间】:2016-12-14 17:54:12
【问题描述】:

我现在正在使用 Apache POI 从 Java 创建一个 excel,我需要在导出的 excel 中展开/折叠全部。请在展开之前和之后找到我想要的以下图像,以及到目前为止我尝试过的以下代码。 (如果您最终尝试,它可能需要所需的 jar)。任何人都可以在这方面提供帮助。

代码:

package com.skumar.excel;

import java.io.FileOutputStream;
import java.util.Set;
import java.util.TreeSet;

import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;

public class ExcelGroupData {


    public static void main(String[] args)
    {

        String excelFilename = null;

        ExcelGroupData myExcel = new ExcelGroupData();
       /* if (args.length < 1)
        {
            System.err.println("Usage: java "+ myExcel.getClass().getName()+
            " Excel_Filename");
            System.exit(1);
        }

        excelFilename = args[0].trim();*/
        excelFilename = "C:\\aaa\\excel.xls";
        myExcel.generateExcel(excelFilename);

    }

    public void  generateExcel(String excelFilename){

        try {

            //New Workbook
            Workbook wb = new XSSFWorkbook();

            Cell c = null;

            //Cell style for header row
            CellStyle cs = wb.createCellStyle();
            cs.setFillForegroundColor(IndexedColors.LIME.getIndex());
            cs.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
            Font f = wb.createFont();
            f.setBoldweight(Font.BOLDWEIGHT_BOLD);
            f.setFontHeightInPoints((short) 12);
            cs.setFont(f);

            //Cell style for summary row
            CellStyle css = wb.createCellStyle();
            f = wb.createFont();
            f.setBoldweight(Font.BOLDWEIGHT_BOLD);
            f.setFontHeightInPoints((short) 10);
            css.setFont(f);

            //New Sheet
            XSSFSheet sheet1 = null;
            sheet1 = (XSSFSheet) wb.createSheet("myData");

            // Row and column indexes
            int idx = 0;
            int idy = 0;

            //Generate column headings
            Row row = sheet1.createRow(idx);
            c = row.createCell(idy);
            c.setCellValue("Customer");
            c.setCellStyle(cs);
            sheet1.setColumnWidth(idy, 10 * 500);
            idy++;
            c = row.createCell(idy);
            c.setCellValue("Order Number");
            c.setCellStyle(cs);
            sheet1.setColumnWidth(idy, 10 * 500);
            idy++;
            c = row.createCell(idy);
            c.setCellValue("Order Total");
            c.setCellStyle(cs);
            sheet1.setColumnWidth(idy, 10 * 500);
            idy++;



            //Next row and reset column 
            idx = idx + 1;
            idy = 0; // reset column 



            //Header of First Group
            int firstRow = idx + 1;
            row = sheet1.createRow(idx);
            c = row.createCell(idy);
            c.setCellValue("ABC");
            idy++;
            c = row.createCell(idy);
            c.setCellValue("101");
            idy++;
            c = row.createCell(idy);
            c.setCellValue(10.99);
            idy++;

            //1st row of 1st Group
            idx = idx + 1; 
            idy = 0; // reset column 
            row = sheet1.createRow(idx);
            c = row.createCell(idy);
            c.setCellValue("ABC1");
            idy++;
            c = row.createCell(idy);
            c.setCellValue("102");
            idy++;
            c = row.createCell(idy);
            c.setCellValue(22.23);
            idy++;

            //2nd row of 1st Group
            idx = idx + 1;
            idy = 0; // reset column 
            row = sheet1.createRow(idx);
            c = row.createCell(idy);
            c.setCellValue("ABC2");
            idy++;
            c = row.createCell(idy);
            c.setCellValue("105");
            idy++;
            c = row.createCell(idy);
            c.setCellValue(25.23);
            idy++;

            //3rd row of 1st Group 
            idx = idx + 1;
            idy = 0; // reset column 
            int lastRow = idx + 1;
            row = sheet1.createRow(idx);
            c = row.createCell(idy);
            c.setCellValue("ABC3");
            idy++;
            c = row.createCell(idy);
            c.setCellValue("103");
            idy++;
            c = row.createCell(idy);
            c.setCellValue(100.33);
            idy++;

            // Second Group: Populate detail row data

            //Header of 2nd Group
            idx = idx + 1;
            idy = 0; // reset column 
            row = sheet1.createRow(idx);
            c = row.createCell(idy);
            c.setCellValue("XYZ");
            idy++;
            c = row.createCell(idy);
            c.setCellValue("103");
            idy++;
            c = row.createCell(idy);
            c.setCellValue(100.33);
            idy++;

            //1st row of 2nd Group
            idx = idx + 1;
            idy = 0;
            row = sheet1.createRow(idx);
            c = row.createCell(idy);
            c.setCellValue("XYZ1");
            idy++;
            c = row.createCell(idy);
            c.setCellValue("103");
            idy++;
            c = row.createCell(idy);
            c.setCellValue(100.33);
            idy++;

            //2nd row of 2nd Group 
            idx = idx + 1;
            idy = 0; // reset column 
            row = sheet1.createRow(idx);
            c = row.createCell(idy);
            c.setCellValue("XYZ2");
            idy++;
            c = row.createCell(idy);
            c.setCellValue("103");
            idy++;
            c = row.createCell(idy);
            c.setCellValue(100.33);
            idy++;

            //3rd row of 2nd Group
            idx = idx + 1;
            idy = 0; // reset column 
            int lastOfSecondGroup = idx+1;
            row = sheet1.createRow(idx);
            c = row.createCell(idy);
            c.setCellValue("XYZ3");
            idy++;
            c = row.createCell(idy);
            c.setCellValue("103");
            idy++;
            c = row.createCell(idy);
            c.setCellValue(100.33);
            idy++;
            System.out.println(firstRow + "   " + lastRow);
            //Group the Rows together
            sheet1.groupRow(1, lastOfSecondGroup-1);
            sheet1.setRowGroupCollapsed(firstRow, true);
            sheet1.setRowSumsBelow(false);

            FileOutputStream fileOut = new FileOutputStream(excelFilename.trim());

            wb.write(fileOut);
            fileOut.close();

        }
        catch (Exception e) {
            System.out.println(e);
        }

    }

    private String getColumnName(int columnNumber) {

        String columnName = "";
        int dividend = columnNumber + 1;
        int modulus;

        while (dividend > 0){
            modulus = (dividend - 1) % 26;
            columnName = (char)(65 + modulus) + columnName;
            dividend = (int)((dividend - modulus) / 26);
        } 
        return columnName;
    }
}

【问题讨论】:

    标签: java excel apache-poi pivot-table


    【解决方案1】:

    根据第一张图片,您无法通过创建单个组来实现此结果。您需要两组 - 一组从第 2 行到第 5 行,第二组从第 6 行到第 9 行

    代码应该是 -

            sheet1.groupRow(1, 4);
            sheet1.setRowGroupCollapsed(1, true);
            sheet1.groupRow(5, 8);
            sheet1.setRowGroupCollapsed(5, true);
    

    我认为不是这些硬编码的行,而是一些分组逻辑。根据逻辑,您可以指定分组的起始行和结束行

    【讨论】:

    • 是的,我已经有了。但是我只寻找一个展开/折叠图标。我认为如果我们可旋转,它是可行的。我没有得到一个很好的例子。不过非常感谢你的回复
    【解决方案2】:

    我发现当外部组折叠时内部组会自动展开的问题。这里是类似的discussion

    对我来说可靠工作的 sn-p(我认为应该被视为解决方法)

    ((XSSFRow) row).getCTRow().setCollapsed(true);
    sheet.setRowGroupCollapsed(row.getRowNum() + 1, true);
    

    【讨论】:

      猜你喜欢
      • 2011-04-21
      • 1970-01-01
      • 2014-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多