各位大侠:小弟想把dataGridView(其中有100万条数据)中的数据导入到Excel中去,现在我用的这些代码能都导出一部分,也就是只能导出6万多条数据,之后就要报错了,在网上查了查说是当数据超过一个数据值后可以重新增加一个sheet,这样就可以把全部数据导入一个Excel中了,我的代码如下:我该如何修改呢?还望各位指教!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
//using Excel = Microsoft.Office.Interop.Excel;
namespace ppppppp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            BindUser();
        }
        /// <summary>
        /// 绑定数据
        /// </summary>
        private void BindUser()
        {
            string connStr = "Data Source=.;Database=UltraTEVMonitor#000;uid=sa;PWD=000";
            string sql = "select * from AlarmLog";
            SqlConnection conn = new SqlConnection(connStr);
            SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0].DefaultView;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //导出到Excel
            try
            {
                if (dataGridView1.Rows.Count == 1)
                {
                    MessageBox.Show("没有数据可以导出");
                }
                else
                {
                    //建立Excel对象
                    Excel.Application excel = new Excel.Application();
                    if (excel == null)
                    {
                        MessageBox.Show("Excel无法启动");
                    }
                    else
                    {
                        excel.Application.Workbooks.Add(true);
                        excel.Visible = true;
                        //生成字段名称
                        for (int i = 0; i < dataGridView1.ColumnCount; i++)
                        {
                            excel.Cells[1, i + 1] = dataGridView1.Columns[i].HeaderText;
                        }
                        //填充数据
                        for (int i = 0; i < dataGridView1.RowCount - 1; i++)
                        {
                            for (int j = 0; j < dataGridView1.ColumnCount; j++)
                            {
                                if (dataGridView1[j, i].ValueType == typeof(string))
                                    excel.Cells[i + 2, j + 1] = "" + dataGridView1[j, i].Value.ToString();
                                else
                                    excel.Cells[i + 2, j + 1] = dataGridView1[j, i].Value.ToString();
                            }
                        }
                    }
                }
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
            }
        }
    }
}