This repository has been archived by the owner on Oct 16, 2023. It is now read-only.
/
Form1.cs
71 lines (64 loc) · 2.38 KB
/
Form1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using DevExpress.XtraPivotGrid;
using System;
using System.Drawing;
using System.Windows.Forms;
namespace HowToExportSelectedCells
{
public partial class Form1 : DevExpress.XtraEditors.XtraForm
{
string fileName = "Book1.xlsx";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
excelDataSource1.FileName = "SalesPerson.xlsx";
excelDataSource1.Fill();
}
private void button1_Click(object sender, EventArgs e)
{
PivotGridControl pivot = pivotGridControl1 ;
if (pivot.Cells.Selection.IsEmpty)
{
pivotGridControl1.ExportToXlsx(fileName);
}
else
{
Rectangle selection = pivot.Cells.Selection;
Point fCell = pivot.Cells.FocusedCell;
pivot.Tag = selection ;
pivot.CustomFieldValueCells += pivotGridControl1_CustomFieldValueCells;
try
{
pivot.RefreshData();
pivot.ExportToXlsx(fileName);
}
finally {
pivot.CustomFieldValueCells -= pivotGridControl1_CustomFieldValueCells;
pivot.RefreshData();
pivot.Cells.FocusedCell = fCell;
pivot.Cells.Selection = selection;
}
}
System.Diagnostics.Process.Start(fileName);
}
void pivotGridControl1_CustomFieldValueCells(object sender, PivotCustomFieldValueCellsEventArgs e)
{
PivotGridControl pivot = (PivotGridControl)sender;
Rectangle selection = (Rectangle)pivot.Tag;
RemoveUnselectedCells(e, false, selection.Top , selection.Bottom -1 );
RemoveUnselectedCells(e, true, selection.Left , selection.Right - 1);
}
private void RemoveUnselectedCells(PivotCustomFieldValueCellsEventArgs e, bool isColumn, int minIndex, int maxIndex)
{
for (int i = e.GetCellCount( isColumn ) - 1; i >= 0; i--)
{
FieldValueCell cell = e.GetCell(isColumn, i);
if (cell == null) continue;
if (minIndex > cell.MaxIndex || maxIndex < cell.MinIndex)
e.Remove(cell);
}
}
}
}