惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

U
Unit 42
N
News and Events Feed by Topic
S
Schneier on Security
G
GRAHAM CLULEY
Scott Helme
Scott Helme
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
GbyAI
GbyAI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
C
CERT Recently Published Vulnerability Notes
T
The Exploit Database - CXSecurity.com
C
Cisco Blogs
T
The Blog of Author Tim Ferriss
Cisco Talos Blog
Cisco Talos Blog
P
Privacy & Cybersecurity Law Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园 - 司徒正美
Blog — PlanetScale
Blog — PlanetScale
Project Zero
Project Zero
MyScale Blog
MyScale Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Apple Machine Learning Research
Apple Machine Learning Research
小众软件
小众软件
The Last Watchdog
The Last Watchdog
Vercel News
Vercel News
The Cloudflare Blog
C
Check Point Blog
Help Net Security
Help Net Security
Microsoft Security Blog
Microsoft Security Blog
AI
AI
Simon Willison's Weblog
Simon Willison's Weblog
云风的 BLOG
云风的 BLOG
M
MIT News - Artificial intelligence
Stack Overflow Blog
Stack Overflow Blog
腾讯CDC
NISL@THU
NISL@THU
S
Security @ Cisco Blogs
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
S
SegmentFault 最新的问题
MongoDB | Blog
MongoDB | Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threatpost
AWS News Blog
AWS News Blog
Cloudbric
Cloudbric
N
News and Events Feed by Topic
PCI Perspectives
PCI Perspectives
S
Securelist
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
V
Vulnerabilities – Threatpost
S
Secure Thoughts

博客园 - 阿哲

SqlServer 2005 快速设置字段/表 的描述字段 将Visio文件装换成HTML文件(在服务器上转换,客户端无需安装visio即可查看) string:值类型?引用类型?[转] SqlServer2005常用sql语句 SQL中FOR XML子句的各种用法 获得Windows中文件类型名称 vs2008生成自定义dll,VS2008发布、生成网站时设置固定的dll文件名 开源.Net CMS系统 [转] 测试使用Windows Live Writter写Blog 试用Windows Live Mail OutlookExpress使用技巧 剩余的GMail邀请 实体类+自定义控件=? 用反射+特性列出所有的枚举变量及其描述信息,绑定到DropDownList上。 有关.NET和RS232设备通讯 .NET资源(收藏) 转:使用P/Invoke来开发用于与串行设备通讯的.NET基类(翻译) 最近有空,研究了一下.NET的通讯方面 实体和实体的集合-续2
完成类似QQ邮箱中‘HTML方式查看’功能查看Office文件
阿哲 · 2009-03-06 · via 博客园 - 阿哲

我们在做一些系统的 下载模块 或者 内部邮箱 的时候,有些使用系统的人可能并没有安装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();
            }
        }

有了这三个方法,我们可以在上传文件时将对应的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;
        }

这样就OK了