Infragistics第三方控件导出数据到excel

来源:互联网 发布:有道语音翻译软件 编辑:程序博客网 时间:2024/05/22 13:20

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 = "导出成功";
                }
            }
        }

原创粉丝点击