求一生成html文件的例子

joelbh 2003-12-28 04:55:47
在做一个新闻系统,动态生成html文件的,最好有源代码的例子
...全文
109 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
joelbh 2003-12-29
  • 打赏
  • 举报
回复
谢谢,我就是写文件那里不行,总调试不了
zsww 2003-12-29
  • 打赏
  • 举报
回复
学习!
  • 打赏
  • 举报
回复
可以的,你必须用一个模板文件,在要写内容的地方写你的关键字,然后用代码替换这些关键字,假设模板文件为text.html,关键字为A1A1A1
代码
C#
using System;
using System.Web;
using System.IO;
using System.Text;
// strText写入内容,strFileName是文件名
public static bool WriteFile(string strText,strign strFileName)
{
string path = HttpContext.Current.Server.MapPath("/news/");
Encoding code = Encoding.GetEncoding("gb2312");
// 读取模板文件
string temp = HttpContext.Current.Server.MapPath("/news/text.htm");
StreamReader sr;
StrteamWriter sw;
string str;
try
{
sr = new StreamReader(tmpFile, code);
str = sr.ReadToEnd(); // 读取文件
}
catch(Exception exp)
{
HttpContext.Current.Response.Write(exp.Message);
HttpContext.Current.Response.End();
}
finally
{
sr.Close();
}
// 这时,模板文件已经读入到名称为str的变量中了
// 替换内容
str = str.Replace("A1A1A1",strText);

// 写文件
try
{
sw = new StreamWriter(Path + strFileName, 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;
}
sagac 2003-12-28
  • 打赏
  • 举报
回复
please refer to the similar program.
一.静态HTML生成方法#coding:utf-8 _author_ = "LiaoPan" _time_ = "2016.6.16"f = open("demo_1.html",'w') message = """ <html>

Hello,World!

demo

html>"""f.write(messa

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧