各位大侠:小弟想把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);
}
}
}
}
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);
}
}
}
}
解决方案 »
- 怎么让在winform所浏览的文件绝对路径,在数据库中保存它的相对路径?或者怎么在winform中怎么获得浏览文件的相对路径啊!
- 求一个解释。。
- win7与VS 2008 兼容问题..知道的麻烦帮下忙,谢谢了
- 第三方控件UltraWebTree 的使用
- 谁给个socket 服务器与客户端的案例,实现多人聊天和私聊功能。。
- (100分)求教如何利用ngen.exe命令将程序集在安装时生成本地代码??
- 已添加到“组件服务”的com怎样在C#下调用呀
- WindowsForm Treeview
- 怎么用datagridview批量添加数据哦 谢谢,第一次用wingdows编程, 请指点指点哦,
- 怎么样在主窗体中改变子窗体中一个文本框的内容?
- 一个关于keypreview的小问题
- C#启动扫描仪
思路:自动添加sheet 写为方法(调用则创建一个新的sheet),把你的数据进行分条数进行写入
虽然可以解决此问题,但效率绝对让你吃惊