aspx 動態產生html(二)

來源:互聯網
上載者:User

第一種方法:直接轉化一個頁面的方法

 public static  bool CreateList(string url, string fna)    {        bool ok;        //準備產生        string strHtml;        StreamReader sr = null; //用來讀取流        StreamWriter sw = null; //用來寫檔案        Encoding code = Encoding.GetEncoding("utf-8"); //定義編碼         //構造web請求,發送請求,擷取響應        WebRequest HttpWebRequest = null;        WebResponse HttpWebResponse = null;        HttpWebRequest = WebRequest.Create(url);        HttpWebResponse = HttpWebRequest.GetResponse();        //獲得流        sr = new StreamReader(HttpWebResponse.GetResponseStream(), code);        strHtml = sr.ReadToEnd();        //寫入檔案        try        {            sw = new StreamWriter(fna, false, code);            sw.Write(strHtml);            sw.Flush();            ok = true;        }        catch (Exception ex)        {            HttpContext.Current.Response.Write("<p>寫入檔案出錯:" + ex.Message);            HttpContext.Current.Response.End();            ok = false;        }        finally        {            sw.Close();        }        return ok;    }

調用

        string url = @"http://localhost:15598/OA/PublicGV.aspx?id=14";        //html分頁檔名        string fna = Server.MapPath("html") + "\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + DateTime.Now.Millisecond.ToString() + ".html";        if (aspxtohtml.CreateList(url, fna))        {            Response.Write("<p>組建檔案成功:" + fna);        }

第二種方法是用一個html模板產生一個html頁面,模版裡面有對應的標籤,可以從資料庫和別的地方取資料,填寫這個標籤,產生一個html頁面,這個方法在很多新聞系統裡有用到

private string CreateDetailPage(string EventID,string EventTitle, string EventBody, string EventTime, string EventStat)    {        //模版所有路徑、模版檔案名稱、產生的檔案路徑、產生的檔案名稱        string path, temp, htmlfilepath,htmlfilename;        path = Server.MapPath("");        temp = Server.MapPath("testhtml.htm");        htmlfilepath = path;        htmlfilename = DateTime.Now.ToString("yyyyMMddHHmmss") + DateTime.Now.Millisecond.ToString() + ".html";        //讀模版        Encoding code = Encoding.GetEncoding("gb2312");        StreamReader sr = null;        StreamWriter sw = null;        string str = "";        try        {            sr = new StreamReader(temp, code);            str = sr.ReadToEnd(); // 讀取檔案        }        catch (Exception exp)        {            HttpContext.Current.Response.Write("<p>讀取檔案出錯:" + exp.Message);            HttpContext.Current.Response.End();            sr.Close();        }        // 替換內容        // 對應模版裡的設定要修改        str = str.Replace("re_symbol_EventID", EventID);        str = str.Replace("re_symbol_EventTitle", EventTitle);        str = str.Replace("re_symbol_EventBody", EventBody);        str = str.Replace("re_symbol_EventTime", EventTime);        str = str.Replace("re_symbol_EventStat", EventStat);        // 寫檔案        try        {            sw = new StreamWriter(htmlfilepath + "//" + htmlfilename, false, code);            sw.Write(str);            sw.Flush();        }        catch (Exception ex)        {            HttpContext.Current.Response.Write("<p>寫入檔案出錯:" + ex.Message);            HttpContext.Current.Response.End();        }        finally        {            sw.Close();        }        return htmlfilename;    }

調用的時候這樣:

   int i;        i = GridView1.SelectedIndex;        if (i == null || i==-1) i = 0;        string EventID, EventTitle, EventBody, EventTime, EventStat;        EventID=GridView1.Rows[i].Cells[0].Text;        EventTitle=GridView1.Rows[i].Cells[1].Text;        EventBody=GridView1.Rows[i].Cells[2].Text;        EventTime=GridView1.Rows[i].Cells[3].Text;        EventStat=GridView1.Rows[i].Cells[4].Text;                //組建檔案,返迴文件名        string fna;        fna=CreateDetailPage(EventID, EventTitle, EventBody, EventTime, EventStat);        Response.Write("<p>組建檔案成功:" + fna);

上個圖吧

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.