private void ToExeilToolStripMenuItem_Click(object sender, EventArgs e)
{
int RowIndtx = 1;
int ColumnIndex = 1;
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
try
{
excel.Application.Workbooks.Add(true);
DataTable dt = (DataTable)this.dataGridView1.DataSource;
if (dt == null) return;
DataColumnCollection dcs = dt.Columns;
foreach (DataColumn dc in dcs)
{
excel.Cells[RowIndtx, ColumnIndex++] = dc.ColumnName;
}
ColumnIndex--;
DataRowCollection drs = dt.Rows;
foreach (DataRow dr in drs)
{
RowIndtx++;
for (int i = 0; i < ColumnIndex; i++)
{
excel.Cells[RowIndtx, i + 1] = dr[i];
}
}
//excel.Visible = true;
SaveFileDialog sfd = new SaveFileDialog();
sfd.ShowDialog();
excel.ActiveWorkbook.SaveAs(sfd.FileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel9795, null, null, false, false
, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, null, null, null, null, null);
}
catch (Exception e1)
{
MessageBox.Show("出现错误:" + e1.Message); }
finally
{
excel.Quit();
excel = null;
}
}
这个是菜单点击事件 ,将dataGridView1 里的数据保存成EXCEL dataGridView1 里的数据 行和列都是不确定的。由于第一对EXCL 操作 想请教下 我这个方法有什么不妥或者更好的方法吗?
有个问题就是 我有个表中数据有6W多条。 保存EXCL的时候出现了错误。 我想 应该是 数据太多了 超过了一个Excl工作薄的最大范围。
对于这个问题 有什么好的解决方法吗?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货