MVC运行一个存储过程来导出到Excel外部的实体框架

时间:2021-11-15 02:06:56

I have a list in a table and I use that list to display a list of reports I can generate by clicking on the export button - I then want the export button to take the question code and export the stored procedure output to excel.

我在表中有一个列表,我使用这个列表来显示我可以通过单击export按钮生成的报告列表——然后我希望export按钮获取问题代码并将存储过程输出导出到excel。

I can't use entity framework as I use 1 stored procedure and each Question Code generates a different set of columns

我不能使用实体框架,因为我使用了一个存储过程,每个问题代码生成不同的列集

    [HttpPost]
    public ActionResult Export(string QCode = "")
    {

        if (QCode != "")
        {
            GridView gv = new GridView() { AutoGenerateColumns = true };
            // Run Stored Procedure Code Not using Entity Framework
            gv.DataSource = StoredProcedure(QCode).ToList();

            gv.DataBind();
            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment; filename=ExportQuestion_" + QCode + ".xls");
            Response.ContentType = "application/ms-excel";
            Response.Charset = "";
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            gv.RenderControl(htw);
            Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();

        }
        return View();
    }

1 个解决方案

#1


1  

        public ActionResult Export(string QCode = "")
    {

        if (QCode != "")
        {
            GridView gv = new GridView() { AutoGenerateColumns = true };
            string strSQL = "GetExcelExtract '" + QCode + "'";
            DataSet DS = new DataSet();
            SqlConnection SQLConn = new SqlConnection(System.Web.Configuration.WebConfigurationManager.AppSettings["ConnectionString"]);
            SqlCommand SQLCmd = new SqlCommand(strSQL, SQLConn);
            SQLCmd.CommandTimeout = 90;
            SqlDataAdapter SQlAdpt = new SqlDataAdapter();
            SQlAdpt.SelectCommand = SQLCmd;
            SQLConn.Open();
            SQlAdpt.Fill(DS);
            SQLConn.Close();


            gv.DataSource = DS;
            gv.DataBind();
            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment; filename=ExportQuestion_" + QCode + ".xls");
            Response.ContentEncoding = System.Text.Encoding.Unicode;
            Response.ContentType = "application/ms-excel";
            Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());
            Response.Charset = "";
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            gv.RenderControl(htw);
            Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();

        }
        return View();
    }

I got it in the end - hope it helps!

我最后得到了它——希望它能有所帮助!

#1


1  

        public ActionResult Export(string QCode = "")
    {

        if (QCode != "")
        {
            GridView gv = new GridView() { AutoGenerateColumns = true };
            string strSQL = "GetExcelExtract '" + QCode + "'";
            DataSet DS = new DataSet();
            SqlConnection SQLConn = new SqlConnection(System.Web.Configuration.WebConfigurationManager.AppSettings["ConnectionString"]);
            SqlCommand SQLCmd = new SqlCommand(strSQL, SQLConn);
            SQLCmd.CommandTimeout = 90;
            SqlDataAdapter SQlAdpt = new SqlDataAdapter();
            SQlAdpt.SelectCommand = SQLCmd;
            SQLConn.Open();
            SQlAdpt.Fill(DS);
            SQLConn.Close();


            gv.DataSource = DS;
            gv.DataBind();
            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment; filename=ExportQuestion_" + QCode + ".xls");
            Response.ContentEncoding = System.Text.Encoding.Unicode;
            Response.ContentType = "application/ms-excel";
            Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());
            Response.Charset = "";
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            gv.RenderControl(htw);
            Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();

        }
        return View();
    }

I got it in the end - hope it helps!

我最后得到了它——希望它能有所帮助!