winform 怎么提高数据导出到execl的速度

victyhappy 2010-10-26 11:39:57
用下面这个写速度还可以,我安装的就是excel2007的,用这个方式的话当保存格式为.xlsx之后,无法打开,请问高手导出到excel导出速度快,格式正确要怎么做

private void print(DevExpress.XtraGrid.Views.Grid.GridView gridView1)
{
if (gridView1.DataRowCount == 0)
{
MessageBox.Show("没有数据可供导出!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else
{
SaveFileDialog dlg = new SaveFileDialog();
dlg.DefaultExt = "xlsx";
dlg.Filter = "Excel2007文件(*.xlsx)|*.xlsx|Excel file(*.xls)|*.xls";
dlg.CheckFileExists = false;
dlg.CheckPathExists = false;
dlg.FilterIndex = 0;
dlg.RestoreDirectory = true;
dlg.CreatePrompt = true;
dlg.Title = "保存为excel文件";
if (dlg.ShowDialog() == DialogResult.OK)
{
Stream stream;
stream = dlg.OpenFile();
StreamWriter sw = new StreamWriter(stream, System.Text.Encoding.GetEncoding(-0));
string columnTitle = "";
try
{
for (int i = 0; i < gridView1.Columns.Count - 4; i++)
{
if (i > 0)
{
columnTitle += "\t";
}
columnTitle += gridView1.Columns[i].Caption;
}
sw.WriteLine(columnTitle);

for (int j = 0; j < gridView1.DataRowCount; j++)
{
string columnValue = "";
for (int k = 0; k < gridView1.Columns.Count - 4; k++)
{

if (k > 0)
{
columnValue += "\t";
}
if (gridView1.GetDataRow(j)[k].Equals(" "))
columnValue += " ";
else
columnValue += gridView1.GetDataRow(j)[k].ToString().Replace("\r\n", " ");

}
sw.WriteLine(columnValue);
}
sw.Close();
stream.Close();
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
finally
{
sw.Close();
stream.Close();
}
}
...全文
165 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
victyhappy 2010-10-26
  • 打赏
  • 举报
回复
线程。详解,不怎么会线程
flyerwing 2010-10-26
  • 打赏
  • 举报
回复
软件不明显,还是多加几个大CPU搞效果会明显点.
netstray 2010-10-26
  • 打赏
  • 举报
回复
关注 中……………………
syluoping 2010-10-26
  • 打赏
  • 举报
回复
用Thread吧,这样界面不会死掉。
niaoked 2010-10-26
  • 打赏
  • 举报
回复
如果数据量真的很大的话就需要用多个线程去导出数据了。
victyhappy 2010-10-26
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 wuyq11 的回复:]

如果数据量比较大,最好不要逐个单元格写入,而应该使用数组写入方式
2007 默认的是 XLSX 它以兼容模式打开 2003 保存的XLS
2003需要安装兼容包.
[/Quote

我保存的格式就是.XLSX模式,而且安装的也是2007的,保存后打开就出现那个问题
wuyq11 2010-10-26
  • 打赏
  • 举报
回复
如果数据量比较大,最好不要逐个单元格写入,而应该使用数组写入方式
2007 默认的是 XLSX 它以兼容模式打开 2003 保存的XLS
2003需要安装兼容包.
victyhappy 2010-10-26
  • 打赏
  • 举报
回复
我上面的这个问题主要是保存的时候有个问题,提示格式与扩展名不一致,怎么回事

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧