Asp.net產生靜態頁面最簡單方法(源碼)

來源:互聯網
上載者:User

public string IssuanceHTML(string NC_Id_S)

{

string returnValue = "";

string htmlfilename=DateTime.Now.ToString("yyyyMMddHHmmss")+".html";

        SqlConnection conn = null;

        SqlDataReader sdr = null;

        try

        {

            string[] NC_Id_SS = NC_Id_S.Split(','); //大量操作時 Split分離資料

            for (int i = 0; i < NC_Id_SS.Length; i++)  //迴圈產生

            {conn = Connection.CreateDbConn();

             conn.Open();

             SqlCommand cmd = new SqlCommand();

cmd.Connection = conn;

    cmd.CommandText = "SELECT NC_Name_S, NC_Content_S FROM NewsContent WHERE NC_Id_S = '" + NC_Id_S + "'";

    sdr = cmd.ExecuteReader();

sdr.Read();

string path = HttpContext.Current.Server.MapPath("/HTML/"); //組建檔案路徑

    Encoding code = Encoding.GetEncoding("gb2312"); //編碼規則

    string temp = HttpContext.Current.Server.MapPath("/Templet/C8Article.html"); 模版檔案

    StreamReader sr = null;

    StreamWriter sw = null;

    string str = "";

    sr = new StreamReader(temp, code);

    str = sr.ReadToEnd();

    sr.Close();

    str = str.Replace("spagetitle", sdr.GetString(0).ToString());//spagetitle為模版檔案中內容

    str = str.Replace("scontent", sdr.GetString(1).ToString());

    sw = new StreamWriter(path + htmlfilename, false, code);

    sw.Write(str);

    sw.Flush();

    sw.Close();

    returnValue = "yes";

     }

        catch(Exception e)

        {  returnValue = e.Message.ToString();

        }

        finally

        {   sdr.Close();

            conn.Close();

        }

        return returnValue;

}

--------------------------------------------------------------------------------------------
另外一個代碼
--------------------------------------------------------------------------------------------
using   System;
using   System.Data;
using   System.Configuration;
using   System.Collections;
using   System.Web;
using   System.Web.Security;
using   System.Web.UI;
using   System.Web.UI.WebControls;
using   System.Web.UI.WebControls.WebParts;
using   System.Web.UI.HtmlControls;
using   Mysqlserver;
using   System.IO;
using   System.Text;
namespace   NewsAdd
{
        public   partial   class   Admin_AdminPanel_NewsAdd   :   System.Web.UI.Page
        {
                protected   void   Page_Load(object   sender,   EventArgs   e)
                {

                }
                protected   void   Button1_Click(object   sender,   EventArgs   e)
                {
                        string   strDate   =   DateTime.Now.ToString( "yyMMdd ")   +   "\ "   +   DateTime.Now.ToString( "yyyymmddhhmmss ");
                        string   strFileName   =   strDate   +   ".shtml ";
                        string   strTitle=Request.Form[ "Title "].ToString().Trim();
                        string   strContent=Request.Form[ "Content "].ToString().Trim();
                        string[]   content   =   strContent.Split(new   Char[]   ...{ ' ¦ '});
                        int   upbound   =   content.Length;
                        SqlServerDataBase   db   =   new   SqlServerDataBase();
                        bool   success   =   db.Insert( "insert   into   inNews(Title,Content,FilePath)values( ' "   +   strTitle   +   " ', ' "   +   strContent   +   " ', ' "   +   strFileName   +   " ') ",   null);

                        string   dir   =   Server.MapPath( "http://www.cnblogs.com/ "+ "NewsFiles/ "+DateTime.Now.ToString( "yyMMdd "));
                        if   (!Directory.Exists(dir))
                        {
                                Directory.CreateDirectory(dir);
                        }

                        try
                        {
                        for   (int   i   =   0;   i   <   content.Length;   i++)
                        {
                        //string[]   newContent   =   new   string[4];
                        StringBuilder   strhtml   =   new   StringBuilder();
                                using   (StreamReader   sr   =   new   StreamReader(Server.MapPath( "http://www.cnblogs.com/ "   +   "NewsFiles/ ")   +   "\template.html ",Encoding.GetEncoding( "gb2312 ")))
                                {
                                        String   oneline;
                                        while   ((oneline   =   sr.ReadLine())   !=   null)
                                        {
                                                strhtml.Append(oneline);
                                        }
                                        sr.Close();
                                }
                        DataSet   ds   =   db.Select( "select   top   1   NewsId   from   inNews   order   by   NewsId   desc ",   null);
                        string   strTable   =   " <table> <tr> <td> $upUrl </td> <td> $Number </td> <td> $downUrl </td> </tr> </table> ";
                                string   FilePath= " ";
                                strhtml   =   strhtml.Replace( "$Title ",   strTitle);
                                strhtml   =   strhtml.Replace( "$NewsId ",   ds.Tables[0].Rows[0][ "NewsId "].ToString());
                                strhtml   =   strhtml.Replace( "$Time ",   DateTime.Now.ToString( "yyyy/MM/dd "));
                                strhtml   =   strhtml.Replace( "$Content ",   content[i]);
                                string   strNumber   =   " ";
                                for   (int   m   =   1;   m   <=upbound;   m++)
                                {
                                        if   (m   ==   1)
                                                strNumber   =   strNumber   +   "   [ "+ " <a   href= "   +   "../ "   +   strDate   +   ".shtml "   +   "> "   +   m   +   " </a> "+ "]   ";
                                        else
                                        {
                                                int   n   =   m   -   1;
                                                strNumber   =   strNumber   +   "   [ "   + " <a   href= "   +   "../ "   +   strDate   +   "_ "   +   n   +   ".shtml "   +   "> "   +     m   +   " </a> "+ "]   ";
                                        }
                                }
                                        if   (upbound   ==   0)
                                        {
                                                FilePath   =   Server.MapPath( "http://www.cnblogs.com/ ")   +   "NewsFiles "   +   "// "   +   strDate   +   ".shtml ";
                                                strhtml   =   strhtml.Replace( "$Pager ",   " ");
                                        }
                                        else
                                        {
                                                if   (i   ==   0)
                                                        FilePath   =   Server.MapPath( "http://www.cnblogs.com/ ")   +   "NewsFiles "   +   "// "   +   strDate   +   ".shtml ";
                                                else
                                                        FilePath   =   Server.MapPath( "http://www.cnblogs.com/ ")   +   "NewsFiles "   +   "// "   +   strDate   +   "_ "   +   i   +   ".shtml ";
                                               
                                                if   (i   ==   0)
                                                        strTable   =   strTable.Replace( "$upUrl ",   " ");  

                                                if   (i   <=   1)//上一頁分頁
                                                        strTable   =   strTable.Replace( "$upUrl ",   " <a   href= "   +   "../ "   +   strDate   +   ".shtml "   +   "> 上一頁 </a> ");
                                                else
                                                {
                                                        int   p   =   i   -   1;
                                                        strTable   =   strTable.Replace( "$upUrl ",   " <a   href= "   +   "../ "   +   strDate   +   "_ "   +   p   +   ".shtml "   +   "> 上一頁 </a> ");
                                                }
                                               
                                                if(upbound==1)
                                                strTable   =   strTable.Replace( "$Number ",   " ");
                                                else
                                                strTable   =   strTable.Replace( "$Number ",   strNumber);
                                                if(i==upbound-1)
                                                        strTable   =   strTable.Replace( "$downUrl ",   " ");

                                                if   (i   !=   upbound   -   1)
                                                {
                                                        int   q   =   i   +   1;
                                                        strTable   =   strTable.Replace( "$downUrl ",   " <a   href= "   +   "../ "   +   strDate   +   "_ "   +   q   +   ".shtml "   +   "> 下一頁 </a> ");
                                                }
                                                else
                                                {
                                                        int   j   =   upbound   -   1;
                                                        strTable   =   strTable.Replace( "$downUrl ",   " <a   href= "   +   "../ "   +   strDate   +   "_ "   +   j   +   ".shtml "   +   "> 下一頁 </a> ");
                                                }

                                                strhtml   =   strhtml.Replace( "$Pager ",   strTable);
                                        }
                                FileInfo   finfo   =   new   FileInfo(FilePath);
                                using   (FileStream   fs   =   finfo.OpenWrite())
                                {
                                        StreamWriter   sw   =   new   StreamWriter(fs,   System.Text.Encoding.Default);
                                        sw.WriteLine(strhtml);
                                        sw.Flush();
                                        sw.Close();
                                }
                        }
                }
                catch   (Exception   err)
                {
                        Response.Write(err.ToString());
                }
                }
}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.