在数据透视表过滤器Epplus中选择项目

时间:2018-02-22 14:27:06

标签: c# excel pivot-table

我创建了一个类,它帮助我使用epplus在excel中创建一个数据透视表,过滤器将所有项目都选为默认值,但是我需要一些过滤器才能在过滤器中选择一个项目。 有没有办法选择过滤器中选择的项目? 或者只是确保只选择了1个项目并禁用多项选择,包括选择所有项目?

public class PivotTableFactory
{   
    private ExcelPivotTable _pivotTable;

    public void CreatePivotTable (Worksheet baseSheet, Worksheet targetSheet, string startRange, string tableName)
    {
        // Selecting All Range of the base Sheet
        String       baseAddress  = "A1:" + baseSheet.EPPlusSheet.Dimension.End.Address;
        ExcelRange   baseRange    = baseSheet.EPPlusSheet.Cells[baseAddress];
        ExcelAddress startAddress = targetSheet.EPPlusSheet.Cells[startRange];

        // Creating Pivot Table
        _pivotTable = targetSheet.EPPlusSheet.PivotTables.Add (startAddress, baseRange, tableName);
        _pivotTable.UseAutoFormatting = false;
    }

    public void AddFilterField(string columnName, string fieldName, eSortType sortType = eSortType.None)
    {
        ExcelPivotTableField dataField = _pivotTable.PageFields.Add (_pivotTable.Fields[columnName]);
        dataField.Name                 = fieldName;
        dataField.Sort                 = sortType;
    }

    public void AddRowField(string columnName, string fieldName, eSortType sortType = eSortType.None)
    {
        ExcelPivotTableField rowField = _pivotTable.RowFields.Add (_pivotTable.Fields[columnName]);
        rowField.Name                 = fieldName;
        rowField.Sort                 = sortType;
    }

    public void AddColumnField (string columnName, string fieldName, eSortType sortType = eSortType.None)
    {
        ExcelPivotTableField rowField = _pivotTable.ColumnFields.Add (_pivotTable.Fields[columnName]);
        rowField.Name                 = fieldName;
        rowField.Sort                 = sortType;
    }

    public void AddValueField (string columnName, string fieldName, DataFieldFunctions func = DataFieldFunctions.Sum, string format = "")
    {
        ExcelPivotTableDataField dataField = _pivotTable.DataFields.Add (_pivotTable.Fields[columnName]);
        dataField.Name                     = fieldName;            
        dataField.Function                 = func;
        dataField.Format                   = format;            
    }
}

0 个答案:

没有答案