iT邦幫忙

2018 iT 邦幫忙鐵人賽
DAY 25
0

外話

目前治療停滯
意外發現我的血管異常
薄弱
正常人打點滴一針
靜脈約能撐4天
我只能2天,而且痛
所以意識有點薄弱

        protected void BExport_Click(object sender, EventArgs e)
        {
            
                string filename = "ExcelFile";
                string title = "ExcelTitle";

                string html = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=big5\">"
                             + "<title>Excel</title></head>" + title + "<br>";
                html += "<table border=\"1\">";

                //表頭
                html += "<tr>";
                for (int i = 0; i < GridView1.Columns.Count; i++)
                {
                    if (!GridView1.Columns[i].Visible)
                    {
                        continue;
                    }
                    html += "<td bgcolor=\"#E4E4E4\">" + GridView1.HeaderRow.Cells[i].Text;
                }

                html += "</tr>";

                //先把分頁關掉
                GridView1.AllowPaging = false;

                //內容
                for (int i = 0; i < GridView1.Rows.Count; i++)
                {
                    html += "<tr>";
                    for (int c = 0; c < GridView1.Columns.Count; c++)
                    {
                        if (!GridView1.Columns[c].Visible)
                        {
                            continue;
                        }
                        if (GridView1.Rows[i].Cells[c].Controls.Count > 0)
                        {
                        //html += "<td>" + ((Label)gv.Rows[i].Cells[c].Controls[1]).Text + "</td>";
                        html += "<td>" + GridView1.Rows[i].Cells[c].Text + "</td>";
                        }
                        else
                        {
                            html += "<td>" + GridView1.Rows[i].Cells[c].Text + "</td>";
                        }

                    }
                    html += "</tr>";
                }
                html += "</table>";

                GridView1.AllowPaging = true;


                string sContentDisposition = "";
                sContentDisposition += "attachment; "; // 強制存檔,未設定則依瀏覽器預設開啟或存檔
                sContentDisposition += "filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8) + ".xls"; //設定檔名可為中文_#1

                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.AddHeader("Content-disposition", sContentDisposition);
                HttpContext.Current.Response.ContentType = "application/vnd.ms-excle";
                HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;

                //utt8 to ascii
                Byte[] bytes = System.Text.Encoding.Unicode.GetBytes(html);
                Byte[] outByte = System.Text.Encoding.Convert(System.Text.Encoding.Unicode, System.Text.Encoding.Default, bytes);

                HttpContext.Current.Response.BinaryWrite(outByte);
                HttpContext.Current.Response.End();
        }

上面程式碼基本可以直接使用
主要就擷取gridview內容
放置於html的table中
若懂得運作
listview也應當可轉為excel

參考

https://dotblogs.com.tw/shunnien/2013/04/11/101414


上一篇
Day24-買套版
下一篇
Day26-修改項目清單
系列文
專案學習的30天30

尚未有邦友留言

立即登入留言