之前项目需要,查找了office文档在线预览的解决方案,顺便记录一下,方便以后查询。
直接在浏览器中打开Office文档在页面上的链接。会弹出如下窗口:
优点:主流浏览器都支持。
缺点:Office文档链接在浏览器中打开,会有如上图的提示,需用户自己选择打开或者保存功能,如果客户电脑上安装迅雷下载软件,会启动迅雷下载,用户体验不好。
office文档转html,首先引入com组件中office库,然后在程序集扩展中引入word,excel,ppt的dll。
然后F6生成,会报如下错误:
解决办法:
office文档转换html辅助类:
Office2HtmlHelper.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Office.Core;
using Word = Microsoft.Office.Interop.Word;
namespace Wolfy.OfficePreview
{
public class Office2HtmlHelper
{
///
/// Word转成Html
///
/// 要转换的文档的路径
/// 转换成html的保存路径
/// 转换成html的文件名字
public static void Word2Html(string path, string savePath, string wordFileName)
{
Word.ApplicationClass word = new Word.ApplicationClass();
Type wordType = word.GetType();
Word.Documents docs = word.Documents;
Type docsType = docs.GetType();
Word.Document doc = (Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { (object)path, true, true });
Type docType = doc.GetType();
string strSaveFileName = savePath + wordFileName + ".html";
object saveFileName = (object)strSaveFileName;
docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML });
docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
}
///
/// Excel转成Html
///
/// 要转换的文档的路径
/// 转换成html的保存路径
/// 转换成html的文件名字
public static void Excel2Html(string path, string savePath, string wordFileName)
{
string str = string.Empty;
Microsoft.Office.Interop.Excel.Application repExcel = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook workbook = null;
Microsoft.Office.Interop.Excel.Worksheet worksheet = null;
workbook = repExcel.Application.Workbooks.Open(path, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];
object htmlFile = savePath + wordFileName + ".html";
object ofmt = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml;
workbook.SaveAs(htmlFile, ofmt, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
object osave = false;
workbook.Close(osave, Type.Missing, Type.Missing);
repExcel.Quit();
}
///
/// ppt转成Html
///
/// 要转换的文档的路径
/// 转换成html的保存路径
/// 转换成html的文件名字
public static void PPT2Html(string path, string savePath, string wordFileName)
{
Microsoft.Office.Interop.PowerPoint.Application ppApp = new Microsoft.Office.Interop.PowerPoint.Application();
string strSourceFile = path;
string strDestinationFile = savePath + wordFileName + ".html";
Microsoft.Office.Interop.PowerPoint.Presentation prsPres = ppApp.Presentations.Open(strSourceFile, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse);
prsPres.SaveAs(strDestinationFile, Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsHTML, MsoTriState.msoTrue);
prsPres.Close();
ppApp.Quit();
}
}
}
Office2Html.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Office2Html.aspx.cs" Inherits="Wolfy.OfficePreview.Office2Html" %>
Office2Html.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Wolfy.OfficePreview
{
public partial class Office2Html : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnWord_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
switch (btn.CommandArgument)
{
case "docx":
Office2HtmlHelper.Word2Html(MapPath("/Doc/分析某网站的SEO策略(外链篇).doc"), MapPath("/Html/"), "分析某网站的SEO策略(外链篇)");
break;
case "xlsx":
Office2HtmlHelper.Excel2Html(MapPath("/Excel/1994-2013北京市历年最低工资标准.xlsx"), MapPath("/Html/"), "1994-2013北京市历年最低工资标准");
break;
case "ppt":
Office2HtmlHelper.PPT2Html(MapPath("/PPT/23种设计模式详解.ppt"), MapPath("/Html/"), "23种设计模式详解");
break;
default:
break;
}
}
}
}
这里为了测试特找了含有图片的office文档,浏览正常:
要求:机器需安装office,并且office环境是纯净的,所谓纯净就是不能有多个版本,lz曾经在电脑上安装过wps,被害苦了总是报如下错误:
报这个错误,只能哭了,网上的关于00046的解决办法都尝试了,不行。然后不得不重新安装office,然后笑了。最好安装office完整版,因为原来装的不是完整版,不知道有没有这方面的原因,也没有测试,建议完整版。
office文档转PDF,PDF转swf,使用flexpaper+swftools实现在线浏览。
在操作office2007时,需安装SaveAsPDFandXPS.exe ,安装成功后,如图所示:
只有安装了SaveAsPDFandXPS.exe,程序操作office文档,才有office文档另存为pdf文件。office2010不需要安装了,内置有这个功能。
核心代码:
Office2PDFHelper.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Word = Microsoft.Office.Interop.Word;
using Excel = Microsoft.Office.Interop.Excel;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Microsoft.Office.Core;
namespace Wolfy.OfficePreview
{
///
/// Office2Pdf 将Office文档转化为pdf
///
public class Office2PDFHelper
{
public Office2PDFHelper()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
///
/// Word转换成pdf
///
/// 源文件路径
/// 目标文件路径
/// true=转换成功
public static bool DOCConvertToPDF(string sourcePath, string targetPath)
{
bool result = false;
Word.WdExportFormat exportFormat = Word.WdExportFormat.wdExportFormatPDF;
object paramMissing = Type.Missing;
Word.ApplicationClass wordApplication = new Word.ApplicationClass();
Word.Document wordDocument = null;
try
{
object paramSourceDocPath = sourcePath;
string paramExportFilePath = targetPath;
Word.WdExportFormat paramExportFormat = exportFormat;
bool paramOpenAfterExport = false;
Word.WdExportOptimizeFor paramExportOptimizeFor = Word.WdExportOptimizeFor.wdExportOptimizeForPrint;
Word.WdExportRange paramExportRange = Word.WdExportRange.wdExportAllDocument;
int paramStartPage = 0;
int paramEndPage = 0;
Word.WdExportItem paramExportItem = Word.WdExportItem.wdExportDocumentContent;
bool paramIncludeDocProps = true;
bool paramKeepIRM = true;
Word.WdExportCreateBookmarks paramCreateBookmarks = Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks;
bool paramDocStructureTags = true;
bool paramBitmapMissingFonts = true;
bool paramUseISO19005_1 = false;
wordDocument = wordApplication.Documents.Open(
ref paramSourceDocPath, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing);
if (wordDocument != null)
wordDocument.ExportAsFixedFormat(paramExportFilePath,
paramExportFormat, paramOpenAfterExport,
paramExportOptimizeFor, paramExportRange, paramStartPage,
paramEndPage, paramExportItem, paramIncludeDocProps,
paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
paramBitmapMissingFonts, paramUseISO19005_1,
ref paramMissing);
result = true;
}
catch
{
result = false;
}
finally
{
if (wordDocument != null)
{
wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing);
wordDocument = null;
}
if (wordApplication != null)
{
wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing);
wordApplication = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
return result;
}
///
/// 把Excel文件转换成PDF格式文件
///
/// 源文件路径
/// 目标文件路径
/// true=转换成功
public static bool XLSConvertToPDF(string sourcePath, string targetPath)
{
bool result = false;
Excel.XlFixedFormatType targetType = Excel.XlFixedFormatType.xlTypePDF;
object missing = Type.Missing;
Excel.ApplicationClass application = null;
Excel.Workbook workBook = null;
try
{
application = new Excel.ApplicationClass();
object target = targetPath;
object type = targetType;
workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing, missing);
workBook.ExportAsFixedFormat(targetType, target, Excel.XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing);
result = true;
}
catch
{
result = false;
}
finally
{
if (workBook != null)
{
workBook.Close(true, missing, missing);
workBook = null;
}
if (application != null)
{
application.Quit();
application = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
return result;
}
///
/// 把PowerPoint文件转换成PDF格式文件
///
///源文件路径
///目标文件路径
///true=转换成功
public static bool PPTConvertToPDF(string sourcePath, string targetPath)
{
bool result;
PowerPoint.PpSaveAsFileType targetFileType = PowerPoint.PpSaveAsFileType.ppSaveAsPDF;
object missing = Type.Missing;
PowerPoint.ApplicationClass application = null;
PowerPoint.Presentation persentation = null;
try
{
application = new PowerPoint.ApplicationClass();
persentation = application.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse); persentation.SaveAs(targetPath, targetFileType, Microsoft.Office.Core.MsoTriState.msoTrue);
result = true;
}
catch
{
result = false;
}
finally
{
if (persentation != null)
{
persentation.Close();
persentation = null;
}
if (application != null)
{
application.Quit();
application = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
return result;
}
}
}
Office2PDF.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Office2PDF.aspx.cs" Inherits="Wolfy.OfficePreview.Office2PDF" %>
Office2PDF.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Wolfy.OfficePreview
{
public partial class Office2PDF : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnWord_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
switch (btn.CommandArgument)
{
case "docx":
Office2PDFHelper.DOCConvertToPDF(MapPath("/Doc/分析某网站的SEO策略(外链篇).doc"), MapPath("/PDF/分析某网站的SEO策略(外链篇).pdf"));
break;
case "xlsx":
Office2PDFHelper.XLSConvertToPDF(MapPath("/Excel/1994-2013北京市历年最低工资标准.xlsx"), MapPath("/PDF/1994-2013北京市历年最低工资标准.pdf"));
break;
case "ppt":
Office2PDFHelper.PPTConvertToPDF(MapPath("/PPT/23种设计模式详解.ppt"), MapPath("/PDF/23种设计模式详解.pdf"));
break;
default:
break;
}
}
}
}
此方案office转pdf文件的过程的要求与方案二要求相同。
pdf转换完成后,就可以将pdf转换为swf,使用flexpaper+swftools实现在线浏览了,可参考我之前的一篇文章:
FlexPaper+SWFTool+操作类=在线预览PDF
office文档直接转换为swf,使用flexpaper+swftool实现在先浏览。
office直接转换为swf,这里使用flashpaper来实现:
FlashPaper是一个虚拟打印机,可将word文件直接转化成swf格式文件(.doc.xls .txt .pdf等文件都可以正常生成SWF格式)。
这里只贴出核心代码:
Office2Swf.aspx.cs
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Wolfy.OfficePreview
{
public partial class Office2Swf : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnWord_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
switch (btn.CommandArgument)
{
case "docx":
ConvertOffice2Swf(MapPath("/Doc/分析某网站的SEO策略(外链篇).doc"), MapPath("/SWF/分析某网站的SEO策略(外链篇).swf"));
break;
case "xlsx":
Office2PDFHelper.XLSConvertToPDF(MapPath("/Excel/1994-2013北京市历年最低工资标准.xlsx"), MapPath("/SWF/1994-2013北京市历年最低工资标准.swf"));
break;
case "ppt":
Office2PDFHelper.PPTConvertToPDF(MapPath("/PPT/23种设计模式详解.ppt"), MapPath("/SWF/23种设计模式详解.swf"));
break;
default:
break;
}
}
///
/// office 转swf
///
/// 要转换的office文档路径
/// 转换后swf的路径
private void ConvertOffice2Swf(string officePath, string swfPath)
{
Process process = new Process(); //创建进程对象
ProcessStartInfo startInfo = new ProcessStartInfo();
string paperroot = @"C:\Program Files\Macromedia\FlashPaper 2\FlashPrinter.exe";//这里是FlashPrinter的路径
string docFile = officePath;
string swfFile = swfPath;
startInfo.FileName = paperroot;
startInfo.Arguments = docFile + " -o " + swfFile;
startInfo.UseShellExecute = false; //不使用系统外壳程序启动
startInfo.RedirectStandardInput = false; //不重定向输入
startInfo.RedirectStandardOutput = false; //重定向输出
startInfo.CreateNoWindow = true; //不创建窗口
process.StartInfo = startInfo;
process.Start();
if (process != null)
process.Close();
}
}
}
鉴于测试时,flashpaper在将office文档转换为swf的时候,在使用flexpaper的浏览时,出现转换的内容为空,猜测:flexpaper能打开的swf文件与flashpaper转的swf文件不兼容。最后使用flashpaper将office文档转换为pdf,然后走方案三,pdf转swf的步骤。另外本地测试时,没问题。将项目部署在IIS上,不能浏览,出现卡死的情况,调试发现,文件太大,在office还没完全转换为pdf的情况下,swftool工具就去寻找pdf文件,出现错误。
IIS上,无法浏览,查询网上解决方案,和权限这块有关,按照步骤设置了,未果,有点遗憾。
使用点聚公司的weboffice控件,测试后发现兼容性较差,放弃。有兴趣的可以研究一下。
office转pdf后,直接浏览器打开,此方案鉴于目前主流浏览器都集成adobe reader功能,可实现直接打开PDF文件。将pdf文件链接可直接打开。
必要条件:本地需安装adobe reader类似软件。
http://blogs.office.com/2013/04/10/office-web-viewer-view-office-documents-in-a-browser/
web在线打印,打印阅览,打印维护,打印设计
鉴于项目情况选择一个适合的方案,其中有方案只是曲线救国,但是同样能达到要求。如果您觉得对你有所帮助,不妨推荐一下,让更多的人都能看到,谢谢你能看到文章最后。
demo:
链接:https://pan.baidu.com/s/1hqEpx5a 密码:gupg
参考文章:
http://www.cnblogs.com/expectszc/archive/2012/04/04/2432149.html
http://www.cnblogs.com/lexlin/articles/2478027.html
http://www.cnblogs.com/gossip/p/3473024.html
http://www.cnblogs.com/expectszc/archive/2012/04/04/2432149.html
阅读原文