Beispiel #1
0
        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem28_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            GridControl grid = null;

            switch (xtraTabControl1.SelectedTabPageIndex)
            {
            case 0:
                grid = gridControl1;
                break;

            case 1:
                grid = gridControl2;
                break;

            case 2:
                grid = gridControl3;
                break;
            }

            SaveFileDialog fileDialog = new SaveFileDialog();

            fileDialog.Title  = "导出Excel";
            fileDialog.Filter = "Excel文件(*.xlsx)|*.xlsx";

            DialogResult dialogResult = fileDialog.ShowDialog(this);

            if (dialogResult == DialogResult.OK)
            {
                DevExpress.XtraPrinting.XlsxExportOptions options = new DevExpress.XtraPrinting.XlsxExportOptions();
                options.TextExportMode = TextExportMode.Text;//设置导出模式为文本
                grid.ExportToXlsx(fileDialog.FileName, options);
                XtraMessageBox.Show("导出成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
Beispiel #2
0
        private void barButtonItem4_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SaveFileDialog fileDialog = new SaveFileDialog();

            fileDialog.Title  = "导出Excel";
            fileDialog.Filter = "Excel文件(*.xlsx)|*.xlsx";

            DialogResult dialogResult = fileDialog.ShowDialog(this);

            if (dialogResult == DialogResult.OK)
            {
                DevExpress.XtraPrinting.XlsxExportOptions options = new DevExpress.XtraPrinting.XlsxExportOptions();
                options.TextExportMode = TextExportMode.Text;                //设置导出模式为文本

                if (tabPane1.SelectedPageIndex == 0)
                {
                    gridControl1.ExportToXlsx(fileDialog.FileName, options);
                }
                else if (tabPane1.SelectedPageIndex == 1)
                {
                    gridControl2.ExportToXlsx(fileDialog.FileName, options);
                }
                XtraMessageBox.Show("导出成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
Beispiel #3
0
        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem28_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            //string s_json = string.Empty;
            //s_json = JsonConvert.SerializeObject(dt_managefee);
            //textEdit1.Text = s_json;
            Frm_FinStat_select frm_1 = new Frm_FinStat_select();

            if (frm_1.ShowDialog() == DialogResult.OK)
            {
                int         i_index = Convert.ToInt32(frm_1.swapdata["index"]);
                GridControl grid    = null;
                if (i_index == 0)
                {
                    grid = gridControl1;
                }
                else if (i_index == 1)
                {
                    grid = gridControl2;
                }
                else if (i_index == 2)
                {
                    grid = gridControl3;
                }

                SaveFileDialog fileDialog = new SaveFileDialog();
                fileDialog.Title  = "导出Excel";
                fileDialog.Filter = "Excel文件(*.xlsx)|*.xlsx";

                DialogResult dialogResult = fileDialog.ShowDialog(this);
                if (dialogResult == DialogResult.OK)
                {
                    DevExpress.XtraPrinting.XlsxExportOptions options = new DevExpress.XtraPrinting.XlsxExportOptions();
                    options.TextExportMode = TextExportMode.Text;                    //设置导出模式为文本
                    grid.ExportToXlsx(fileDialog.FileName, options);
                    XtraMessageBox.Show("导出成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            frm_1.Dispose();
        }
Beispiel #4
0
        /// <summary>
        /// DevExpress控件通用导出Excel,支持多个控件同时导出在同一个Sheet表或者分不同工作薄
        /// eg:ExportToXlsx("test",true,"控件",gridControl1,gridControl2);
        /// 将gridControl1和gridControl2的数据一同导出到同一个文件不同的工作薄
        /// eg:ExportToXlsx("test",false,"",gridControl1,gridControl2);
        /// 将gridControl1和gridControl2的数据一同导出到同一个文件同一个的工作薄
        /// <param name="FileName">Excel路径</param>
        /// <param name="isPageForEachLink">多个打印控件是否分多个工作薄显示</param>
        /// <param name="sheetName">工作薄名称</param>
        /// <param name="printables">控件集 eg:GridControl,PivotGridControl,TreeList,ChartControl...</param>
        public static void ExportToExcel(string FileName, bool isPageForEachLink, string sheetName,
                                         params DevExpress.XtraPrinting.IPrintable[] printables)
        {
            // SaveFileDialog saveFileDialog = new SaveFileDialog()
            // {
            //     FileName = title,
            //     Title = "导出Excel",
            //     Filter = "Excel文件(*.xlsx)|*.xlsx|Excel文件(*.xls)|*.xls"
            // };
            // DialogResult dialogResult = saveFileDialog.ShowDialog();
            // if (dialogResult == DialogResult.Cancel)
            //     return;
            // string FileName = saveFileDialog.FileName;

            DevExpress.XtraPrintingLinks.CompositeLink link =
                new DevExpress.XtraPrintingLinks.CompositeLink(new DevExpress.XtraPrinting.PrintingSystem());
            foreach (var item in printables)
            {
                var plink = new DevExpress.XtraPrinting.PrintableComponentLink()
                {
                    Component = item
                };
                link.Links.Add(plink);
            }

            if (isPageForEachLink) //15.1 的Xls不支持这个功能,15.2未知
            {
                link.CreatePageForEachLink();
            }
            //默认工作薄名称
            if (string.IsNullOrEmpty(sheetName))
            {
                sheetName = "Sheet";
            }
            try
            {
                int count = 1;
                //在重复名称后加(序号)
                while (System.IO.File.Exists(FileName))
                {
                    if (FileName.Contains(")."))
                    {
                        int start = FileName.LastIndexOf("(");
                        int end   = FileName.LastIndexOf(").") - FileName.LastIndexOf("(") + 2;
                        FileName = FileName.Replace(FileName.Substring(start, end), string.Format("({0}).", count));
                    }
                    else
                    {
                        FileName = FileName.Replace(".", string.Format("({0}).", count));
                    }

                    count++;
                }

                if (FileName.LastIndexOf(".xlsx") >= FileName.Length - 5)
                {
                    DevExpress.XtraPrinting.XlsxExportOptions options =
                        new DevExpress.XtraPrinting.XlsxExportOptions(TextExportMode.Value, true, false, true)
                    {
                        SheetName = sheetName
                    };
                    if (isPageForEachLink)
                    {
                        options.ExportMode = DevExpress.XtraPrinting.XlsxExportMode.SingleFilePageByPage;
                    }
                    link.ExportToXlsx(FileName, options);
                }
                else
                {
                    DevExpress.XtraPrinting.XlsExportOptions options =
                        new DevExpress.XtraPrinting.XlsExportOptions(TextExportMode.Value, true, false, true)
                    {
                        SheetName = sheetName
                    };
                    if (isPageForEachLink) //15.Xls没有这个属性,15.2未知
                    {
                        options.ExportMode = DevExpress.XtraPrinting.XlsExportMode.SingleFilePageByPage;
                    }
                    link.ExportToXls(FileName, options);
                }

                if (DevExpress.XtraEditors.XtraMessageBox.Show("保存成功,是否打开文件?", "提示", MessageBoxButtons.YesNo,
                                                               MessageBoxIcon.Information) == DialogResult.Yes)
                {
                    System.Diagnostics.Process.Start(FileName); //打开指定路径下的文件
                }
            }
            catch (Exception ex)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message);
            }
        }
Beispiel #5
0
            public void Export(ViewExportType exportType)
            {
                SaveFileDialog dialog1 = new SaveFileDialog();
                if (exportType == ViewExportType.Excel2003)
                    dialog1.Filter = "Excel Workbook (*.xls)|*.xls";
                else if (exportType == ViewExportType.Excel2007)
                    dialog1.Filter = "Excel Workbook (*.xslx)|*.xlsx";
                else if (exportType == ViewExportType.CSV)
                    dialog1.Filter = "CSV (Comma Delimited) (*.csv)|*.csv";

                dialog1.Title = "Save As";
                dialog1.CheckPathExists = true;
                dialog1.CheckFileExists = false;
                if (dialog1.ShowDialog() == DialogResult.OK) {
                    if (dialog1.FileName != "") {
                        if (dialog1.FilterIndex == 1) {
                            gridView1.OptionsPrint.AutoWidth = false;
                            gridView1.BestFitColumns();

                            FileStream fs = (FileStream)dialog1.OpenFile();
                            if (exportType == ViewExportType.CSV) {
                                DevExpress.XtraPrinting.CsvExportOptions opts = new DevExpress.XtraPrinting.CsvExportOptions();
                                gridView1.Export(DevExpress.XtraPrinting.ExportTarget.Csv, fs, opts);
                            }
                            else if (exportType == ViewExportType.Excel2007) {
                                DevExpress.XtraPrinting.XlsxExportOptions opts = new DevExpress.XtraPrinting.XlsxExportOptions();
                                opts.ExportMode = DevExpress.XtraPrinting.XlsxExportMode.SingleFile;
                                opts.SheetName = "Sheet1";
                                gridControl1.ExportToXlsx(fs, opts);
                            }
                            else if (exportType == ViewExportType.Excel2003) {
                                DevExpress.XtraPrinting.XlsExportOptions opts = new DevExpress.XtraPrinting.XlsExportOptions();
                                opts.ExportMode = DevExpress.XtraPrinting.XlsExportMode.SingleFile;
                                opts.SheetName = "Sheet1";
                                gridControl1.ExportToXls(fs, opts);
                            }
                            fs.Close();
                        }
                    }
                }
            }