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> ...
随机推荐
- Netty 中文教程 Hello World !详解
1.HelloServer 详解 HelloServer首先定义了一个静态终态的变量---服务端绑定端口7878.至于为什么是这个7878端口,纯粹是笔者个人喜好.大家可以按照自己的习惯选择端口.当然 ...
- 移动端页面使用rem来做适配
文/九彩拼盘(简书作者)原文链接:http://www.jianshu.com/p/eb05c775d3c6著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. rem介绍 rem(font ...
- css文件都写在一个里面还是每个页面都引用单独的css样式好?
因为网站比较小,外加网站页面有很多重复构件,决定采用“构件复用”搭建网页,但是遇到了一个问题.因为虽然有共同的css,但是每个页面或多或少都有独立的样式控制,到底是写在同一个css还是分离看上去清楚一 ...
- uva 757
贪心 优先队列 #include <cstdio> #include <cstdlib> #include <cmath> #include <map> ...
- 请求--拦截器--action经过
引用我这里想知道的是同名的多个参数,会被自动的放置在List或者数组中,我想知道是怎么实现的,因为取一个参数和取多个同名的参数是不同的方法: 一个是request.getParameter 一个是re ...
- ByteArrary(优化数据存储和数据流)
原地址:http://www.unity蛮牛.com/blog-1801-799.html 首页 博客 相册 主题 留言板 个人资料 ByteArrary(优化数据存储和数据流) 分类:unity ...
- POJ 3045
Cow Acrobats Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2302 Accepted: 912 Descr ...
- POJ 1731
#include<iostream> #include<string> #include<algorithm> using namespace std; int m ...
- apache common-io.jar FileUtils
//复制文件 void copyFile(File srcFile, File destFile) //将文件内容转化为字符串 String readFileToString(File file ...
- 欧拉工程第65题:Convergents of e
题目链接 现在做这个题目真是千万只草泥马在心中路过 这个与上面一题差不多 这个题目是求e的第100个分数表达式中分子的各位数之和 What is most surprising is that the ...