NPOI真实世界枢纽示例

时间:2016-07-22 19:58:32

标签: c# excel pivot-table epplus npoi

我搜索了很多,但我最后在这里问这个问题。我的问题没有令人满意的答案:我想创建一个excel枢轴(WorkSheet1作为数据透视表,WorkSheet2作为数据源)我从DataTable创建DataSource没有任何问题。但是没有用于添加分层列和分层行和度量的指导文档,它们映射回DataSource(工作表2)。我很确定映射是通过以下方式完成的:

var pt = pivotSheet.CreatePivotTable(new AreaReference(new CellReference("A2"), new CellReference(rowCount, columnCount -1)), new CellReference("A1"), dataSheet);

但是没有证据表明如何映射分层列和分层行和度量。

我已经浪费了太多时间,所以我非常需要帮助:任何帮助都将受到赞赏......

关心efaruk ......

1 个答案:

答案 0 :(得分:4)

这是我使用EPPlus [*****]

的解决方案
public static class ExcelPivotHelper
{
    public static void CreateExcelPivot(Stream outputStream, DataTable data, PivotSettings settings)
    {
        using (var pckg = new ExcelPackage())
        {
            var book = pckg.Workbook;
            var pivotSheet = book.Worksheets.Add("Pivot");
            var dataSheet = book.Worksheets.Add("Data");
            var dataRange = GenerateDataSheet(data, dataSheet);
            GeneratePivotSheet(pivotSheet, dataRange, settings);
            pckg.SaveAs(outputStream);
        }
    }

    private static void GeneratePivotSheet(ExcelWorksheet pivotSheet, ExcelRangeBase dataRange, PivotSettings settings)
    {
        //var rowCount = data.Rows.Count;
        //var columnCount = data.Columns.Count;
        var pt = pivotSheet.PivotTables.Add(pivotSheet.Cells["A1"], dataRange, "PivotTable");

        pt.MultipleFieldFilters = true;
        pt.RowGrandTotals = true;
        pt.ColumGrandTotals = true;
        pt.Compact = true;
        pt.CompactData = true;
        pt.GridDropZones = false;
        pt.Outline = false;
        pt.OutlineData = false;
        pt.ShowError = true;
        pt.ErrorCaption = "[error]";
        pt.ShowHeaders = true;
        pt.UseAutoFormatting = true;
        pt.ApplyWidthHeightFormats = true;
        pt.ShowDrill = true;
        pt.DataOnRows = false;

        pt.FirstHeaderRow = 1;  // first row has headers
        pt.FirstDataCol = 1;    // first col of data
        pt.FirstDataRow = 2;    // first row of data

        pt.TableStyle = TableStyles.Medium6;

        //pt.ColumGrandTotals = true;
        //pt.RowGrandTotals = true;
        //pt.GrandTotalCaption = "Genel Toplam";

        // pt.RowHeaderCaption = "";
        foreach (var column in settings.Columns)
        {
            var field = pt.Fields[column.Field];
            var pivotField = pt.ColumnFields.Add(field);
            if (column.PivotDataType == PivotDataType.Date)
            {
                //pivotField.AddDateGrouping(eDateGroupBy.Days);
            }
        }

        foreach (var row in settings.Rows)
        {
            var field = pt.Fields[row.Field];
            var pivotField = pt.RowFields.Add(field);
            if (row.PivotDataType == PivotDataType.Date)
            {
                //pivotField.AddDateGrouping(eDateGroupBy.Days);
            }
        }

        foreach (var measurement in settings.Measurements)
        {
            var field = pt.Fields[measurement.Field];
            var pivotField = pt.DataFields.Add(field);
            pivotField.Format = measurement.DisplayFormat;
            pivotField.Function = DataFieldFunctions.Sum;
            pivotField.Name = measurement.Caption;
        }

    }

    private static readonly Type[] NumericTypes = { typeof(decimal), typeof(double), typeof(float), typeof(int), typeof(long), typeof(short), typeof(byte) };
    private static ExcelRangeBase GenerateDataSheet(DataTable data, ExcelWorksheet dataSheet)
    {
        dataSheet.Hidden = eWorkSheetHidden.VeryHidden;
        var range = dataSheet.Cells["A1"].LoadFromDataTable(data, true, TableStyles.Medium6);
        range.AutoFitColumns();
        for (var i = 0; i < data.Columns.Count; i++)
        {
            var ix = i + 1;
            var cell = dataSheet.Cells[2, ix, data.Rows.Count + 1, ix];
            var column = data.Columns[i];
            if (NumericTypes.Contains(column.DataType))
            {
                cell.Style.Numberformat.Format = "#,##0";
            }
            else if (column.DataType == typeof(DateTime))
            {
                cell.Style.Numberformat.Format = "dd.MM.yyyy";
            }
        }

        return range;
    }
}


public class PivotSettings
{
    private string _generalTotalText = "Genel Toplam";
    private string _yesText = "Evet";
    private string _noText = "Hayır";
    private string _fileExtension = ".xlsx";

    /// <summary>
    ///     Default ctor
    /// </summary>
    public PivotSettings()
    {
        Columns = new List<PivotItemSetting>(10);
        Rows = new List<PivotItemSetting>(10);
        Measurements = new List<MeasureSetting>(10);
    }

    /// <summary>
    /// Column Item Settings
    /// </summary>
    public List<PivotItemSetting> Columns { get; set; }

    /// <summary>
    /// Row Item Settings
    /// </summary>
    public List<PivotItemSetting> Rows { get; set; }

    /// <summary>
    /// Measure Settings
    /// </summary>
    public List<MeasureSetting> Measurements { get; set; }

    /// <summary>
    ///     Display text for 'Genel Toplam' word. Default is 'Genel Toplam'
    /// </summary>
    public string GeneralTotalText
    {
        get { return _generalTotalText; }
        set { _generalTotalText = value; }
    }

    /// <summary>
    ///     Display text for 'Evet' word. Default is 'Evet'
    /// </summary>
    public string YesText
    {
        get { return _yesText; }
        set { _yesText = value; }
    }

    /// <summary>
    ///     Display text for 'Hayır' word. Default is 'Hayır'
    /// </summary>
    public string NoText
    {
        get { return _noText; }
        set { _noText = value; }
    }

    /// <summary>
    ///     Excel File Extension
    /// </summary>
    public string FileExtension
    {
        get { return _fileExtension; }
        set { _fileExtension = value; }
    }
}


/// <summary>
///     Setting for Pivot Column and Row
/// </summary>
public class PivotItemSetting
{
    private string _displayFormat = "{0}";

    /// <summary>
    ///     Field Name
    /// </summary>
    public string Field { get; set; }

    /// <summary>
    ///     Desired data type of the field
    /// </summary>
    public PivotDataType PivotDataType { get; set; }

    /// <summary>
    ///     Desired display format string for field
    /// </summary>
    public string DisplayFormat
    {
        get { return _displayFormat; }
        set { _displayFormat = value; }
    }

    /// <summary>
    ///     Automatically filled by generation process. Don't fill manually.
    /// </summary>
    public int CalculatedItemCount { get; set; }
}

/// <summary>
///     Pivot Measure Setting
/// </summary>
public class MeasureSetting
{
    private string _displayFormat = "#,##0";

    /// <summary>
    ///     Field Name
    /// </summary>
    public string Field { get; set; }

    /// <summary>
    ///     Measure Caption
    /// </summary>
    public string Caption { get; set; }

    /// <summary>
    ///     Type of Measure
    /// </summary>
    public MeasureType MeasureType { get; set; }

    /// <summary>
    ///     Desired display format string for measure
    /// </summary>
    public string DisplayFormat
    {
        get { return _displayFormat; }
        set { _displayFormat = value; }
    }
}

/// <summary>
///     Masure type
/// </summary>
public enum MeasureType
{
    /// <summary>
    ///     Sum of the values for measure field
    /// </summary>
    Sum
}

<强>更新

当您质疑时,是的DataTable是正常的DataTable ... 流是任何类型的流取决于你想如何使用它:在我的情况下,MemoryStream创建ASP.Net MVC响应为文件下载...

以下是自定义类型:

/// <summary>
///     Settings for Pivot Table
/// </summary>
public class PivotSettings
{
    private string _generalTotalText = "Genel Toplam";
    private string _yesText = "Evet";
    private string _noText = "Hayır";
    private string _fileExtension = ".xlsx";

    /// <summary>
    ///     Default ctor
    /// </summary>
    public PivotSettings()
    {
        Columns = new List<PivotItemSetting>(10);
        Rows = new List<PivotItemSetting>(10);
        Measurements = new List<MeasureSetting>(10);
    }

    /// <summary>
    /// Column Item Settings
    /// </summary>
    public List<PivotItemSetting> Columns { get; set; }

    /// <summary>
    /// Row Item Settings
    /// </summary>
    public List<PivotItemSetting> Rows { get; set; }

    /// <summary>
    /// Measure Settings
    /// </summary>
    public List<MeasureSetting> Measurements { get; set; }

    /// <summary>
    ///     Display text for 'Genel Toplam' word. Default is 'Genel Toplam'
    /// </summary>
    public string GeneralTotalText
    {
        get { return _generalTotalText; }
        set { _generalTotalText = value; }
    }

    /// <summary>
    ///     Display text for 'Evet' word. Default is 'Evet'
    /// </summary>
    public string YesText
    {
        get { return _yesText; }
        set { _yesText = value; }
    }

    /// <summary>
    ///     Display text for 'Hayır' word. Default is 'Hayır'
    /// </summary>
    public string NoText
    {
        get { return _noText; }
        set { _noText = value; }
    }

    /// <summary>
    ///     Excel File Extension
    /// </summary>
    public string FileExtension
    {
        get { return _fileExtension; }
        set { _fileExtension = value; }
    }
}

/// <summary>
///     Setting for Pivot Column and Row
/// </summary>
public class PivotItemSetting
{
    private string _displayFormat = "{0}";

    /// <summary>
    ///     Field Name
    /// </summary>
    public string Field { get; set; }

    /// <summary>
    ///     Desired data type of the field
    /// </summary>
    public PivotDataType PivotDataType { get; set; }

    /// <summary>
    ///     Desired display format string for field
    /// </summary>
    public string DisplayFormat
    {
        get { return _displayFormat; }
        set { _displayFormat = value; }
    }

    /// <summary>
    ///     Automatically filled by generation process. Don't fill manually.
    /// </summary>
    public int CalculatedItemCount { get; set; }
}

/// <summary>
///     Pivot Measure Setting
/// </summary>
public class MeasureSetting
{
    private string _displayFormat = "#,##0";

    /// <summary>
    ///     Field Name
    /// </summary>
    public string Field { get; set; }

    /// <summary>
    ///     Measure Caption
    /// </summary>
    public string Caption { get; set; }

    /// <summary>
    ///     Type of Measure
    /// </summary>
    public MeasureType MeasureType { get; set; }

    /// <summary>
    ///     Desired display format string for measure
    /// </summary>
    public string DisplayFormat
    {
        get { return _displayFormat; }
        set { _displayFormat = value; }
    }
}

/// <summary>
///     Masure type
/// </summary>
public enum MeasureType
{
    /// <summary>
    ///     Sum of the values for measure field
    /// </summary>
    Sum
}

/// <summary>
///     Pivot Item type 
/// </summary>
public enum PivotDataType
{
    /// <summary>
    ///     Not Defined, Default
    /// </summary>
    None,
    /// <summary>
    ///     String
    /// </summary>
    String,
    /// <summary>
    /// Date Part of DateTime
    /// </summary>
    Date,
    /// <summary>
    /// DateTime
    /// </summary>
    DateTime,
    /// <summary>
    /// Numeric
    /// </summary>
    Numeric,
    /// <summary>
    /// Boolean
    /// </summary>
    Boolean
}

...问候