C#WinFrom DEV控件Gridview导入导出Excel

用微软自带效率底,能直接用:

测试1秒20+的数据

Microsoft.Office.Interop.Excel 
public static void ExportToExcel(GridView gridView, string 文件名)
        {
            System.Data.DataTable dt = (gridView.DataSource as System.Data.DataView).Table;
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "Excel文件(*.xls)|*.xls";
            dlg.FilterIndex = 0;
            dlg.RestoreDirectory = true;
            //dlg.CreatePrompt = true;
            dlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
            dlg.FileName = string.Format("{0}_{1}", DateTime.Now.ToString("yyyy-MM-dd HH时mm分"), 文件名);
            if (dlg.ShowDialog() != DialogResult.OK) return;
            string filePath = dlg.FileName;
            using (DevExpress.Utils.WaitDialogForm wdf = new DevExpress.Utils.WaitDialogForm("请稍等", "正在导出数据...", new Size(400, 80)))
            {
                int OLDOFFICEVESION = -4143;
                int NEWOFFICEVESION = 56;
                //保存excel文件的格式
                int FormatNum;
                //excel版本号
                string Version;
                //启动应用
                Excel.Application xlApp = new Excel.Application();

                System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
                Excel.Workbooks workbooks = xlApp.Workbooks;
                //创建文件
                Excel.Workbook workbook = workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
                //创建sheet
                Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];
                //获取你使用的excel 的版本号
                Version = xlApp.Version;
                //使用Excel 97-2003
                if (Convert.ToDouble(Version) < 12)
                {
                    FormatNum = OLDOFFICEVESION;
                }
                //使用 excel 2007或更新
                else
                {
                    FormatNum = NEWOFFICEVESION;
                }
                //添加输出excel表格的表头信息信息
                //注意这里的excel对应的单元格第一个位置为[1,1],而不是我们平时定义[0,0]
                for (int i = 0; i < gridView.Columns.Count; i++)
                {
                    worksheet.Cells[1, i + 1] = gridView.Columns[i].FieldName;
                }
                //添加输出excel表格的内容信息
                for (int rowIndex = 1; rowIndex < dt.Rows.Count + 1; rowIndex++)
                {
                    DataRow rowArr = dt.Rows[rowIndex - 1];
                    for (int cellIndex = 0; cellIndex < gridView.Columns.Count; cellIndex++)
                    {
                        string aa = rowArr[cellIndex].ToString();
                        worksheet.Cells[rowIndex + 1, cellIndex + 1] = rowArr[gridView.Columns[cellIndex].FieldName];
                    }
                }
                //删除已存在的excel文件,否则会无法保存创建的excel文件
                if (File.Exists(filePath))
                {
                    try
                    {
                        File.Delete(filePath);
                    }
                    catch (IOException e)
                    {
                        Console.WriteLine(e.Message);
                    }
                }
                //保存,这里必须指定FormatNum文件的格式,否则无法打开创建的excel文件
                workbook.SaveAs(filePath, FormatNum);
                //显示创建的excel文件,true打开Excel,false隐藏Excel
                xlApp.Visible = false;
                //关闭Excel进程
                xlApp.Quit();

            }
        }


用数据流导出Excel,打开报安全提示,可以导成.CSV 不限制格式问题

测试1秒3W+的数据


  public static void ExportToExcel(GridView gridView, string 文件名)
        {
            if (gridView.DataRowCount < 1) return;
            System.Data.DataTable dt = (gridView.DataSource as System.Data.DataView).Table;
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "Execl files (*.xls)|*.xls";
            dlg.FilterIndex = 0;
            dlg.RestoreDirectory = true;
            //dlg.CreatePrompt = true;
            dlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
            dlg.FileName = string.Format("{0}_{1}", DateTime.Now.ToString("yyyy-MM-dd HH时mm分"), 文件名);
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                Stream myStream = null;
                StreamWriter sw = null;
                try
                {
                    //Stream myStream;
                    myStream = dlg.OpenFile();
                    sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
                    //写入列标题   
                    bool first = true;
                    foreach (GridColumn gc in gridView.Columns)
                    {
                        if (first) first = false;
                        else sw.Write("\t");
                        sw.Write(gc.FieldName);
                    }
                    sw.WriteLine();

                    //写入列内容   
                    for (int i = 0; i < gridView.DataRowCount; i++)
                    {
                        first = true;
                        DataRow dr = dt.Rows[i];
                        foreach (GridColumn gc in gridView.Columns)
                        {
                            if (first) first = false;
                            else sw.Write("\t");
                            sw.Write(dr[gc.FieldName].ToString());
                        }
                        sw.WriteLine();
                    }
                    sw.Close();
                    myStream.Close();
                }
                catch (Exception a)
                {
                    MessageBox.Show(a.ToString());
                }
                finally
                {
                    sw.Close();
                    myStream.Close();
                    MessageBox.Show("导出成功,文件在" + dlg.FileName + "下");
                }
            }
        }

导入Excel,用‘/t’方式导出才能用,正常各式导入出现乱码,没解决 大家可给更好建议

  public static void ExportDataGridToExcel(GridControl gridControl)
        {
            bool daoflag = false;
            OpenFileDialog OpenFile = new OpenFileDialog();
            OpenFile.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
            OpenFile.Filter = "Execl files (*.xls)|*.xls"; ;
            OpenFile.FileName = "";
            if (OpenFile.ShowDialog() != DialogResult.OK) return;
            Stream myStream = null;
            StreamReader sr = null;
            try
            {
                myStream = OpenFile.OpenFile();
                sr = new StreamReader(myStream, System.Text.Encoding.GetEncoding(-0));
                System.Data.DataTable dt = new System.Data.DataTable();
                string FirstLine = sr.ReadLine();
                string[] fieldName = FirstLine.Split('\t');
                for (int i = 0; i < fieldName.Length; i++)
                {
                    dt.Columns.Add(fieldName[i]);
                }

                for (;;)
                {
                    if (daoflag)
                    { MessageBox.Show("程序中断"); break; }
                    string ln = sr.ReadLine();
                    if (ln == null) break;
                    DataRow dr = dt.NewRow();
                    string[] fields = ln.Split('\t');
                    if (fields[0] == null) continue;
                    int j = 0;
                    foreach (string each in fields)
                    {
                        dr[j] = each;
                        j++;
                    }
                    dt.Rows.Add(dr);
                }
                //for (int i = 0; i < dt.Rows.Count; i++)
                //{
                //    gridView.AddNewRow();
                //    gridView.MoveLast();
                //    DataRow dr = dt.Rows[i];
                //    foreach (GridColumn gc in gridView.Columns)
                //    {
                //        if (!为空(dr[gc.FieldName]))
                //        {
                //            gridView.SetRowCellValue(i, gc.FieldName, dr[gc.FieldName]);
                //        }
                //    }
                //}
                gridControl.DataSource = dt;
            }
            catch (Exception er)
            {
                MessageBox.Show(er.ToString());
                return;
            }
            finally
            {
                sr.Close();
                myStream.Close();
            }
            MessageBox.Show("导入商品会员价设置单完成!!", "注意", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值