C# 生成静态页面的分页程序
代码如下
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 System.Data.SqlClient;
using System.IO;
using System.Text;
public partial class Default4 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ //定义参数
StreamWriter sw = null;
string htmlfilename;
string path = HttpContext.Current.Server.MapPath("html/");
// 数据库生成分页
int onepage=10;
string ConnectionString = "连接字符串";
SqlConnection myConnection = new SqlConnection(ConnectionString);
string sSelect = "SELECT count(*) as pcount from 表名";
SqlCommand cmd = new SqlCommand(sSelect,myConnection);
myConnection.Open();
SqlDataReader Dr = cmd.ExecuteReader();
Dr.Read();
int Count=int.Parse(Dr["pcount"].ToString());
int allpages=Count/onepage;
for (int i = 0;i<allpages; i++)
{
htmlfilename = "index_"+""+i+""+".html";
int start = i * onepage;
int nextpage=i+1;
int perpage=i-1;
if (nextpage==allpages)
{
nextpage=i;
}
if (perpage==-1)
{
perpage=0;
}
// 写文件
try
{
SqlConnection gConnection = new SqlConnection(ConnectionString);
gConnection.Open();
string vQuery = "SELECT TOP "+onepage+" * FROM 表名 WHERE (ID NOT IN (SELECT TOP "+start+" Id FROM 表名 ORDER BY id)) ORDER BY ID";
SqlDataAdapter vmd = new SqlDataAdapter(vQuery,gConnection);
DataSet ds=new DataSet();
vmd.Fill(ds,"table1");
DataTable dt=ds.Tables["table1"];
sw = new StreamWriter(path + htmlfilename , false, Encoding.GetEncoding("GB2312"));
foreach(DataRow dr in dt.Rows)
{
sw.WriteLine(""+dr["字段名"].ToString()+dr["字段名"]+"<br>");
}
sw.WriteLine("<a href="+"index_"+""+perpage+""+".html"+">上一页</a>");
sw.WriteLine("<a href="+"index_"+""+nextpage+""+".html"+">下一页</a>");
sw.Flush();
}
catch(Exception ex)
{
HttpContext.Current.Response.Write(ex.Message);
HttpContext.Current.Response.End();
}
finally
{
sw.Close();
}
}
}
}
---------------------------------------------------
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());
}
}
}
}