实现Excel的小计功能

时间:2012-02-09 12:30:24

标签: java excel apache-poi folding subtotal

Excel提供菜单Data - >中的小计选项。 Outline - > Subtotal。它会自动创建子和以及折叠数据的可能性。下图显示了操作如何转换工作表。

enter image description here

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

我意识到有groupRow()方法,但那些嵌套组不能正常工作。如果我使用以下代码,我只会得到两个组。一大(1-7)和(1-3)。组(5-7)丢失,更改呼叫顺序无效。

sheet.groupRow(1, 7);
sheet.groupRow(1, 3);
sheet.groupRow(5, 7);

3 个答案:

答案 0 :(得分:3)

我使用了相当古老的POI版本,但这就是我的做法:
我还需要多个嵌套组,所以我有一个存储缩进级别的行的模型(它是一个树,因此缩进是隐式的)。我和访问者遍历了模型以获取组的开始和结束行号。然后为每个组调用HSSFSheet.groupRow。如果我没记错的话,小组电话的顺序很重要。

答案 1 :(得分:0)

我认为这正是您所寻找的:

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

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

答案 2 :(得分:0)

使用以下库,您可以计算所需的小计

<dependency>
  <groupId>com.github.bld-commons.excel</groupId>
  <artifactId>generator-excel</artifactId>
  <version>3.1.1</version>
</dependency>

此库是apache poi的包装。
在源代码下方:

  1. 您创建一个代表表行的类。
  2. 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;
        }
        
    }
    
  3. 您创建一个代表工作表的类。
  4. 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);
        }
    
    }
    
  5. 班级考试
  6. 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