蓝派网(www.lan27.com)-精选网络资源,分享和交流! 文章首页站内搜索在线手册广告代码酷站欣赏万年历
您现在的位置: 蓝派网 >> 文章中心 >> 网络编程 >> .NET >> ASP.NET >> 正文

高速从DataSet导出到Excel的方案

作者:佚名    文章来源:网络    更新时间:2008-10-29 18:57:42

//   将当前查询的数据导入Excel中  
        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["sqlcon"]);

        //   设置为true,则在当前的数据上追加数据,否则,重写当前数据  
        StreamWriter w = new StreamWriter(Server.MapPath("数据库.xls"), false, Encoding.Default);
        con.Open();

        try
        {
            SqlCommand SqlCmd = con.CreateCommand();
            SqlCmd.CommandText = "select * from flysoblog";
            SqlDataReader Reader = SqlCmd.ExecuteReader();
            for (int i = 0; i < Reader.FieldCount; ++i)
            {
                w.Write(Reader.GetName(i));
                w.Write('\t');
            }
            w.Write("\r\n");
            object[] values = new object[Reader.FieldCount];
            while (Reader.Read())
            {
                Reader.GetValues(values);
                for (int i = 0; i < values.Length; ++i)
                {
                    w.Write(values[i].ToString());
                    w.Write('\t');
                }
                w.Write("\r\n");
            }
            w.Flush();
            w.Close();
            Reader.Close();
            con.Close();
            Response.Redirect("数据库.xls");
        }
        catch
        {
            w.Close();
            con.Close();
            return;
        }

发表评论】【打印此文】【关闭窗口】【点击数:
★好玩的休闲小游戏★