单击按钮时如何创建用于选择目标的浏览窗口

时间:2011-11-21 14:55:30

标签: c# asp.net button

我有一个简单的按钮,用于将数据库表从MySQL导出到excel文件。到目前为止,它的目标是硬编码到C:/驱动器。当您按下导出按钮时,您将获得excel文件自动导出到C:用户无法看到它。 这是代码:

//button for Exporting to Excel (.xls)
    //hardcoded destination to C:/
    protected void Button2_Click(object sender, EventArgs e)
    {
        try
        {
            string connectionString = "server=localhost; UserId=root; database=dbcss; ";
            MySqlConnection con = new MySqlConnection(connectionString);

            con.Open();
            string getProject = ddlProjectID.SelectedValue; //get the already selected project from the dropdown menu

            //create sql command object
            string cmdText = "SELECT 'activity_id', 'activity_name', 'wbs_no', 'activity_status', 'wbs_name', 'sequence', 'pad', 'project_id', 'start_date', 'finish_date' INTO OUTFILE 'C://TASK.xls' FROM management WHERE project_id = '" + getProject + "';";

            MySqlCommand cmd = new MySqlCommand(cmdText, con);

            cmd.ExecuteNonQuery();
            con.Close();

        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }

    }

如何在弹出窗口的地方弹出电脑并选择目的地?

任何帮助将不胜感激 谢谢;)

1 个答案:

答案 0 :(得分:1)

您必须更改响应标头和内容类型,并将文件发送回浏览器:

试试这些:

Send query results to Excel from ASP.NET website

One click ASP.NET export button control for Excel/CSV file