如何处理CSV出口无显示的GridView [英] How to process a CSV for exporting without displaying in gridview

查看:115
本文介绍了如何处理CSV出口无显示的GridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经建立了一个GridView控件来显示一个查询时选择从各个联赛球队的结果。然后我可以选择下载到CSV点击
当所有的结果都显示在GridView控件下载到CSV按钮看起来。这种行之有效。
它是在网格中显示这一点,但当时有被检索很多球队正在采取一段时间来加载。有时候,它会采取如此
长时间将开始网页会挂起。现在,我想绕过GridView控件的显示,只是能够点击
下载到CSV按钮,然后对结果进行下载。
presently,我试图注释掉code,显示在GridView但这
似乎总是打破code。另外我试图在GridView设置为可见=假,它不允许被显示在GridView,
但仍处理的结果(因此仍然经受网页挂如果在第gridview的数据集是超出一定规模的)。理想的情况下
我想griview被完全绕过,但结果是在CSV文件中查看。

I had built a gridview to display the results from a query that is selecting teams from a various leagues. I could then choose to "download to CSV" by clicking on a download to CSV button that appears when all the results are displayed in the gridview. This worked well. It was displaying this in a grid but when there were many teams being retrieved it was taking a while to load. Sometimes it would take so long it would start to the web page would hang. Now I want to bypass the displaying of the gridview and just be able to click the "download to CSV" button and for the results to be downloaded. Presently, I have tried to comment out the code to display the Gridview but this always seems to break the code. Alternatively I have tried to set the gridview to Visible="False" which does not allow the gridview to be shown, but still processes the results (so still is subjected to the webpage hanging if the dataset in th gridview is beyond a certain size). Ideally I want the griview to be bypassed completely but the results to be viewable in the CSV file.

推荐答案

如果您需要直接从 DB 这里下载是一个例子:(当你没有张贴任何code I要带一个例子,解释)

If you need to download directly from DB here is an example :(As you didnt post any code i'm taking an example and explaining)

protected void ExportExcel(object sender, EventArgs e)
{
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers"))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                using (DataTable dt = new DataTable())
                {
                    sda.Fill(dt);
                    using (XLWorkbook wb = new XLWorkbook())
                    {
                        wb.Worksheets.Add(dt, "Customers");

                        Response.Clear();
                        Response.Buffer = true;
                        Response.Charset = "";
                        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                        Response.AddHeader("content-disposition", "attachment;filename=SqlExport.xlsx");
                        using (MemoryStream MyMemoryStream = new MemoryStream())
                        {
                            wb.SaveAs(MyMemoryStream);
                            MyMemoryStream.WriteTo(Response.OutputStream);
                            Response.Flush();
                            Response.End();
                        }
                    }
                }
            }
        }
    }
}

这篇关于如何处理CSV出口无显示的GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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