gpt4 book ai didi

c# - NPOI 真实世界枢轴示例

转载 作者:太空狗 更新时间:2023-10-29 23:13:09 29 4
gpt4 key购买 nike

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

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

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

我已经浪费了太多时间,所以我非常需要帮助:任何帮助将不胜感激......

问候 efaruk...

最佳答案

这是我使用 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,如您所问...Stream 是任何类型的 Stream 取决于你想如何使用它:在我的例子中 MemoryStream 创建 ASP.Net MVC Response 作为文件下载......

这里是自定义类型:

/// <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
}

问候...

关于c# - NPOI 真实世界枢轴示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38534401/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com