完成类似QQ邮箱中‘HTML方式查看’功能查看Office文件
我们在做一些系统的 下载模块 或者 内部邮箱 的时候,有些使用系统的人可能并没有安装office,但是又急需查看附件的内容甚至图片等等,下载安装一个Office又不大现实(时间要求紧?在某个网吧?在朋友家里?),那么如何查看一些常用的附件呢?
其实QQ邮箱有个功能很好,它可以以 HTML方式查看 Office文件,例如Word文档、Excel表格已经PowerPoint幻灯片等,这个功能有很多实现的办法,例如使用SPS就是办法之一,但是其实大部分时候,我们用不到SPS,或者由于正版软件太贵了,那如何使用ASP.NET 2.0(C#)来实现这个功能?
注意:以下代码在vs2008,office2007,windowsxp下测试通过,项目需要添加Excel 12、Word12、PowerPoint12、Office12 四个com引用
/// <summary>
/// 将PPT文件转换成HTML格式
/// </summary>
/// <param name="PptFilePath">PPT文件路径</param>
public static void PptToHtmlFile(string PptFilePath)
{
Microsoft.Office.Interop.PowerPoint.Application ppt = new Microsoft.Office.Interop.PowerPoint.Application();
Microsoft.Office.Interop.PowerPoint.Presentation pptFile = null;
try
{
//获得html文件名
string htmlFileName = PptFilePath.Substring(0, PptFilePath.LastIndexOf(".")) + ".html";
//打开一个ppt文件
pptFile = ppt.Presentations.Open(PptFilePath, Microsoft.Office.Core.MsoTriState.msoTrue,
Microsoft.Office.Core.MsoTriState.msoCTrue, Microsoft.Office.Core.MsoTriState.msoFalse);
//转换成html格式
pptFile.SaveAs(htmlFileName, Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsHTML,
Microsoft.Office.Core.MsoTriState.msoCTrue);
}
finally
{
if (pptFile != null)
{
pptFile.Close();
}
ppt.Quit();
GC.Collect();
}
}
/// <summary>
/// 将Excel文件转换成HTML格式
/// </summary>
/// <param name="ExcelFilePath">Excel文件路径</param>
public static void ExcelToHtmlFile(string ExcelFilePath)
{
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook oBook = null;
// 缺省参数
object Unknown = Type.Missing;
try
{
//目标html文件路径
object Target = ExcelFilePath.Substring(0, ExcelFilePath.LastIndexOf(".")) + ".html";
//为了保险,只读方式打开
object readOnly = true;
// 指定另存为格式(html)
object format = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml;
//打开Excel文件
oBook = excelApp.Workbooks.Open(ExcelFilePath, Unknown, readOnly,
Unknown, Unknown, Unknown, Unknown, Unknown, Unknown,
Unknown, Unknown, Unknown, Unknown, Unknown, Unknown);
// 转换格式
oBook.SaveAs(Target, format, Unknown, Unknown, Unknown, Unknown,
Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
Unknown, Unknown, Unknown, Unknown, Unknown);
}
finally
{
if (oBook != null)
{
oBook.Close(false, Unknown, Unknown);
}
excelApp.Quit();
GC.Collect();
}
}
/// <summary>
/// 将Word文档转换成HTML格式
/// </summary>
/// <param name="WordFilePath">Word文档格式</param>
public static void WordToHtmlFile(string WordFilePath)
{
Microsoft.Office.Interop.Word.Application newApp = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document doc = null;
// 缺省参数
object Unknown = Type.Missing;
try
{
// 指定原文件和目标文件
object Source = WordFilePath;
object Target = WordFilePath.Substring(0, WordFilePath.LastIndexOf(".")) + ".html";
//为了保险,只读方式打开
object readOnly = true;
// 指定另存为格式(html)
object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML;
// 打开doc文件
doc = newApp.Documents.Open(ref Source, ref Unknown, ref readOnly,
ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown);
// 转换格式
doc.SaveAs(ref Target, ref format,
ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown);
}
finally
{
if (doc != null)
{
// 关闭文档和Word程序
doc.Close(ref Unknown, ref Unknown, ref Unknown);
}
newApp.Quit(ref Unknown, ref Unknown, ref Unknown);
GC.Collect();
}
}
/// 将PPT文件转换成HTML格式
/// </summary>
/// <param name="PptFilePath">PPT文件路径</param>
public static void PptToHtmlFile(string PptFilePath)
{
Microsoft.Office.Interop.PowerPoint.Application ppt = new Microsoft.Office.Interop.PowerPoint.Application();
Microsoft.Office.Interop.PowerPoint.Presentation pptFile = null;
try
{
//获得html文件名
string htmlFileName = PptFilePath.Substring(0, PptFilePath.LastIndexOf(".")) + ".html";
//打开一个ppt文件
pptFile = ppt.Presentations.Open(PptFilePath, Microsoft.Office.Core.MsoTriState.msoTrue,
Microsoft.Office.Core.MsoTriState.msoCTrue, Microsoft.Office.Core.MsoTriState.msoFalse);
//转换成html格式
pptFile.SaveAs(htmlFileName, Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsHTML,
Microsoft.Office.Core.MsoTriState.msoCTrue);
}
finally
{
if (pptFile != null)
{
pptFile.Close();
}
ppt.Quit();
GC.Collect();
}
}
/// <summary>
/// 将Excel文件转换成HTML格式
/// </summary>
/// <param name="ExcelFilePath">Excel文件路径</param>
public static void ExcelToHtmlFile(string ExcelFilePath)
{
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook oBook = null;
// 缺省参数
object Unknown = Type.Missing;
try
{
//目标html文件路径
object Target = ExcelFilePath.Substring(0, ExcelFilePath.LastIndexOf(".")) + ".html";
//为了保险,只读方式打开
object readOnly = true;
// 指定另存为格式(html)
object format = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml;
//打开Excel文件
oBook = excelApp.Workbooks.Open(ExcelFilePath, Unknown, readOnly,
Unknown, Unknown, Unknown, Unknown, Unknown, Unknown,
Unknown, Unknown, Unknown, Unknown, Unknown, Unknown);
// 转换格式
oBook.SaveAs(Target, format, Unknown, Unknown, Unknown, Unknown,
Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
Unknown, Unknown, Unknown, Unknown, Unknown);
}
finally
{
if (oBook != null)
{
oBook.Close(false, Unknown, Unknown);
}
excelApp.Quit();
GC.Collect();
}
}
/// <summary>
/// 将Word文档转换成HTML格式
/// </summary>
/// <param name="WordFilePath">Word文档格式</param>
public static void WordToHtmlFile(string WordFilePath)
{
Microsoft.Office.Interop.Word.Application newApp = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document doc = null;
// 缺省参数
object Unknown = Type.Missing;
try
{
// 指定原文件和目标文件
object Source = WordFilePath;
object Target = WordFilePath.Substring(0, WordFilePath.LastIndexOf(".")) + ".html";
//为了保险,只读方式打开
object readOnly = true;
// 指定另存为格式(html)
object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML;
// 打开doc文件
doc = newApp.Documents.Open(ref Source, ref Unknown, ref readOnly,
ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown);
// 转换格式
doc.SaveAs(ref Target, ref format,
ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown);
}
finally
{
if (doc != null)
{
// 关闭文档和Word程序
doc.Close(ref Unknown, ref Unknown, ref Unknown);
}
newApp.Quit(ref Unknown, ref Unknown, ref Unknown);
GC.Collect();
}
}
有了这三个方法,我们可以在上传文件时将对应的office文件转成html的文件,跟office文件放在同一个目录中,同时加一个对应的链接“HTML方式查看”即可。
如果是第二期添加这个功能,那可以在获得文件列表的时候,先取得文件的扩展名,然后再查看有没有对应的HTML文件,如果没有,则即时生成一个,很方便,不过如果量很大的话,转换的速度会比较慢,最好是写一个winform的程序遍历整个目录,先把html文件生成一遍,这样再打开就会很快了。
简单的写一个如下:
/// <summary>
/// 生成文件对应的HTML版本(没有考虑HTML文件已经存在的处理)
/// </summary>
/// <param name="fileFullName">文件路径</param>
/// <returns>如果生成了对应的HTML文件,返回true,如果不需要生成HTML文件,返回flase</returns>
public static bool ConvertFileToHtml(string fileFullName)
{
System.IO.FileInfo file = new System.IO.FileInfo(fileFullName);
if (file.Exists)
{
string strExt = file.Extension.Substring(1).ToLower();
switch (strExt)
{
case "doc":
case "docx":
WordToHtmlFile(fileFullName);
return true;
case "xls":
case "xlsx":
ExcelToHtmlFile(fileFullName);
return true;
case "ppt":
case "pptx":
PptToHtmlFile(fileFullName);
return true;
default:
return false;
}
}
return false;
}
/// 生成文件对应的HTML版本(没有考虑HTML文件已经存在的处理)
/// </summary>
/// <param name="fileFullName">文件路径</param>
/// <returns>如果生成了对应的HTML文件,返回true,如果不需要生成HTML文件,返回flase</returns>
public static bool ConvertFileToHtml(string fileFullName)
{
System.IO.FileInfo file = new System.IO.FileInfo(fileFullName);
if (file.Exists)
{
string strExt = file.Extension.Substring(1).ToLower();
switch (strExt)
{
case "doc":
case "docx":
WordToHtmlFile(fileFullName);
return true;
case "xls":
case "xlsx":
ExcelToHtmlFile(fileFullName);
return true;
case "ppt":
case "pptx":
PptToHtmlFile(fileFullName);
return true;
default:
return false;
}
}
return false;
}
这样就OK了