码迷,mamicode.com
首页 > Web开发 > 详细

office文件转换为html之word篇

时间:2016-04-21 20:10:15      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:


using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Text; using System.Reflection; using Microsoft.Office.Interop.Word; using Aspose.Words; using System.Text.RegularExpressions; namespace word_html { public partial class office_html : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } private string InsertHeadHtml(string strHtml, string realPath) { int index = strHtml.IndexOf("<body"); strHtml = strHtml.Insert(index, "<div style=‘height:60px;font-size:14px;margin:0px 0px 12px 0px;padding:14px 4px 12px 12px;line-height:24px;height:1%;‘>以下是该文档的HTML预览效果。<br/><span>由于是网页提取显示word中的内容,有可能部分显示与源文档中有差异,如想查看更准确的信息,</span>您可以点击 <a style=‘color:6699FF;text-decoration:underline;‘ href=‘/Lib/UdControls/Download.aspx?action=Download&appFormCode=" + HttpContext.Current.Request.QueryString["appFormCode"].ToString() + "&path=" + HttpContext.Current.Request.QueryString["path"].ToString() + "&encrypt=" + HttpContext.Current.Request.QueryString["encrypt"].ToString() + "‘><b>下载原始附件</b></a></div>"); Regex reg = new Regex(@"(?<start><img[^>]+?src="")(?<src>[^""]+?)(?<end>""[^>]+?>)"); strHtml = reg.Replace(strHtml, delegate(Match m) { return string.Format("{0}{1}{2}{3}", m.Groups["start"].Value, realPath, m.Groups["src"].Value, m.Groups["end"].Value ); }); return strHtml; } private void WordToHtmlFile(string WordFilePath) { //try //{ // 指定原文件和目标文件 string realPath = WordFilePath.Substring(0, WordFilePath.LastIndexOf("/") + 1); WordFilePath = System.Web.HttpContext.Current.Server.MapPath(WordFilePath); object target = WordFilePath.Substring(0, WordFilePath.LastIndexOf(".")) + ".html"; //string realPath = WordFilePath.Substring(0, WordFilePath.LastIndexOf(".")) + ".html"; if (!File.Exists(target.ToString())) { Aspose.Words.Document doc = new Aspose.Words.Document(WordFilePath); doc.Save(target.ToString(), SaveFormat.Html); //doc.Close(); } StreamReader sr = new StreamReader(target.ToString(), Encoding.Default); //string strHtml = sr.ReadToEnd(); //strHtml = InsertHeadHtml(strHtml, realPath); //HttpContext.Current.Response.Write(strHtml); sr.Close(); //} //catch (Exception ex) //{ // //记录异常 // LogEntry logEntry = new LogEntry(); // logEntry.Message = ex.Message; // logEntry.Title = "---->将Word文档转换成HTML格式异常[WordToHtmlFile]"; // logEntry.TimeStamp = DateTime.Now; // logEntry.LogEntryType = LogEntryType.Error; // logEntry.LogCatalog = LogCatalog.ExceptionLog; // logEntry.StackTrace = ex.StackTrace; // LogPosition logPosition = LogPosition.FileLog; // string positionParameter = SysConfig.ToString(SysConfig.GetAppSetting("LogPath")); // SysLogger.Write(logEntry, logPosition, positionParameter); //} } protected void Button1_Command(object sender, CommandEventArgs e) { string path = @"/office_file/Myword.docx"; string path_html = @"/office_file/Myword.html"; WordToHtmlFile(path); if (File.Exists((string)path_html)) { File.Delete((string)path_html); } } } }

  前台加个butten 就可以了  还有就是需要 Aspose.Words   不过目测可以不用 用微软的直接可以   如果有问题可以和我联系  

qq1754463019

office文件转换为html之word篇

标签:

原文地址:http://www.cnblogs.com/cxl123/p/5418209.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!