我是用for循环把datagridview中的数据一条一条导出到excel的,但是数据超过几千条就好慢,不知道有什么更好的方法

解决方案 »

  1.   

    是不是只有用for循环导出数据
      

  2.   

            /// <summary>
            /// 判断本机是否安装Excel文件方法
            /// </summary>
            /// <returns></returns>
            private static bool codeboolisExcelInstalled()
            {
                Type type = Type.GetTypeFromProgID("Excel.Application");
                return type != null;
            }/// <summary>
            /// 将DataGridView内容到非标准的Excel格式,默认工作表名与文件名相同 
            /// </summary>
            public static void ExportToNonstandardExcel(DataGridView myDGV)
            {
                if (codeboolisExcelInstalled())
                {
                    if (GetEffectiveRows(myDGV) == 0)
                    {
                        MessageBox.Show(" 没有数据可供导出!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    else
                    {
                        SaveFileDialog saveFileDialog = new SaveFileDialog();
                        saveFileDialog.DefaultExt = "xls";
                        saveFileDialog.Filter = "Microsoft Excel 97-2003 (*.xls)|*.xls|Microsoft Excel 2007-2010 (*.xlsx)|*.xlsx";
                        saveFileDialog.FilterIndex = 0;
                        saveFileDialog.RestoreDirectory = true;
                        saveFileDialog.CreatePrompt = false;//指示该文件不存在,是否提示用户创建
                        saveFileDialog.Title = "导出数据到Excel表格";
                        saveFileDialog.ShowDialog();
                        if (saveFileDialog.FileName.IndexOf(":") < 0) return; //被点了"取消"
                        Stream myStream;
                        myStream = saveFileDialog.OpenFile();
                        StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
                        string columnTitle = "";
                        try
                        {
                            //写入列标题
                            for (int i = 0; i < myDGV.ColumnCount; i++)
                            {
                                if (i > 0)
                                {
                                    columnTitle += "\t";
                                }
                                columnTitle += myDGV.Columns[i].HeaderText;
                            }
                            sw.WriteLine(columnTitle);                        //写入列内容
                            for (int j = 0; j < myDGV.Rows.Count; j++)
                            {
                                string columnValue = "";
                                for (int k = 0; k < myDGV.Columns.Count; k++)
                                {
                                    if (k > 0)
                                    {
                                        columnValue += "\t";
                                    }
                                    if (myDGV.Rows[j].Cells[k].Value == null)
                                    {
                                        columnValue += "";
                                    }
                                    else
                                    {
                                        columnValue += myDGV.Rows[j].Cells[k].Value.ToString().Trim();
                                    }
                                }
                                sw.WriteLine(columnValue);
                            }
                            MessageBox.Show("数据导出成功! ", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        catch (Exception er)
                        {
                            MessageBox.Show(er.Message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        finally
                        {
                            sw.Close();
                            myStream.Close();
                            GC.Collect();//强行销毁
                        }
                    }
                }
                else
                {
                    MessageBox.Show("当前系统没有发现可执行的Excel文件,\r\n\r\n如需使用Excel功能请先安装Office程序!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }