自己写了一个生成静态页的详细页和列表页的代码

using System;
using System.Data;
using System.Configuration;
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 zj123.Model;
using System.IO;
using System.Text.RegularExpressions;
namespace WebUI.html
{
public class EasyHtml
{
public bool MakeHtml( string artid)
{
bool succ = false ;
// 读取数据
string sql = string .Format( " selecta.*,b.ClassName,b.Depth,b.OrderBy,b.isTop,b.isList,b.IsLinks,b.Links,b.PicURL,b.ParentID,b.TemplateoutId,b.TemplateinNeiId,b.TemplateinListIdfromarticlea,zonebwherea.classid=b.classidanda.articleid={0} " ,artid);
DataTabledt
= DbHelperOleDb.Query(sql).Tables[ 0 ];
if (dt.Rows.Count > 0 )
{
// 外模板
string waitemp = "" ;
DataTablewaidt
= DbHelperOleDb.Query( string .Format( " select*fromtemplatewhereTemplateID={0} " ,Convert.ToString(dt.Rows[ 0 ][ " TemplateoutId " ]).Trim())).Tables[ 0 ];
if (waidt.Rows.Count > 0 )
{
waitemp
= Convert.ToString(waidt.Rows[ 0 ][ " TemplateContent " ]);
}
// 内模板
string neitemp = "" ;
DataTableneidt
= DbHelperOleDb.Query( string .Format( " select*fromtemplatewhereTemplateID={0} " ,Convert.ToString(dt.Rows[ 0 ][ " TemplateinNeiId " ]).Trim())).Tables[ 0 ];
if (neidt.Rows.Count > 0 )
{
neitemp
= Convert.ToString(neidt.Rows[ 0 ][ " TemplateContent " ]);
}
// 替换模板
waitemp = waitemp.Replace( " $intemplate$ " ,neitemp);
// 替换类型
waitemp = waitemp.Replace( " $ClassName$ " ,classes.Common.GetP(Convert.ToString(dt.Rows[ 0 ][ " classid " ])));
#region "文章列表"

// 最新的文章
System.Text.StringBuildersbneartitle = new System.Text.StringBuilder( " <div> " );
DataTablenearTitledt
= DbHelperOleDb.Query( " selecttop10casewhenlen(title)>20thensubstring(title,1,20)elsetitleendasTitle,HtmlPathfromarticlewhereiscreate=1orderbyupdatetimedesc " ).Tables[ 0 ];
for ( int i = 0 ;i < nearTitledt.Rows.Count;i ++ )
{
sbneartitle.AppendFormat(
" <li><ahref="{0}">{1}</a></li> " ,Convert.ToString(nearTitledt.Rows[i][ " HtmlPath " ]),Convert.ToString(nearTitledt.Rows[i][ " Title " ]));
}
sbneartitle.Append(
" </ol></div> " );
waitemp
= waitemp.Replace( " $NearTitle$ " ,sbneartitle.ToString());
sbneartitle.Remove(
0 ,sbneartitle.ToString().Length);
sbneartitle.Append(
" <div> " );

// 固定
nearTitledt = DbHelperOleDb.Query( " selecttop10casewhenlen(title)>20thensubstring(title,1,20)elsetitleendasTitle,HtmlPathfromarticlewhereiscreate=1andOnTop=1orderbyupdatetimedesc " ).Tables[ 0 ];
for ( int i = 0 ;i < nearTitledt.Rows.Count;i ++ )
{
sbneartitle.AppendFormat(
" <li><ahref="{0}">{1}</a></li> " ,Convert.ToString(nearTitledt.Rows[i][ " HtmlPath " ]),Convert.ToString(nearTitledt.Rows[i][ " Title " ]));
}
sbneartitle.Remove(
0 ,sbneartitle.ToString().Length);
sbneartitle.Append(
" </ol></div> " );
waitemp
= waitemp.Replace( " $OnTopTitle$ " ,sbneartitle.ToString());
sbneartitle.Remove(
0 ,sbneartitle.ToString().Length);
sbneartitle.Append(
" <div> " );
// 热门
nearTitledt = DbHelperOleDb.Query( " selecttop10casewhenlen(title)>20thensubstring(title,1,20)elsetitleendasTitle,HtmlPathfromarticlewhereiscreate=1andHits>1000orderbyHitsdesc,updatetimedesc " ).Tables[ 0 ];
for ( int i = 0 ;i < nearTitledt.Rows.Count;i ++ )
{
sbneartitle.AppendFormat(
" <li><ahref="{0}">{1}</a></li> " ,Convert.ToString(nearTitledt.Rows[i][ " HtmlPath " ]),Convert.ToString(nearTitledt.Rows[i][ " Title " ]));
}
sbneartitle.Append(
" </ol></div> " );


waitemp
= waitemp.Replace( " $HitsTitle$ " ,sbneartitle.ToString());
sbneartitle.Remove(
0 ,sbneartitle.ToString().Length);
sbneartitle.Append(
" <div> " );
// 推荐Elite
nearTitledt = DbHelperOleDb.Query( " selecttop10casewhenlen(title)>20thensubstring(title,1,20)elsetitleendasTitle,HtmlPathfromarticlewhereiscreate=1andElite=1orderbyupdatetimedesc " ).Tables[ 0 ];
for ( int i = 0 ;i < nearTitledt.Rows.Count;i ++ )
{
sbneartitle.AppendFormat(
" <li><ahref="{0}">{1}</a></li> " ,Convert.ToString(nearTitledt.Rows[i][ " HtmlPath " ]),Convert.ToString(nearTitledt.Rows[i][ " Title " ]));
}
sbneartitle.Remove(
0 ,sbneartitle.ToString().Length);
sbneartitle.Append(
" </ol></div> " );
waitemp
= waitemp.Replace( " $Elite$ " ,sbneartitle.ToString());

/////////////////////////////////////////////////////////////////////////////////////////////
#endregion
// 替换最新的数据
for ( int j = 0 ;j < dt.Columns.Count;j ++ )
{
if (dt.Columns[j].ColumnName.Trim() == " Title " )
{
string temp1 = "" ;
string temp2 = "" ;
string typefont = Convert.ToString(dt.Rows[ 0 ][ " TitleFontType " ]);
if (typefont == " 0 " )
{
temp1
= " <strong> " ;
temp2
= " </strong> " ;
}
else if (typefont == " 1 " )
{
temp1
= " <em> " ;
temp2
= " </em> " ;
}
else if (typefont == " 2 " )
{
temp1
= " <strong><em> " ;
temp2
= " </strong></em> " ;
}
else
{

}
// System.Text.RegularExpressions.Regexreg=newSystem.Text.RegularExpressions.Regex();
// Matchmatch=Regex.Matches();
// reg.Replace(waitemp,Convert.ToString(dt.Rows[0][dt.Columns[j].ColumnName.Trim()]),1,waitemp.IndexOf("$Title$"));
string temp11 = waitemp.Substring( 0 ,waitemp.IndexOf( " $Title$ " ) + 8 );
string temp22 = waitemp.Substring(waitemp.IndexOf( " $Title$ " ) + 8 );
temp11
= temp11.Replace( " $Title$ " ,Convert.ToString(dt.Rows[ 0 ][dt.Columns[j].ColumnName.Trim()]));
temp22
= temp22.Replace( " $Title$ " , " <fontcolor=' " + Convert.ToString(dt.Rows[ 0 ][ " TitleFontColor " ]) + " '> " + temp1 + Convert.ToString(dt.Rows[ 0 ][dt.Columns[j].ColumnName.Trim()]) + temp2 + " </font> " );
waitemp
= temp11 + temp22;
temp11
= null ;
temp22
= null ;
// waitemp=waitemp.Replace("$"+dt.Columns[j].ColumnName.Trim()+"$","<fontcolor='"+Convert.ToString(dt.Rows[0]["TitleFontColor"])+"'>"+temp1+Convert.ToString(dt.Rows[0][dt.Columns[j].ColumnName.Trim()])+temp2+"</font>");
}
else
{
waitemp
= waitemp.Replace( " $ " + dt.Columns[j].ColumnName.Trim() + " $ " ,Convert.ToString(dt.Rows[ 0 ][dt.Columns[j].ColumnName.Trim()]));
}

}
// 替换链表信息
// $typejs$
waitemp = waitemp.Replace( " $typejs$ " ,Convert.ToString(dt.Rows[ 0 ][ " ClassID " ]));
// $numjs$
waitemp = waitemp.Replace( " $numjs$ " , " 10 " );
// $setjs$
waitemp = waitemp.Replace( " $setjs$ " , "" );

/// js脚本信息


// 根据路径生成页面
// 路径检查
string path = System.Web.HttpContext.Current.Request.PhysicalApplicationPath.Trim() + " \ " + Convert.ToString(dt.Rows[ 0 ][ " Links " ]);
if ( ! Directory.Exists(path))
Directory.CreateDirectory(path);
string filename = " article_ " + Convert.ToString(dt.Rows[ 0 ][ " ArticleID " ]) + " .html " ;
using (StreamWritersw = new StreamWriter(path + " \ " + filename, false ,System.Text.Encoding.GetEncoding( " gb2312 " )))
{
sw.Write(waitemp);
sw.Flush();
sw.Close();
}
// 修改文章转台
try
{
if (artid == null )
return false ;
int j = int .Parse(artid);
}
catch
{
return false ;
}
// if(Convert.ToString(dt.Rows[0]["htmlpath"]).Trim().Length>0)
// {
// try
// {
// File.Delete(HttpContext.Current.Request.PhysicalApplicationPath+Convert.ToString(dt.Rows[0]["htmlpath"]).Trim().Replace("/","\"));
// }
// catch(Exceptionex)
// {


// }
// }
string sql1 = string .Format( " updatearticlesetiscreate=1,htmlpath='{0}'wherearticleid={1} " , " / " + Convert.ToString(dt.Rows[ 0 ][ " Links " ]).Replace( " \\ " , " / " ) + " / " + filename,artid);
DbHelperOleDb.ExecuteSql(sql1);

}
else
{
return false ;
}


return succ;
}
public bool MakeList( string classid)
{
bool succ = false ;
try
{
int i = int .Parse(classid);

}
catch (Exceptionex)
{

return false ;
}
DataTabledt
= DbHelperOleDb.Query( string .Format( " selecta.*,b.ClassName,b.Depth,b.OrderBy,b.isTop,b.isList,b.IsLinks,b.Links,b.PicURL,b.ParentID,b.TemplateoutId,b.TemplateinNeiId,b.TemplateinListIdfromarticlea,zonebwherea.classid=b.classidandiscreate=1anda.classid={0} " ,classid)).Tables[ 0 ];
string classpath = HttpContext.Current.Request.PhysicalApplicationPath + " \ " + Convert.ToString(dt.Rows[ 0 ][ " Links " ]).Trim();
// 读取外模板信息
string templatewai = "" ;
DataTabletemplatewaidt
= DbHelperOleDb.Query( string .Format( " select*fromtemplatewhereTemplateID={0} " ,Convert.ToString(dt.Rows[ 0 ][ " TemplateoutId " ]))).Tables[ 0 ];
if (templatewaidt.Rows.Count > 0 )
{
templatewai
= Convert.ToString(templatewaidt.Rows[ 0 ][ " TemplateContent " ]);
}
// 根据类型替换标题

templatewai
= templatewai.Replace( " $Title$ " ,Convert.ToString(dt.Rows[ 0 ][ " ClassName " ]).Trim());

templatewai
= templatewai.Replace( " $ClassName$ " ,classes.Common.GetP(classid));

int zong = dt.Rows.Count;
int size = 10 ;
try
{
size
= Convert.ToInt16(System.Configuration.ConfigurationManager.AppSettings[ " pagesize " ]);
}
catch (Exceptionex)
{

size
= 10 ;
}
int pagecount = zong % size == 0 ? (zong / 10 ):(zong / 10 + 1 );
string listcontent = "" ;
DataTabledtlist
= DbHelperOleDb.Query( string .Format( " select*fromtemplatewhereTemplateID={0} " ,Convert.ToString(dt.Rows[ 0 ][ " TemplateinListId " ]))).Tables[ 0 ];
if (dtlist.Rows.Count > 0 )
{
listcontent
= Convert.ToString(dtlist.Rows[ 0 ][ " TemplateContent " ]);
}

// 循环生成分页
for ( int i = 0 ;i < pagecount;i ++ )
{
System.Text.StringBuildersbneilist
= new System.Text.StringBuilder( " <TABLEcellSpacing=0cellPadding=0width=760border=0><TBODY> " );



for ( int j = (i * size < zong) ? (i * size):(zong);j < ((i + 1 ) * size < zong ? ((i + 1 ) * size):(zong));j ++ )
{
sbneilist.Append(
" <TR><TDvAlign=topalign=middlebgColor=#f7f7f7> " );
string temp = listcontent;
for ( int k = 0 ;k < dt.Columns.Count;k ++ )
{

temp
= temp.Replace( " $ " + dt.Columns[k].ColumnName.Trim() + " $ " ,Convert.ToString(dt.Rows[j][dt.Columns[k].ColumnName.Trim()]));
}
sbneilist.Append(temp);
sbneilist.Append(
" </td></tr> " );

}

sbneilist.Append(
" <TR><TDheight=36> " ).Append(classes.Common.GetPage(i + 1 ,size,zong)).Append( " </td></tr> " );
sbneilist.Append(
" </tbody></table> " );
#region "文章列表"

// 最新的文章
System.Text.StringBuildersbneartitle = new System.Text.StringBuilder( " <div> " );
DataTablenearTitledt
= DbHelperOleDb.Query( " selecttop10casewhenlen(title)>20thensubstring(title,1,20)elsetitleendasTitle,HtmlPathfromarticlewhereiscreate=1orderbyupdatetimedesc " ).Tables[ 0 ];
for ( int curr = 0 ;curr < nearTitledt.Rows.Count;curr ++ )
{
sbneartitle.AppendFormat(
" <li><ahref="{0}">{1}</a></li> " ,Convert.ToString(nearTitledt.Rows[curr][ " HtmlPath " ]),Convert.ToString(nearTitledt.Rows[curr][ " Title " ]));
}
sbneartitle.Append(
" </ol></div> " );
templatewai
= templatewai.Replace( " $NearTitle$ " ,sbneartitle.ToString());
sbneartitle.Remove(
0 ,sbneartitle.ToString().Length);
sbneartitle.Append(
" <div> " );

// 固定
nearTitledt = DbHelperOleDb.Query( " selecttop10casewhenlen(title)>20thensubstring(title,1,20)elsetitleendasTitle,HtmlPathfromarticlewhereiscreate=1andOnTop=1orderbyupdatetimedesc " ).Tables[ 0 ];
for ( int curr = 0 ;curr < nearTitledt.Rows.Count;curr ++ )
{
sbneartitle.AppendFormat(
" <li><ahref="{0}">{1}</a></li> " ,Convert.ToString(nearTitledt.Rows[curr][ " HtmlPath " ]),Convert.ToString(nearTitledt.Rows[curr][ " Title " ]));
}
sbneartitle.Remove(
0 ,sbneartitle.ToString().Length);
sbneartitle.Append(
" </ol></div> " );
templatewai
= templatewai.Replace( " $OnTopTitle$ " ,sbneartitle.ToString());
sbneartitle.Remove(
0 ,sbneartitle.ToString().Length);
sbneartitle.Append(
" <div> " );
// 热门
nearTitledt = DbHelperOleDb.Query( " selecttop10casewhenlen(title)>20thensubstring(title,1,20)elsetitleendasTitle,HtmlPathfromarticlewhereiscreate=1andHits>1000orderbyHitsdesc,updatetimedesc " ).Tables[ 0 ];
for ( int curr = 0 ;curr < nearTitledt.Rows.Count;curr ++ )
{
sbneartitle.AppendFormat(
" <li><ahref="{0}">{1}</a></li> " ,Convert.ToString(nearTitledt.Rows[curr][ " HtmlPath " ]),Convert.ToString(nearTitledt.Rows[curr][ " Title " ]));
}
sbneartitle.Append(
" </ol></div> " );


templatewai
= templatewai.Replace( " $HitsTitle$ " ,sbneartitle.ToString());
sbneartitle.Remove(
0 ,sbneartitle.ToString().Length);
sbneartitle.Append(
" <div> " );
// 推荐Elite
nearTitledt = DbHelperOleDb.Query( " selecttop10casewhenlen(title)>20thensubstring(title,1,20)elsetitleendasTitle,HtmlPathfromarticlewhereiscreate=1andElite=1orderbyupdatetimedesc " ).Tables[ 0 ];
for ( int curr = 0 ;curr < nearTitledt.Rows.Count;curr ++ )
{
sbneartitle.AppendFormat(
" <li><ahref="{0}">{1}</a></li> " ,Convert.ToString(nearTitledt.Rows[curr][ " HtmlPath " ]),Convert.ToString(nearTitledt.Rows[curr][ " Title " ]));
}
sbneartitle.Remove(
0 ,sbneartitle.ToString().Length);
sbneartitle.Append(
" </ol></div> " );
templatewai
= templatewai.Replace( " $Elite$ " ,sbneartitle.ToString());

/////////////////////////////////////////////////////////////////////////////////////////////
#endregion
string tempzong = templatewai.Replace( " $intemplate$ " ,sbneilist.ToString());
string indexshow = (i + 1 ).ToString() == " 1 " ? ( "" ):((i + 1 ).ToString());
using (StreamWritersw = new StreamWriter(classpath + " \index " + indexshow + " .html " , false ,System.Text.Encoding.GetEncoding( " gb2312 " )))
{
sw.Write(tempzong);
sw.Flush();
sw.Close();
};
}<br
分享到:
评论

你可能感兴趣的:(sql,Web,UI,脚本,J#)