Infragistics自帶導出控件UltraGridExcelExporter,用此控件可以輕鬆的將UltraWinGrid中的數據導出到excel文件中,而且格式可以自定。
/// <summary>
/// 導出
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnExp_Click(object sender, EventArgs e)
{
if (this.grdCKTZ == null || this.grdCKTZ.Rows.Count == 0)
{
this.label2.Text = "請先查詢後導出";
return;
}
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
saveFileDialog.FilterIndex = 0;
saveFileDialog.RestoreDirectory = true;
saveFileDialog.FileName = "能耗查詢表";
string strName = "";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
strName = saveFileDialog.FileName;
if (strName.Length != 0)
{
try
{
//當前目錄中已存在此文件
if (System.IO.File.Exists(strName))
{
//刪除
System.IO.File.Delete(strName);
}
}
catch
{
this.label2.Text = "導出失敗";
return;
}
this.grdCKTZ.DisplayLayout.Bands[0].HeaderVisible = true;
this.grdCKTZ.DisplayLayout.Bands[0].Header.Caption = "報表名字";
this.grdCKTZ.DisplayLayout.Bands[0].Indentation = 0;
this.grdCKTZ.DisplayLayout.RowConnectorStyle = Infragistics.Win.UltraWinGrid.RowConnectorStyle.None;
this.grdCKTZ.DisplayLayout.Bands[0].Header.Appearance.ThemedElementAlpha = Infragistics.Win.Alpha.Transparent;
this.grdCKTZ.DisplayLayout.Bands[0].Header.Appearance.BackColor = Color.LightBlue;
this.grdCKTZ.DisplayLayout.Bands[0].Header.Appearance.BackGradientStyle = Infragistics.Win.GradientStyle.Horizontal;
this.grdExcelExp.Export(this.grdCKTZ, strName);
this.label2.Text = "導出成功";
}
}
}