net html页面打印,asp.net生成html页面的方法

1. 可以建立非常复杂的页面,利用包含js文件的方法,在js文件内加入document.write()方法可以在所有页面内加入如页面头,广告等内容。

2. 静态html文件利用MS Windows2000的Index Server可以建立全文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();

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值