#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); //保存栏位状态
|
}
|
}
|
}
|
}
|