通用动态生成静态HTML页方法


ContractedBlock.gif ExpandedBlockStart.gif 通用动态生成静态HTML页方法
ExpandedBlockStart.gifContractedBlock.gif        /**//// <summary>
InBlock.gif        
/// 动态生成HTML页--韩国际(2006-04-18)
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="format">**用于替换HTML模板的变量($htmlformat[*]),format数组中的第一个变量format[0]应该是生成的静态HTML页相对于图片目录Images的相对路径,如:"../"</param>
InBlock.gif        
/// <param name="templateUrl">调用HTML模板的URL + 模板名称,如:"Temp/Template.htm"</param>
InBlock.gif        
/// <param name="saveHtmlUrl">保存生成的HTML页的URL + HTML文件名称,如:"Temp/20060418.htm"</param>
InBlock.gif        
/// <param name="encodingName">读取HTML模板页和生成HTML页所使用的字符编码方式,如:"GB2312"</param>
ExpandedBlockEnd.gif        
/// <returns>布尔类型--True=生成成功,False=生成失败</returns>

None.gif        public bool WriteHtml(string[] format,string templateUrl,string saveHtmlUrl,string encodingName)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            
//---------------------读html模板页面到stringbuilder对象里---- 
InBlock.gif
            System.Text.StringBuilder htmltext=new System.Text.StringBuilder();
InBlock.gif            
try 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif                System.IO.StreamReader sr 
= new System.IO.StreamReader(Server.MapPath(templateUrl.Trim()),System.Text.Encoding.GetEncoding(encodingName));
InBlock.gif                
string line; 
InBlock.gif                
while ((line = sr.ReadLine()) != null
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif
InBlock.gif                      htmltext.Append(line);
ExpandedSubBlockEnd.gif                }
 
InBlock.gif                sr.Close(); 
ExpandedSubBlockEnd.gif            }
 
InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif                 System.Web.HttpContext.Current.Response.Write(
"<script>alert('读取HTML模板 "+templateUrl.Trim()+" 出错!');</script>"); 
ExpandedSubBlockEnd.gif            }
 
InBlock.gif            
//----------替换htm模板里的标记为传入的数组
InBlock.gif
            for(int i=0;i<format.Length;i++
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif                 htmltext.Replace(
"$htmlformat["+i+"]",format[i]); 
ExpandedSubBlockEnd.gif            }
    
InBlock.gif            
//----------生成htm文件------------------―― 
InBlock.gif
            try 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif                
string tmphtm=Server.MapPath(saveHtmlUrl);
InBlock.gif                     System.IO.StreamWriter sw 
= new System.IO.StreamWriter(tmphtm.Trim(),false,System.Text.Encoding.GetEncoding(encodingName)); 
InBlock.gif                     sw.WriteLine(htmltext); 
InBlock.gif                     sw.Flush(); 
InBlock.gif                     sw.Close(); 
InBlock.gif                
return true;
ExpandedSubBlockEnd.gif            }
 
InBlock.gif            
catch 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif                System.Web.HttpContext.Current.Response.Write(
"<script>alert('"+saveHtmlUrl.Trim()+" HTML文件生成失败!');</script>"); 
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif            }
 
ExpandedBlockEnd.gif        }

None.gif

转载于:https://www.cnblogs.com/hanguoji/archive/2006/04/18/378388.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值