GridView导出EXCEL(C#)

huminghua 2010-11-09 09:55:21
求一个效率比较高的导出EXCEL!由于数据量比较大所以要求导出的效率要高!
...全文
374 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
huminghua 2010-11-09
  • 打赏
  • 举报
回复
MessageBox看到这个,这是winform咯!不是WEB咯!嘿嘿!其实是差不多啦!
xuzysun 2010-11-09
  • 打赏
  • 举报
回复
给它一个线程,让它慢慢导!!
chazikai24 2010-11-09
  • 打赏
  • 举报
回复
excel的话最多也就支持6W5K多条数据,数据量又不大。最多半分钟能搞定。很快的啊
g505149841 2010-11-09
  • 打赏
  • 举报
回复

saveexcel . Filter = "Execl files (*.xls)|*.xls|文本文件|*.txt";
saveexcel . FilterIndex = 0;
saveexcel.RestoreDirectory = true;
saveexcel.Title = "導出檔保存路徑";
saveexcel.FileName = null;
saveexcel.ShowDialog();
string FileName = saveexcel.FileName;

if (FileName.Length != 0)
{



FileStream objFileStream;
StreamWriter objStreamWriter;
string strLine = "";
objFileStream = new FileStream(FileName, System.IO.FileMode.Create, System.IO.FileAccess.Write);



objStreamWriter = new StreamWriter(objFileStream, System.Text.Encoding.Unicode);


for (int i = 0; i < dt.Columns.Count; i++)
{
strLine = strLine + dt.Columns[i].ColumnName.ToString() + Convert.ToChar(9);

}
objStreamWriter.WriteLine(strLine);
strLine = "";

for (int i = 0; i < dt.Rows.Count; i++)
{

for (int j = 0; j < dt.Columns.Count; j++)
{
strLine = strLine + dt.Rows[i][j].ToString() + Convert.ToChar(9);

}
objStreamWriter.WriteLine(strLine);

strLine = "";
}
objStreamWriter.Close();
objFileStream.Close();
MessageBox.Show("資料已經成功導出到:" + saveexcel.FileName.ToString(), "導出完成", MessageBoxButtons.OK, MessageBoxIcon.Information);
huminghua 2010-11-09
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 fdh120 的回复:]

public static void GridViewToExcel(GridView Gdv, string stitle)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
Ht……
[/Quote]效率不高!我就是想换个效率高的!
herott632482577 2010-11-09
  • 打赏
  • 举报
回复
private void button4_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
saveFileDialog.FilterIndex = 0;
saveFileDialog.RestoreDirectory = true;
saveFileDialog.CreatePrompt = true;
saveFileDialog.Title = "导出Excel文件到";

DateTime now = DateTime.Now;
saveFileDialog.FileName = now.Year.ToString().PadLeft(2)
+ now.Month.ToString().PadLeft(2, '0')
+ now.Day.ToString().PadLeft(2, '0') + "-"
+ now.Hour.ToString().PadLeft(2, '0')
+ now.Minute.ToString().PadLeft(2, '0')
+ now.Second.ToString().PadLeft(2, '0');

saveFileDialog.ShowDialog();

Stream myStream;
myStream = saveFileDialog.OpenFile();
StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding("gb2312"));
string str = "";
try
{
//写标题
for (int i = 0; i < dataGridView1.ColumnCount; i++)
{
if (i > 0)
{
str += "\t";
}
str += dataGridView1.Columns[i].HeaderText;
}

sw.WriteLine(str);
//写内容
for (int j = 0; j < dataGridView1.Rows.Count; j++)
{
string tempStr = "";
for (int k = 0; k < dataGridView1.Columns.Count; k++)
{
if (k > 0)
{
tempStr += "\t";
}
tempStr += dataGridView1.Rows[j].Cells[k].Value.ToString();
}
sw.WriteLine(tempStr);
}
sw.Close();
myStream.Close();
}
catch (Exception ex)
{
MessageBox.Show("您取消了此次操作");
}
finally
{
sw.Close();
myStream.Close();
}
}

速 战 2010-11-09
  • 打赏
  • 举报
回复
先打开excel,然后把数据 dataset,组合成二维数组 然后再插到EXCEL 效率不错.
不懂装懂 2010-11-09
  • 打赏
  • 举报
回复
public static void GridViewToExcel(GridView Gdv, string stitle)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Charset = "GB2312";
string filename = "attachment;filename=" + System.Web.HttpUtility.UrlEncode(stitle, System.Text.Encoding.UTF8) + ".xls";
HttpContext.Current.Response.AppendHeader("Content-Disposition", filename);
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.ContentType = "application/ms-excel";
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
Gdv.RenderControl(oHtmlTextWriter);
HttpContext.Current.Response.Output.Write(oStringWriter.ToString());
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}
常用写法,不知道高不高效
yang448246711 2010-11-09
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 elkiss 的回复:]

沙发。
[/Quote]

丫头你的沙发就坐得远咧....
huminghua 2010-11-09
  • 打赏
  • 举报
回复
你们这是来蹭分的吗?
elkiss 2010-11-09
  • 打赏
  • 举报
回复
还以为我是沙发呢
yang448246711 2010-11-09
  • 打赏
  • 举报
回复
要不百度一下下.....再不行上google....要不然上天涯....
RONG_SHAO 2010-11-09
  • 打赏
  • 举报
回复
加大计算机硬件性能算不?

将数据切分后分段导出~
yang448246711 2010-11-09
  • 打赏
  • 举报
回复
数据库还是项目?
birdlonger 2010-11-09
  • 打赏
  • 举报
回复
网上自己找个破解的aspose.excel ,体验下什么叫做瞬秒!我就用过那玩意,导出速度无敌快。

110,555

社区成员

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

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

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