.Net에서 DataSet을 excel로 내보내는 방법

5333 단어 Excel
코드를 남겨서 나중에 잊어버리지 않게.
protected void Export_Click(object sender, EventArgs e)

        {

            DataSet data = ""      //    



            DataTable dt = data.Tables[0];

            DataRow[] myrow = dt.Select();





            Response.Clear();

            Response.ContentType = "application/vnd.ms-excel";

            Response.Charset = "gb2312";

            Response.ContentEncoding = Encoding.GetEncoding("gb2312");          //                 

            Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("       ") + ".xls"));

            int cl = dt.Columns.Count;

            string colhead="";     //   

            string item = "";        // 



            for (int i = 0; i < cl; i++)

            {

                if (i == cl - 1)

                {

                    colhead +=dt.Columns[i].Caption.ToString()+"
"; } else { colhead += dt.Columns[i].Caption.ToString() + "\t"; } } Response.Write(colhead); foreach (DataRow row in myrow) { for (int i = 0; i < cl; i++) { if (i == cl - 1) { item += row[i].ToString() + "
"; } else { item += row[i].ToString() + "\t"; } } Response.Write(item); item = ""; } //Response.Write("<meta http-equiv=\"content-type\" content=\"application/ms-excel; charset=gb2312\"/>"); // Response.End(); }

좋은 웹페이지 즐겨찾기