首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >实现Excel的小计功能

实现Excel的小计功能
EN

Stack Overflow用户
提问于 2012-02-09 20:30:24
回答 3查看 3.2K关注 0票数 4

Excel在“Data -> Outline -> Subtotal”菜单中提供了“分类汇总”选项。它自动创建子和和折叠数据的可能性。下图演示了该操作如何转换工作表。

这正是我需要通过POI做的事情。我知道如何在单元格中设置一个小计函数,这样我就可以自己计算中间和。但是我如何在左边框上启用这种折叠呢?

我意识到有groupRow()方法,但这些嵌套的组并不像它们应该的那样工作。如果我使用下面的代码,我只得到两个组。一个大的(1-7)和(1-3)。组(5-7)丢失,更改呼叫顺序无效。

代码语言:javascript
复制
sheet.groupRow(1, 7);
sheet.groupRow(1, 3);
sheet.groupRow(5, 7);
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-02-09 20:55:46

我使用了一个相当老的POI版本,但我是这样做的:

我还需要多个嵌套组,所以我有一个用于存储缩进级别的行的模型(它是一个树,所以缩进是隐式的)。我和一个访问者一起遍历了模型,以获得组的开始行号和结束行号。然后为每个组调用HSSFSheet.groupRow。如果我没记错的话,群呼的顺序很重要。

票数 3
EN

Stack Overflow用户

发布于 2016-11-18 21:39:39

我想这就是你要找的:

http://www.mysamplecode.com/2011/10/apache-poi-excel-row-group-collapse.html

如果使用subtotal(9,<range>)而不是sum(<range>),则可以执行嵌套组,因为小计将忽略其范围内具有小计的单元格

票数 0
EN

Stack Overflow用户

发布于 2020-10-30 01:32:51

使用下面的库,你可以计算出你想要的小计

代码语言:javascript
复制
<dependency>
  <groupId>com.github.bld-commons.excel</groupId>
  <artifactId>generator-excel</artifactId>
  <version>3.1.1</version>
</dependency>

这个库是apache poi的包装器。

以下是源代码:

  1. 您可以创建一个表示表所在行的类。

代码语言:javascript
复制
package bld.generator.report.junit.entity;

import org.apache.poi.ss.usermodel.DataConsolidateFunction;
import org.apache.poi.ss.usermodel.HorizontalAlignment;

import bld.generator.report.excel.RowSheet;
import bld.generator.report.excel.annotation.ExcelCellLayout;
import bld.generator.report.excel.annotation.ExcelColumn;
import bld.generator.report.excel.annotation.ExcelFont;
import bld.generator.report.excel.annotation.ExcelSubtotal;
import bld.generator.report.excel.annotation.ExcelSubtotals;

@ExcelSubtotals(labelTotalGroup = "Total",endLabel = "total")
public class SalaryRow implements RowSheet {

    @ExcelColumn(columnName = "Name", indexColumn = 0)
    @ExcelCellLayout
    private String name;
    @ExcelColumn(columnName = "Amount", indexColumn = 1)
    @ExcelCellLayout(horizontalAlignment = HorizontalAlignment.RIGHT)
    @ExcelSubtotal(dataConsolidateFunction = DataConsolidateFunction.SUM,excelCellLayout = @ExcelCellLayout(horizontalAlignment = HorizontalAlignment.RIGHT,font=@ExcelFont(bold = true)))
    private Double amount;
    
    public SalaryRow() {
        super();
    }
    public SalaryRow(String name, Double amount) {
        super();
        this.name = name;
        this.amount = amount;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Double getAmount() {
        return amount;
    }
    public void setAmount(Double amount) {
        this.amount = amount;
    }
    
}

  1. 您可以创建一个表示工作表的类。

代码语言:javascript
复制
package bld.generator.report.junit.entity;

import javax.validation.constraints.Size;

import bld.generator.report.excel.SheetData;
import bld.generator.report.excel.annotation.ExcelHeaderLayout;
import bld.generator.report.excel.annotation.ExcelMarginSheet;
import bld.generator.report.excel.annotation.ExcelSheetLayout;
@ExcelSheetLayout
@ExcelHeaderLayout
@ExcelMarginSheet(bottom = 1.5,left = 1.5,right = 1.5,top = 1.5)
public class SalarySheet extends SheetData<SalaryRow> {

    public SalarySheet(@Size(max = 31) String sheetName) {
        super(sheetName);
    }

}

  1. 类测试

代码语言:javascript
复制
package bld.generator.report.junit;

import java.util.ArrayList;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import bld.generator.report.excel.BaseSheet;
import bld.generator.report.excel.GenerateExcel;
import bld.generator.report.excel.data.ReportExcel;
import bld.generator.report.junit.entity.SalaryRow;
import bld.generator.report.junit.entity.SalarySheet;
import bld.generator.report.utils.ExcelUtils;


@RunWith(SpringRunner.class)
@SpringBootTest
@ConfigurationProperties
@ComponentScan(basePackages = {"bld.generator","bld.read"})
@EnableTransactionManagement
public class SalaryTest {

    private static final String PATH_FILE = "/mnt/report/";

    @Autowired
    private GenerateExcel generateExcel;

    /**
     * Sets the up.
     *
     * @throws Exception the exception
     */
    @Before
    public void setUp() throws Exception {
    }

    @Test
    public void testSalary() throws Exception {
        List<BaseSheet> listBaseSheet = new ArrayList<>();
        SalarySheet salarySheet=new SalarySheet("salary");
        salarySheet.getListRowSheet().add(new SalaryRow("a",2.0));
        salarySheet.getListRowSheet().add(new SalaryRow("a",2.0));
        salarySheet.getListRowSheet().add(new SalaryRow("a",2.0));
        salarySheet.getListRowSheet().add(new SalaryRow("a",2.0));
        salarySheet.getListRowSheet().add(new SalaryRow("c",1.0));
        salarySheet.getListRowSheet().add(new SalaryRow("c",1.0));
        salarySheet.getListRowSheet().add(new SalaryRow("c",1.0));
        salarySheet.getListRowSheet().add(new SalaryRow("c",1.0));
        listBaseSheet.add(salarySheet);
        
        ReportExcel report=new ReportExcel("test", listBaseSheet);
        
        byte[] byteReport = this.generateExcel.createFileXlsx(report);
        ExcelUtils.writeToFile(PATH_FILE,report.getTitle(), ".xlsx", byteReport);
        
    }
    
    

}

在github上的项目链接下:

在结果下面。

enter image description here

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

https://stackoverflow.com/questions/9210861

复制
相关文章

相似问题

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