xmlns:dxpg="http://schemas.devexpress.com/winfx/2008/xaml/pivotgrid"
页面:
<StackPanel Grid.Row="0" Margin="420,22,0,23" Width="200" Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Left">
<WrapPanel x:Name="wrapPanelArea" Width="60" Height="20" Cursor="Hand" >
<RadioButton x:Name="radButArea" Content="面积" GroupName="TJType" IsChecked="True" Checked="radButArea_Checked"/>
</WrapPanel>
<WrapPanel x:Name="wrapPanelNumber" Width="60" Height="20" Cursor="Hand" >
<RadioButton x:Name="radButNumber" Content="个数" GroupName="TJType" Checked="radButNumber_Checked"/>
</WrapPanel>
</StackPanel>
<CustomControl:KImgButton Width="90" Height="30" CornerRadius="3" Content="导出" Foreground="White" HorizontalAlignment="Right" Margin="0,18,0,17" Click="KImgButton_Click"/>
<dxpg:PivotGridControl x:Name="pivotGrid" FieldValueDisplayText="PivotGridControl2_FieldValueDisplayText"
RowTreeWidth="130"
ShowFilterHeaders="False" ChartSelectionOnly="False" ChartProvideDataByColumns ="True" ShowRowHeaders="False" ShowColumnHeaders="False" ShowDataHeaders="False"
ChartShowColumnGrandTotals="False" ChartShowRowGrandTotals="False">
<dxpg:PivotGridControl.Fields>
<dxpg:PivotGridField x:Name="fieldProductName" FieldName="ProductName" Area="RowArea" AreaIndex="0"
Caption="行字段标题" Width="155" />
<dxpg:PivotGridField x:Name="fieldExtendedPrice" FieldName="Extended Price" Caption="统计值字段" Area="DataArea" AreaIndex="0"
CellFormat="c" />
<dxpg:PivotGridField x:Name="fieldOrderDate" FieldName="OrderDate" UnboundFieldName="fieldOrderDate"
Area="ColumnArea" AreaIndex="0"
GroupInterval="DateMonth" Caption="列字段标题" />
</dxpg:PivotGridControl.Fields>
</dxpg:PivotGridControl>
数据绑定;
public void SetDataSource(DataTable dt, string f_Value, string f_Count, string f_Col, string f_Row)
{
this.pivotGrid.DataSource = null;
this.pivotGrid.Fields.Clear();
this.pivotGrid.DataSource = dt;//数据源
this.pivotGrid.Fields.Add(f_Row, DevExpress.Xpf.PivotGrid.FieldArea.RowArea);//横向列名
this.pivotGrid.Fields.Add(f_Col, DevExpress.Xpf.PivotGrid.FieldArea.ColumnArea);//纵向列名
F_Value = f_Value;// 根据 F_Value 分类统计
F_Count = f_Count;// 根据 F_Count 分类统计(用于多个分类统计时,当数据源不变时)
if (this.radButArea.IsChecked == true)
{
this.pivotGrid.Fields.Add(f_Value, DevExpress.Xpf.PivotGrid.FieldArea.DataArea);
}
else
{
this.pivotGrid.Fields.Add(f_Col, DevExpress.Xpf.PivotGrid.FieldArea.DataArea);
}
}
//修改 列名
private void PivotGridControl2_FieldValueDisplayText(object sender, DevExpress.Xpf.PivotGrid.PivotFieldDisplayTextEventArgs e)
{
if(e.ValueType==DevExpress.Xpf.PivotGrid.FieldValueType.GrandTotal)//总计
{
if (e.IsColumn && e.DisplayText.Trim() == "Grand Total")//第一层列总计标题
{
e.DisplayText = "总计";
}
else if (e.IsColumn)//其他层列总计标题
{
e.DisplayText = e.DisplayText + "总计";
}
else if (e.IsColumn == false && e.DisplayText.Trim() == "Grand Total")//第一层行总计标题
{
e.DisplayText = "总计";
}
else
{
e.DisplayText = "";
}
}
}
private void radButArea_Checked(object sender, RoutedEventArgs e)
{
if (this.pivotGrid == null) return;
if (this.radButArea.IsChecked == true)
{
this.pivotGrid.Fields[2].FieldName = F_Value;
}
else
{
this.pivotGrid.Fields[2].FieldName = F_Count;
}
}
private void radButNumber_Checked(object sender, RoutedEventArgs e)
{
if (this.pivotGrid == null) return;
if (this.radButArea.IsChecked == true)
{
this.pivotGrid.Fields[2].FieldName = F_Value;
}
else
{
this.pivotGrid.Fields[2].FieldName = F_Count;
}
}
//导出
private void KImgButton_Click(object sender, RoutedEventArgs e)
{
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.FileName = "Document"; // Default file name
dlg.DefaultExt = ".xlsx"; // Default file extension
dlg.Filter = "excel documents (.xlsx)|*.xlsx"; // Filter files by extension
// Show save file dialog box
Nullable<bool> result = dlg.ShowDialog();
// Process save file dialog box results
if (result == true)
{
// Save document
string filename = dlg.FileName;
this.pivotGrid.ExportToXlsx(filename);
}
}