出口数据表到CSV与保存文件对话框 - C# [英] export datatable to CSV with Save File Dialog box - C#

查看:136
本文介绍了出口数据表到CSV与保存文件对话框 - C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

努力让我的CSV导出,以显示保存文件对话框了。它保存到文件就OK了,但不会让用户保存。任何人都可以看到我可能会用code是想错了吗?

Struggling to get my CSV export to display the Save File dialog box up. It saves to file ok, but doesn't let the user save. Can anybody see where I might be going wrong with the code please?

string filename = Server.MapPath("~/download.csv");
StreamWriter sw = new StreamWriter(filename, false);

int iColCount = dt.Columns.Count;
for (int i = 0; i < iColCount; i++)
{
    sw.Write(dt.Columns[i]);
    if (i < iColCount - 1)
    {                
        sw.Write(",");
    }
}
sw.Write(sw.NewLine);

foreach (DataRow dr in dt.Rows)
{
    for (int i = 0; i < iColCount; i++)
    {
        if (!Convert.IsDBNull(dr[i]))
        {
            sw.Write(dr[i].ToString());
        }
        if (i < iColCount - 1)
        {
            sw.Write(",");
        }
    }
    sw.Write(sw.NewLine);
}
sw.Close();

Response.Clear();
Response.ContentType = "application/csv";
Response.AddHeader("Content-Disposition", "attachment; filename=download.csv");
Response.WriteFile(filename);
Response.Flush();
Response.End();

我以为长大对话框中的内容处理线,但也许有别的东西,我需要做的。

I thought the Content Disposition line that brought up the dialog box, but maybe there is something else I need to do.

感谢

推荐答案

嗯,已经发现的问题是什么...

Ah, have discovered what the problem is...

我已经得到了按钮更新面板并没有意识到响应对象在一个更新面板不起作用。

I've got the button in an update panel and didn't realise the response object doesn't work in an update panel.

我做的下载按钮触发,现在的伟大工程。

I made the download button a trigger, and it now works great.

<Triggers>
    <asp:PostBackTrigger ControlID="btnDownload" />
</Triggers>

感谢您的建议反正

Thanks for the suggestions anyway

这篇关于出口数据表到CSV与保存文件对话框 - C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆