• C#把gird数据导出到excel



            /// <summary>
            /// DataTable导出到Excel
            /// </summary>
            /// <param name="fileName">默认的文件名</param>
            /// <param name="dataTable">数据源,一个DataTable数据表</param>
            /// <param name="titleRowCount">标题占据的行数,为0则表示无标题</param>
            public void ExportExcel(string fileName, DevExpress.Xpf.Grid.GridControl gc, int titleRowCount)
            {
                string saveFileName = "";
                //bool fileSaved = false;
                SaveFileDialog saveDialog = new SaveFileDialog();
                saveDialog.DefaultExt = "xls";
                saveDialog.Filter = "Excel文件|*.xls";
                saveDialog.FileName = fileName;
                saveDialog.ShowDialog();
                saveFileName = saveDialog.FileName;
                if (saveFileName.IndexOf(":") < 0) return; //被点了取消
                Microsoft.Office.Interop.Excel.Application xlApp;
                try
                {
                    xlApp = new Microsoft.Office.Interop.Excel.Application();

                }
                catch (Exception)
                {
                    MessageBox.Show("无法创建Excel对象,可能您的机子未安装Excel");
                    return;
                }
                finally
                {
                }

                Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
                Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
                Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];//取得sheet1
                //写Title
                if (titleRowCount != 0)
                    MergeCells(worksheet, 1, 1, titleRowCount, gc.Columns.Count, "test");

                //写入列标题
                for (int i = 0; i <= gc.Columns.Count - 1; i++)
                {
                    worksheet.Cells[titleRowCount + 1, i + 1] = gc.Columns[i].Header.ToString();

                }
                //写入数值
                for (int r = 0; r <= gc.GetRowsAsync(0, gc.VisibleRowCount).Result.Count; r++)
                {
                    for (int i = 0; i <= gc.Columns.Count - 1; i++)
                    {
                        string cellValue1 = gc.GetCellDisplayText(r, gc.Columns[i]);
                        worksheet.Cells[r + titleRowCount + 2, i + 1] = cellValue1;
                    }
                    System.Windows.Forms.Application.DoEvents();
                }

                worksheet.Columns.EntireColumn.AutoFit();//列宽自适应
                Excel.Range rg = worksheet.Range[worksheet.Cells[2, 2], worksheet.Cells[gc.GetRowsAsync(0, gc.VisibleRowCount).Result.Count+1, 2]];
                rg.NumberFormat = "000000";

                if (saveFileName != "")
                {
                    try
                    {
                        workbook.Saved = true;
                        workbook.SaveCopyAs(saveFileName);
                        //fileSaved = true;
                    }
                    catch (Exception ex)
                    {
                        //fileSaved = false;
                        MessageBox.Show("导出文件时出错,文件可能正被打开!n" + ex.Message);
                    }

                }
                xlApp.Quit();
                GC.Collect();//强行销毁
                // if (fileSaved && System.IO.File.Exists(saveFileName)) System.Diagnostics.Process.Start(saveFileName); //打开EXCEL
                MessageBox.Show(fileName + "导出到Excel成功", "提示", MessageBoxButton.OK);
            }

  • 相关阅读:
    使用eclipse的时候maven项目的项目没有问题,但是项目报×
    大数据学习笔记5
    大数据学习笔记4
    Docker端口映射
    hadoop集群访问——Hadoop客户端访问、Java API访问
    Hadoop环境搭建--Docker完全分布式部署Hadoop环境(菜鸟采坑吐血整理)
    Java反射
    大数据学习笔记3
    大数据学习笔记2
    大数据学习笔记1-大数据处理架构Hadoop
  • 原文地址:https://www.cnblogs.com/tianmochou/p/6290029.html
Copyright © 2020-2023  润新知