我是用for循环把datagridview中的数据一条一条导出到excel的,但是数据超过几千条就好慢,不知道有什么更好的方法
解决方案 »
- 如何动态构造dataset
- C/S client多窗口
- 关于CMPP Deliver信息 string类型字段 XmlSerializer后存在非法字符问题
- c# 如果一个文件已经存在,怎样重写它?
- DataGrid中的数据如何更新到Excel中去,请大家帮忙给出点资料
- 我在picturebox控件上画了个图,当图超过设点大小的时候,外面的部份被删掉了,没有出现滚运条,应该设什么属性啊
- 拖放IE中的链接到我的Form上,如何知道拖动的网址?
- 关于c#中的控件问题。
- 拜托大家聊点实际点的好不好?都在讲.net怎么样怎么样~说点实际点的唉~csdn越来越冷了
- wyd1520来一下 C# Winform TCP数据合包问题 接着之前的一帖子
- 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);
}
}