一聚教程网:一个值得你收藏的教程网站

热门教程

asp.net生成html页面的方法

时间:2022-06-25 04:09:55 编辑:袖梨 来源:一聚教程网

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模板页面

01.

02.

03.

04.

05.

06.

07.

08.

11.

12.

09.$htmlformat[3]

10.

13.

14.
复制代码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("");

18.}//---------------------给标记数组赋值------------format[0]="background="bg.jpg"";//背景图片

19.format[1]= "#990099";//字体颜色

20.format[2]="150px";//字体大小

21.format[3]= "生成的模板html页面";//文字说明

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:
" + exc.ToString() + ".");
            }
            finally
            {
                Response.Write("恭喜,转换成功!");
                Repeater1.DataBind();
            }
        }
    }
}

 

热门栏目