1. 可以建立非常複雜的頁面,利用包含js檔案的方法,在js檔案內加入document.write()方法可以在所有頁面內加入如頁面頭,廣告等內容。
2. 靜態html檔案利用MS Windows2000的Index Server可以建立全文檢索搜尋引擎,利用asp教程.net可以以DataTable的方式得到搜尋結果。而Win2000的Index服務無法尋找xml檔案的內容。假如包括了資料庫教程搜尋與Index索引雙重尋找,那麼此搜尋功能將非常強盛。
3. 節省伺服器的負荷,哀求一個靜態html檔案比一個aspx檔案伺服器資源節省很多。
缺點
思路二: 假如用硬式編碼方式,工作量非常大,需要非常多的html代碼。調試難題。而且使用寫入程式碼天生的html樣式無法修改,假如網站更換樣式,那麼必需得重新編碼,給後期帶來巨大的工作量。
因此這裡採用的是第一種思路
範例程式碼如下:
1.定義(template.htm)html模板頁面 <html>
01.<head>
02.<title></title>
03.<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
04.</head>
05.<body >
06.<table $htmlformat[0] height="100%" border="0" width="100%" cellpadding="10" cellspacing="0" bgcolor="#eeeeee" style="border:1px solid #000000">
07.<tr>
08.<td width="100%" valign="middle" align="left">
09.<span style="color: $htmlformat[1];font-size: $htmlformat[2]">$htmlformat[3]</span>
10.</td>
11.</tr>
12.</table>
13.</body>
14.</html>
複製代碼2.asp.net教程代碼:
01.//==============讀html模板頁面到stringbuilder對象裡===============string[] format=new string[4];//定義和htmlyem標記數目一致的數組
02.StringBuilder htmltext=new StringBuilder();
03.try
04.{
05. using (StreamReader sr = new StreamReader("存放模板頁面的路徑和頁面名"))
06. {
07. String line;
08. while ((line = sr.ReadLine()) != null)
09. {
10. htmltext.Append(line);
11. }
12. sr.Close();
13. }
14.}
15.catch
16.{
17. Response.Write("<Script>alert('讀取檔案錯誤')</Script>");
18.}//---------------------給標記數組賦值------------format[0]="background="bg.jpg"";//背景圖片
19.format[1]= "#990099";//字型顏色
20.format[2]="150px";//字型大小
21.format[3]= "<marquee>產生的模板html頁面</marquee>";//文字說明
22.//----------替換htm裡的標記為你想加的內容
23.for(int i=0;i<4;i++)
24.{
25. htmltext.Replace("$htmlformat["+i+"]",format[i]);
26.}//==============產生htm檔案==============try
27.{
28. using(StreamWriter sw=new StreamWriter("存放路徑和頁面名",false,System.Text.Encoding.GetEncoding("GB2312")))
29.{
30. sw.WriteLine(htmltext);
31. sw.Flush();
32. sw.Close();
33.}}catch{Response.Write ("The file could not be wirte:");}
複製代碼小結:
用此方法可以方便的產生html檔案。程式使用了是迴圈替換,因此對需替換大量元素的模板速度非常快
看一個把word上傳產生html檔案地
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using Microsoft.Office.Interop.Word;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
/// 上傳檔案並轉存為htm
public string Doc2Html(System.Web.UI.HtmlControls.HtmlInputFile wordFilePath)
{
Microsoft.Office.Interop.Word.ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass();
Type wordType = word.GetType();
Microsoft.Office.Interop.Word.Documents docs = word.Documents;
// 開啟檔案
Type docsType = docs.GetType();
//先上傳再解析為html
string filePath = uploadDoc(wordFilePath);
//判斷是否上傳檔案成功
if (filePath == "0")
return "0";
//判斷是否為word檔案
if (filePath == "1")
return "1";
object fileName = filePath;
//開啟一個Word文檔
Microsoft.Office.Interop.Word.Document doc = (Microsoft.Office.Interop.Word.Document)docsType.InvokeMember("Open",
System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { fileName, true, true });
// 轉換格式,另存新檔html
Type docType = doc.GetType();
string strfileName = fileName.ToString();
//擷取轉換檔的檔案名稱,與Doc檔案同名
int fullNameIndex = strfileName.LastIndexOf("");
string filename = strfileName.Substring(fullNameIndex);
string pattern = @"(.doc)";
filename = System.Text.RegularExpressions.Regex.Replace(filename,pattern,String.Empty);
//被轉換的html文檔儲存的位置
string ConfigPath = HttpContext.Current.Server.MapPath("Html/" + filename + ".html");
object saveFileName = ConfigPath;
docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
null, doc, new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML });
//一定要先關閉,否則退出Word時不能釋放資源
docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
//退出Word
wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
//轉到新產生的頁面
return ("Html/" + filename + ".html");
}
public string uploadDoc(System.Web.UI.HtmlControls.HtmlInputFile uploadFiles)
{
if (uploadFiles.PostedFile != null)
{
string fileName = uploadFiles.PostedFile.FileName;
string fileExtension = System.IO.Path.GetExtension(fileName).ToLower();
string newfileName = "";
try
{
//驗證是否為word格式
if (fileExtension == ".doc" || fileExtension == "docx")
{
DateTime now = DateTime.Now;
newfileName = now.Year.ToString() + now.Month.ToString() + now.Day.ToString() + now.Hour.ToString() + now.Minute.ToString() + now.Second.ToString();
//上傳路徑 指當前上傳頁面的同一級的目錄下面的Doc路徑
uploadFiles.PostedFile.SaveAs(System.Web.HttpContext.Current.Server.MapPath("Doc/" + newfileName + fileExtension));
}
else
{
return "1";
}
}
catch
{
return "0";
}
return System.Web.HttpContext.Current.Server.MapPath("Doc/" + newfileName + fileExtension);
}
else
{
return "0";
}
}
protected void btnUpload_Click(object sender, EventArgs e)
{
if (File1.PostedFile.ContentLength > 0)
{
try
{
Doc2Html(File1);
}
catch (Exception exc)
{
Response.Write("Error:<br />" + exc.ToString() + ".");
}
finally
{
Response.Write("恭喜,轉換成功!");
Repeater1.DataBind();
}
}
}
}