Java Apache POI-全部展开/全部折叠

Java Apache POI-全部展开/全部折叠,java,excel,apache-poi,pivot-table,Java,Excel,Apache Poi,Pivot Table,我现在正在使用ApachePOI从Java创建excel,我需要在导出的excel中展开/折叠所有内容。请找到下面的图片,我想之前和之后展开,下面的代码到目前为止,我尝试。(如果您正在尝试,它可能需要所需的jar)。有谁能帮忙吗 代码: package com.skumar.excel; 导入java.io.FileOutputStream; 导入java.util.Set; 导入java.util.TreeSet; 导入org.apache.poi.xssf.usermodel.XSSFC

我现在正在使用ApachePOI从Java创建excel,我需要在导出的excel中展开/折叠所有内容。请找到下面的图片,我想之前和之后展开,下面的代码到目前为止,我尝试。(如果您正在尝试,它可能需要所需的jar)。有谁能帮忙吗

代码:

package com.skumar.excel;
导入java.io.FileOutputStream;
导入java.util.Set;
导入java.util.TreeSet;
导入org.apache.poi.xssf.usermodel.XSSFCellStyle;
导入org.apache.poi.xssf.usermodel.xssfheet;
导入org.apache.poi.xssf.usermodel.xssf工作簿;
导入org.apache.poi.ss.usermodel.Cell;
导入org.apache.poi.ss.usermodel.CellStyle;
导入org.apache.poi.ss.usermodel.Font;
导入org.apache.poi.ss.usermodel.IndexedColors;
导入org.apache.poi.ss.usermodel.Row;
导入org.apache.poi.ss.usermodel.工作簿;
公共类ExcelGroupData{
公共静态void main(字符串[]args)
{
字符串excelFilename=null;
ExcelGroupData myExcel=新建ExcelGroupData();
/*如果(参数长度<1)
{
System.err.println(“用法:java”+myExcel.getClass().getName()+
“Excel_文件名”);
系统出口(1);
}
excelFilename=args[0].trim()*/
excelFilename=“C:\\aaa\\excel.xls”;
generateExcel(excelFilename);
}
public void generateExcel(字符串excelFilename){
试一试{
//新工作簿
工作簿wb=新XSSFWorkbook();
细胞c=null;
//标题行的单元格样式
CellStyle cs=wb.createCellStyle();
cs.setFillForegroundColor(IndexedColors.LIME.getIndex());
cs.setFillPattern(XSSFCellStyle.SOLID_前台);
Font f=wb.createFont();
f、 setBoldweight(字体为粗体,粗体);
f、 设置字体高度输入点((短)12);
cs.setFont(f);
//摘要行的单元格样式
CellStyle css=wb.createCellStyle();
f=wb.createFont();
f、 setBoldweight(字体为粗体,粗体);
f、 设置字体高度输入点((短)10);
css.setFont(f);
//新表
XSSFSheet sheet1=null;
sheet1=(XSSFSheet)wb.createSheet(“myData”);
//行和列索引
int-idx=0;
int-idy=0;
//生成列标题
Row Row=sheet1.createRow(idx);
c=行。创建单元(idy);
c、 setCellValue(“客户”);
c、 赛特风格(cs);
图1.设置柱宽(idy,10*500);
idy++;
c=行。创建单元(idy);
c、 setCellValue(“订单号”);
c、 赛特风格(cs);
图1.设置柱宽(idy,10*500);
idy++;
c=行。创建单元(idy);
c、 setCellValue(“订单总额”);
c、 赛特风格(cs);
图1.设置柱宽(idy,10*500);
idy++;
//下一行和重置列
idx=idx+1;
idy=0;//重置列
//第一组标题
int firstRow=idx+1;
row=sheet1.createRow(idx);
c=行。创建单元(idy);
c、 setCellValue(“ABC”);
idy++;
c=行。创建单元(idy);
c、 setCellValue(“101”);
idy++;
c=行。创建单元(idy);
c、 setCellValue(10.99);
idy++;
//第一组第一排
idx=idx+1;
idy=0;//重置列
row=sheet1.createRow(idx);
c=行。创建单元(idy);
c、 setCellValue(“ABC1”);
idy++;
c=行。创建单元(idy);
c、 setCellValue(“102”);
idy++;
c=行。创建单元(idy);
c、 setCellValue(22.23);
idy++;
//第一组第二排
idx=idx+1;
idy=0;//重置列
row=sheet1.createRow(idx);
c=行。创建单元(idy);
c、 setCellValue(“ABC2”);
idy++;
c=行。创建单元(idy);
c、 setCellValue(“105”);
idy++;
c=行。创建单元(idy);
c、 setCellValue(25.23);
idy++;
//第一组第三排
idx=idx+1;
idy=0;//重置列
int lastRow=idx+1;
row=sheet1.createRow(idx);
c=行。创建单元(idy);
c、 setCellValue(“ABC3”);
idy++;
c=行。创建单元(idy);
c、 setCellValue(“103”);
idy++;
c=行。创建单元(idy);
c、 setCellValue(100.33);
idy++;
//第二组:填充明细行数据
//第二组头球
idx=idx+1;
idy=0;//重置列
row=sheet1.createRow(idx);
c=行。创建单元(idy);
c、 setCellValue(“XYZ”);
idy++;
c=行。创建单元(idy);
c、 setCellValue(“103”);
idy++;
c=行。创建单元(idy);
c、 setCellValue(100.33);
idy++;
//第二组第一排
idx=idx+1;
idy=0;
row=sheet1.createRow(idx);
c=行。创建单元(idy);
c、 setCellValue(“XYZ1”);
idy++;
c=行。创建单元(idy);
c、 setCellValue(“103”);
idy++;
c=行。创建单元(idy);
c、 setCellValue(100.33);
idy++;
//第二组第二排
idx=idx+1;
idy=0;//重置列
row=sheet1.createRow(idx);
c=行。创建单元(idy);
c、 setCellValue(“XYZ2”);
idy++;
c=行。创建单元(idy);
c、 setCellValue(“103”);
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;
    }
}
        sheet1.groupRow(1, 4);
        sheet1.setRowGroupCollapsed(1, true);
        sheet1.groupRow(5, 8);
        sheet1.setRowGroupCollapsed(5, true);