1
yhj
2024-07-24 5e5d945e91568b973faa27d8ab0bcef99fc4a6c5
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#region
 
using System;
using System.Collections;
using System.Windows.Forms;
using CSFrameworkV5.Common;
using CSFrameworkV5.Core;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
 
#endregion
 
namespace CSFrameworkV5.Library
{
    public partial class frmExportGridData : frmBaseDialog
    {
        private Hashtable _columnVisibleState;
        private GridView _gv;
 
        private frmExportGridData()
        {
            InitializeComponent();
            _columnVisibleState = new Hashtable();
        }
 
        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }
 
        private void btnCheckAll_Click(object sender, EventArgs e)
        {
            chkListColumns.CheckAll();
        }
 
        private void btnOk_Click(object sender, EventArgs e)
        {
            if (chkListColumns.CheckedItems.Count < 1)
            {
                Msg.Warning("请勾选要导出的栏位!");
                return;
            }
 
            try
            {
                var dlg = new SaveFileDialog();
                if (cmbFileFormat.SelectedIndex == 0)
                    dlg.Filter = cmbFileFormat.Text + "|*.xls";
 
                if (cmbFileFormat.SelectedIndex == 1)
                    dlg.Filter = cmbFileFormat.Text + "|*.xlsx";
 
                if (cmbFileFormat.SelectedIndex == 2)
                    dlg.Filter = cmbFileFormat.Text + "|*.pdf";
 
                if (cmbFileFormat.SelectedIndex == 3)
                    dlg.Filter = cmbFileFormat.Text + "|*.rtf";
 
                if (cmbFileFormat.SelectedIndex == 4)
                    dlg.Filter = cmbFileFormat.Text + "|*.html";
 
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    _gv.BeginUpdate();
                    HideColumn(true);
                    btnOk.Enabled = false;
                    frmWaitingEx.ShowMe(this);
                    if (cmbFileFormat.SelectedIndex == 0)
                        _gv.ExportToXls(dlg.FileName);
 
                    if (cmbFileFormat.SelectedIndex == 1)
                        _gv.ExportToXlsx(dlg.FileName);
 
                    if (cmbFileFormat.SelectedIndex == 2)
                        _gv.ExportToPdf(dlg.FileName);
 
                    if (cmbFileFormat.SelectedIndex == 3)
                        _gv.ExportToRtf(dlg.FileName);
 
                    if (cmbFileFormat.SelectedIndex == 4)
                        _gv.ExportToHtml(dlg.FileName);
 
                    frmWaitingEx.HideMe(this);
                    HideColumn(false);
                    _gv.EndUpdate();
                    Msg.ShowInformation("导出文件成功!\r\n" + dlg.FileName);
                }
 
                btnOk.Enabled = true;
            }
            catch (Exception ex)
            {
                frmWaitingEx.HideMe(this);
                Msg.ShowException(ex);
                btnOk.Enabled = true;
            }
        }
 
        private void btnUnCheckAll_Click(object sender, EventArgs e)
        {
            chkListColumns.UnCheckAll();
        }
 
        /// <summary>
        ///     功能入口
        /// </summary>
        /// <param name="gv"></param>
        public static void DoExport(GridView gv)
        {
            var form = new frmExportGridData();
            form._gv = gv;
            form.LoadGridColumns(gv, new string[] { });
            form.ShowDialog();
        }
 
        public static void DoExport(GridView gv, string[] columnFields)
        {
            var form = new frmExportGridData();
            form._gv = gv;
            form.LoadGridColumns(gv, columnFields);
            form.ShowDialog();
        }
 
        private void frmExportGridData_FormClosing(object sender,
            FormClosingEventArgs e)
        {
            HideColumn(false);
        }
 
        private void frmExportGridData_Load(object sender, EventArgs e)
        {
            cmbFileFormat.SelectedIndex = 0; //预设导出Excel
        }
 
        private void HideColumn(bool isHide)
        {
            //隐藏没有勾选的栏位
            if (isHide)
            {
                foreach (CheckedListBoxItem item in chkListColumns.Items)
                    _gv.Columns[item.Value.ToStringEx()].Visible =
                        item.CheckState == CheckState.Checked;
            }
            else
            {
                //还原栏位显示状态
                if (_columnVisibleState.Count > 0)
                    foreach (GridColumn col in _gv.Columns)
                        col.Visible = (bool)_columnVisibleState[col.FieldName];
            }
        }
 
        private void LoadGridColumns(GridView view, string[] columnFields)
        {
            chkListColumns.Items.Clear();
 
            var check = false;
 
            //加载栏位列表.
            foreach (GridColumn col in view.Columns)
            {
                check = Array.IndexOf(columnFields, col.FieldName) >= 0;
                chkListColumns.Items.Add(col.FieldName, col.Caption,
                    CheckState.Unchecked, true);
                if (!_columnVisibleState.ContainsKey(col.FieldName))
                    _columnVisibleState.Add(col.FieldName,
                        col.Visible); //保存栏位状态
            }
        }
    }
}