C#.net中转换成html页

Asp.net生成静态html文件
内容 一 表结构:

CREATE TABLE [dbo].[HtmlNews] (
 [Id] [int] IDENTITY (1, 1) NOT NULL ,
 [subject] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
 [filename] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
 [Addtime] [datetime] NULL
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[HtmlNews] ADD
 CONSTRAINT [DF_HtmlNews_Addtime] DEFAULT (getdate()) FOR [Addtime]
GO

二 WebForm1.aspx <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="htmlNews.WebForm1" %>


<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="htmlNews.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>WebForm1</title>
  <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
  <meta name="CODE_LANGUAGE" Content="C#">
  <meta name="vs_defaultClientScript" content="JavaScript">
  <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
 </HEAD>
 <body MS_POSITIONING="GridLayout">
  <form id="Form1" method="post" runat=server">
   <FONT face="宋体">
    <asp:TextBox id="subject" style="Z-INDEX: 101; LEFT: 168px; POSITION: absolute; TOP: 112px" runat=server"></asp:TextBox>
    <asp:Button id="Button1" style="Z-INDEX: 102; LEFT: 168px; POSITION: absolute; TOP: 176px" runat=server"
     Text="Button"></asp:Button>
    <asp:RequiredFieldValidator id="RequiredFieldValidator1" style="Z-INDEX: 103; LEFT: 384px; POSITION: absolute; TOP: 120px"
     runat=server" ErrorMessage="*" ControlToValidate="subject"></asp:RequiredFieldValidator></FONT>
  </form>
 </body>
</HTML>


三 WebForm1.aspx.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace htmlNews
{
 /// <summary>
 /// WebForm1 的摘要说明。
 /// </summary>
 public class WebForm1 : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.TextBox subject;
  protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
  protected System.Web.UI.WebControls.Button Button1;
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
  }

  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
 
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.Button1.Click += new System.EventHandler(this.Button1_Click);
   this.Load += new System.EventHandler(this.Page_Load);  }
  #endregion

  public void Button1_Click(object sender, System.EventArgs e)
  {
   string s=subject.Text.Trim().ToString();
   cars cf=new cars();
   string newfilename=cf.new_file()+".html";

   string sql="insert into htmlnews(subject,filename) values('"+s+"','"+newfilename+"')";  
  
   SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
   conn.Open(); 
  
   SqlCommand cmd=new SqlCommand(sql,conn);
   cmd.ExecuteNonQuery();
   conn.Close();

    if(cf.WriteFile(s,newfilename))
    {
     Response.Write("添加成功");
    }
    else
    {
     Response.Write("生成HTML出错!");
    }

 }
 }
}
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="htmlNews.WebForm1" %>

 

cars.cs

using System;
using System.IO;
using System.Web;
using System.Text;

namespace htmlNews
{
 /// <summary>
 /// cars 的摘要说明。
 /// </summary>
 public class cars
 {
  public cars()
  {
   //
   // TODO: 在此处添加构造函数逻辑
   //
  }

  public bool WriteFile(string strText,string fn)
  {
   string path = HttpContext.Current.Server.MapPath("news/");
   Encoding code = Encoding.GetEncoding("gb2312");
   // 读取模板文件
   string temp = HttpContext.Current.Server.MapPath("type.htm");
   StreamReader sr=null;
   StreamWriter sw=null;
   string str=""; 
   try
   {
    sr = new StreamReader(temp, code);
    str = sr.ReadToEnd(); // 读取文件
   }
   catch(Exception exp)
   {
    HttpContext.Current.Response.Write(exp.Message);
    HttpContext.Current.Response.End();
    sr.Close();
   }
 
  
   string htmlfilename=fn;
   // 替换内容
   // 这时,模板文件已经读入到名称为str的变量中了
   str =str.Replace("$subject$",strText); //模板页中的ShowArticle
  
   // 写文件
   try
   {
    sw = new StreamWriter(path + htmlfilename , false, code);
    sw.Write(str);
    sw.Flush();
   }
   catch(Exception ex)
   {
    HttpContext.Current.Response.Write(ex.Message);
    HttpContext.Current.Response.End();
   }
   finally
   {
    sw.Close();
   }
   return true;
  }

  //文件名称名称,年月日时分秒,随机数
  public string new_file()
  {
   string newfile="";
   Random a=new Random();
   newfile=DateTime.Now.ToString("yyyyMMddhhmmss") + DateTime.Now.Millisecond.ToString()+"_"+a.Next(1,100000).ToString();
   return newfile;
  } }
}
 

 


模板文件忘了贴出来

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
 <head>
  <title></title>
  <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
  <meta name="ProgId" content="VisualStudio.HTML">
  <meta name="Originator" content="Microsoft Visual Studio .NET 7.1">
 </head>
 <body>
  <FONT face="宋体">
   <TABLE id="Table1" cellSpacing="1" cellPadding="1" width="500" border="1">
    <TR>
     <TD width="293">$subject$</TD>    
    </TR>
   </TABLE>
  </FONT>
 </body>
</html>

你可能感兴趣的:(html)