HTML页面导出为Word
protected void btnExport_Click(object sender, EventArgs e)
{
string strFileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".doc";
string sHtml = hfdHtml.Value;//前台的HTML传递过来的,注意加ValidateRequest="false"
string sMht = HtmlToMht(sHtml);
DnLoadFileFromMemoryStream(strFileName, sMht);
} /// <summary>
/// 将HTML文本导出到Word或者MHT格式
/// </summary>
/// <param name="strHtml"></param>
/// <returns></returns>
public static string HtmlToMht(string strHtml)
{
strHtml = strHtml.Replace("<", "<").Replace(">", ">");
StringBuilder sb = new StringBuilder();
sb.AppendLine("From:");
sb.AppendLine("Subject:");
sb.AppendLine("Date:");
sb.AppendLine("MIME-Version: 1.0");
sb.AppendLine("Content-Type: multipart/related;");
sb.AppendLine("\ttype=\"text/html\";");
sb.AppendLine("\tboundary=\"----=_AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"");
sb.AppendLine("\n");
sb.AppendLine("------=_AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
sb.AppendLine("Content-Type: text/html;");
sb.AppendLine("charset=\"gb2312\"");
sb.AppendLine("Content-Transfer-Encoding: quoted-printable");
sb.AppendLine("\n");
sb.AppendLine("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
sb.AppendLine("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
sb.AppendLine("<head>");
sb.AppendLine("<style>");
sb.AppendLine("@page WordSection1");
sb.AppendLine("{size:532.5pt 757.5pt;");
sb.AppendLine("mso-page-orientation:poPortrait;");
sb.AppendLine("margin:26.25pt 26.25pt 26.25pt 26.25pt;");
sb.AppendLine("mso-header-margin:42.55pt;");
sb.AppendLine("mso-footer-margin:49.6pt;");
sb.AppendLine("mso-paper-source:0;}");
sb.AppendLine("div.WordSection1");
sb.AppendLine("{page:WordSection1;}");
sb.AppendLine("</style>");
sb.AppendLine("</head>");
sb.AppendLine("<body>");
sb.AppendLine("<div class=3DWordSection1>");
sb.AppendLine(strHtml.Replace("src=", "src=3D").Replace("style=\"", "style= \\\"")).Replace("rowSpan=", "rowSpan=\\\"")
.Replace("colSpan=", "colSpan=\\\"").Replace("width=", "width=\\\"").Replace("height=", "height=\\\"");
sb.AppendLine("</div>");
sb.AppendLine("</body>");
sb.AppendLine("</html>");
sb.AppendLine("\n");
string[] imgSrcs = GetHtmlImageUrlList(strHtml);
if (imgSrcs.Length > 0)
{
for (int i = 0; i < imgSrcs.Length; i++)
{
string strLink = imgSrcs[i];
string strBianm = ConvertBase64(strLink);
if (strBianm != "")
{
sb.AppendLine("\n");
sb.AppendLine("------=_AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
sb.AppendLine("Content-Type: image/jpeg");
sb.AppendLine("Content-Transfer-Encoding: base64");
sb.AppendLine("Content-Location: " + strLink);
sb.AppendLine("\n");
sb.AppendLine(strBianm);
sb.AppendLine("\n");
}
}
}
sb.AppendLine("------=_AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA--");
return sb.ToString();
} /// <summary>
/// 更改Img等HTML标签从相对路径改为绝对路径,注意HTML里初始化时是什么路径,在后台就是什么路径,它不会在后台变为绝对路径,所以有必要转换一下,不然导出的图片不能显示
/// </summary>
/// <param name="sHtmlText"></param>
/// <returns></returns>
public static string[] GetHtmlImageUrlList(string sHtmlText)
{
// 定义正则表达式用来匹配 img 标签
Regex regImg = new Regex(@"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase);
// 搜索匹配的字符串
MatchCollection matches = regImg.Matches(sHtmlText);
int i = 0;
string[] sUrlList = new string[matches.Count];
// 取得匹配项列表
foreach (Match match in matches)
sUrlList[i++] = match.Groups["imgUrl"].Value;
return sUrlList;
} /// <summary>
/// 将图片转换为Base64位格式字符串流
/// </summary>
/// <param name="filepath"></param>
/// <returns></returns>
public static string ConvertBase64(string filepath)
{
//变量
string result = string.Empty;
string path = string.Empty;
if (filepath.Trim().Substring(0, 4) == "http")
{
result = string.Empty;
}
else
{
path = HttpContext.Current.Server.MapPath(filepath);
//将文件转换为stream
using (FileStream fs = new FileStream(path, FileMode.Open))
{
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
result = Convert.ToBase64String(buffer); //base64编码
}
}
//返回编码后的字符串
return result;
} /// <summary>
/// 将文本流转换为word流
/// </summary>
/// <param name="sFileName"></param>
/// <param name="sContent"></param>
public static void DnLoadFileFromMemoryStream(string sFileName, string sContent)
{
byte[] arrByte = Encoding.UTF8.GetBytes(sContent);
using (MemoryStream ms = new MemoryStream())
{
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + sFileName);
HttpContext.Current.Response.BinaryWrite(arrByte);
}
}
HTML页面导出为Word的更多相关文章
- aspx页面导出为word
aspx页面导出为word代码: System.IO.StringWriter sw = new System.IO.StringWriter(); System.Web.UI.HtmlTextWri ...
- (转)WEB页面导出为Word文档后分页&横向打印的方法
<html> <HEAD> <title>WEB页面导出为Word文档后分页&横向打印的方法 </title> < ...
- c# 将页面导出到word(含图片及控件)
/// <summary> /// 创建word /// <param name="filePath">文件路径 </param> /// &l ...
- PHP 将html页面导出至Word
<?php header("Content-Type: application/msword"); header("Content-Disposition: att ...
- 【MVC】 非常简单的页面导出 WORD, EXCEL方法
[MVC] 页面导出 WORD, EXCEL 前端 js function output() { var para = new Object(); para.html = getHtml(" ...
- Java 实现HTML富文本导出至word完美解决方案
一. 问题的提出 最近用java开发一个科技项目信息管理系统,里面有一个根据项目申请书的模板填写项目申报信息的功能,有一个科技项目申请书word导出功能. 已有的实现方式:采用标准的jsp模板输出实现 ...
- javascript下用ActiveXObject控件替换word书签,将内容导出到word后打印第1/2页
由于时间比较紧,没多的时候去学习研究上述工具包,现在用javascript操作ActiveXObject控件,用替换word模板中的书签方式解决. 最近有需求将数据导出到word里,然后编辑打印. 想 ...
- 将HTML导出生成word文档
前言: 项目开发中遇到了需要将HTML页面的内容导出为一个word文档,所以有了这边随笔. 当然,项目开发又时间有点紧迫,第一时间想到的是用插件,所以百度了下.下面就介绍两个导出word文档的方法. ...
- 网页导出成word文档的默认视图方式问题
网页导出成word文档的默认视图方式问题 一般保存后的word文档默认是“Web版式视图”打开,这样会给客户的感觉不是真正的word文档,必须实现打开就是“页面视图” 1. 修改<html> ...
随机推荐
- SQL Server视图
想来想去,总想写写SQL Server方面的知识,像视图.存储过程,大数据量操作的优化等等. 先把基础的知识总结个遍先,然后再寻求更高更远的发展.这篇文章,将带大家来看看视图. 何谓视图,视图包含行和 ...
- execvp使用实例
问题描述: 本程序实现模拟shell功能,用户输入命令,返回相应的结果 问题解决: 注: 以上指出了execvp函数的使用,使用时第一个参数是文件名,第二个参数是一个 ...
- [BZOJ 1044] [HAOI2008] 木棍分割 【二分 + DP】
题目链接:BZOJ 1044 第一问是一个十分显然的二分,贪心Check(),很容易就能求出最小的最大长度 Len . 第二问求方案总数,使用 DP 求解. 使用前缀和,令 Sum[i] 为前 i 根 ...
- 【转载】struct和typedef struct彻底明白了
分三块来讲述: 1 首先://注意在C和C++里不同 在C中定义一个结构体类型要用typedef: typedef struct Student { int a; }Stu; 于是在声明变量的时候就可 ...
- StringBuffer用法
public class StringBufferTest { public static void main(String[] args) { StringBuffer sb=new StringB ...
- hdu 1242 Rescue(BFS,优先队列,基础)
题目 /******************以下思路来自百度菜鸟的程序人生*********************/ bfs即可,可能有多个’r’,而’a’只有一个,从’a’开始搜,找到的第一个’r ...
- mahout安装配置
1.下载mahout 下载地址:http://mahout.apache.org 我下载的最新版:mahout-distribution-0.9 2.把mahout解压到你想存放的文档,我是放在/Us ...
- 核稀疏表示分类(KSRC)
参考:<Kernel SparseRepresention-Based Classifier> 原文地址:http://www.cnblogs.com/Rosanna/p/3372153. ...
- javascript表格的添加和删除
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- VisualSVN Server的windows 2003配置和使用方法(图文并茂)
1.为什么要用VisualSVN Server,而不用Subversion? 回答: 因为如果直接使用Subversion,那么在Windows 系统上,要想让它随系统启动,就要封装SVN Serve ...