datagridview导成excel很慢 我是用for循环把datagridview中的数据一条一条导出到excel的,但是数据超过几千条就好慢,不知道有什么更好的方法 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 是不是只有用for循环导出数据 /// <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); } } C#用中文做变量名、函数名、类名、命名空间名等有什么问题或不妥 路径编码的的问题 如何禁用ctrl+c winform中TreeView的AfterSelect问题 c#如何监控进程的线程? 关于Socket.BeginSend的最后一个参数 c# 一个关于类的简单问题 急等!明天就要用了,请大家帮忙,C#中的XML解析!!! 如何改变TableControl上面标签的颜色呢 c# 打开文件的问题 winform下隐藏控件问题 如何在 DataTable 中定位 DataGridView 所指
/// 判断本机是否安装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);
}
}