且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将大型gridview数据导出到Excel工作表中

更新时间:2023-11-30 19:57:16

嗨raheem,

尝试这可能对你有用。



Hi raheem,
Try this may it work for you.

string FileName = "StudentAttendanceReport";
            // DataTable dt = ds.Tables[0];
            Context.Response.Write("<table border=1 width='100%'><tr align = 'center'>");
            for (int i = 0; i < dt.Columns.Count; i++)

            {

                Context.Response.Write("<td><B>" + dt.Columns[i].ToString() + "</B></td>");

            }

            Context.Response.Write("</tr>");

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

            {

                DataRow dr = dt.Rows[i];

                Context.Response.Write("<tr align = 'Left'>");
                for (int iCol = 0; iCol < dt.Columns.Count; iCol++)

                {

                    switch (dr[iCol].ToString())

                    {

                        case "/":

                            Context.Response.Write("<td nowrap>" + "" + "</td>");
                            break;
                        default:
                            Context.Response.Write("<td nowrap>" + dr[iCol].ToString() + "</td>");
                            break;
                    }

                }
                Context.Response.Write("</tr>");
            }
            Context.Response.Write("</table>");

            Context.Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName + ".xls");
            Context.Response.ContentType = "application/vnd.ms-excel";
            Context.Response.Charset = "";
            Context.Response.Flush();
            Context.Response.Close();
            Context.Response.End();
        }
        catch (Exception)
        {

            // throw;
        }


由于您有大量数据,因此可以在获取时增加命令对象的超时时间: -



As there is a large amount of data for you you can increase the timeout time for your command object while fetching :-

SqlCommand cmd = new SqlCommand("your query", {connection object});
cmd.CommandTimeout = 999999 ;





希望这会为你工作。



Hope this will work for you.