4

Excelは、メニューData-> Outline->から小計オプションを提供しますSubtotal。サブサムとデータを折りたたむ可能性を自動的に作成します。次の画像は、アクションがシートをどのように変換するかを示しています。

ここに画像の説明を入力してください

そして、これはまさに私がPOIを介して行う必要があることです。小計関数をセルに設定して、自分で中間合計を計算できるようにする方法を知っています。しかし、どうすれば左の境界でこの折りたたみを有効にできますか?

メソッドがあることに気づきましたがgroupRow()、それらのネストされたグループは期待どおりに機能しません。次のコードを使用すると、2つのグループしか取得できません。1つの大きな(1-7)と(1-3)。グループ(5-7)が欠落しており、呼び出しの順序を変更しても効果はありません。

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

3 に答える 3

3

私はかなり古いバージョンのPOIを使用していますが、これが私が行った方法です。
複数のネストされたグループも必要だったので、インデントレベルも格納されている行のモデルがありました(ツリーであるため、インデントは暗黙的でした)。グループの開始行と終了行の番号を取得するために、訪問者と一緒にモデルをトラバースしました。その後、各グループに対してHSSFSheet.groupRowを呼び出します。私の記憶が正しければ、グループ呼び出しの順序が重要です。

于 2012-02-09T12:55:45.957 に答える
0

私はこれがまさにあなたが探しているものだと思います:

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

subtotal(9,<range>)の代わりにを使用するとsum(<range>)、小計がその範囲内の小計を持つセルを無視するため、ネストされたグループを実行できます

于 2016-11-18T13:39:38.887 に答える
0

次のライブラリを使用すると、必要な小計を計算できます

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

このライブラリはapachepoiのラッパーです。
ソースコードの下:

  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のプロジェクトのリンクの下:

結果の下。
ここに画像の説明を入力してください

于 2020-10-29T17:32:50.600 に答える