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

推荐订阅源

Recorded Future
Recorded Future
C
Cyber Attacks, Cyber Crime and Cyber Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Scott Helme
Scott Helme
Cyberwarzone
Cyberwarzone
C
CERT Recently Published Vulnerability Notes
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Palo Alto Networks Blog
Google Online Security Blog
Google Online Security Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
WordPress大学
WordPress大学
博客园 - 聂微东
L
LINUX DO - 最新话题
月光博客
月光博客
小众软件
小众软件
T
Troy Hunt's Blog
A
Arctic Wolf
量子位
I
Intezer
大猫的无限游戏
大猫的无限游戏
T
Tailwind CSS Blog
S
Schneier on Security
Schneier on Security
Schneier on Security
NISL@THU
NISL@THU
T
Threat Research - Cisco Blogs
TaoSecurity Blog
TaoSecurity Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园_首页
有赞技术团队
有赞技术团队
N
News and Events Feed by Topic
美团技术团队
The Cloudflare Blog
P
Privacy International News Feed
S
Security Affairs
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
G
GRAHAM CLULEY
N
News | PayPal Newsroom
Apple Machine Learning Research
Apple Machine Learning Research
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
L
LINUX DO - 热门话题
V
Vulnerabilities – Threatpost
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
C
CXSECURITY Database RSS Feed - CXSecurity.com
宝玉的分享
宝玉的分享
Application and Cybersecurity Blog
Application and Cybersecurity Blog
IT之家
IT之家
Hacker News: Ask HN
Hacker News: Ask HN
雷峰网
雷峰网

博客园 - Candyxiaoqiang

使用 ADO.NET 访问 Oracle 9i 存储过程(转) 使用 ADO.NET 和 Oracle 进行高级数据访问 (转自) HttpRequest, HttpRuntime, AppDomain and friends() 在showModalDialog窗体里,单击服务端Button产生新页面解决方法 - Candyxiaoqiang - 博客园 asp.net按钮 button的onclick事件 与oncommand 事件的区别 汇总 SQL 连接字符串的说明(转) JavaScript相关资料( 收集中.......) - Candyxiaoqiang - 博客园 Visual Studio常用小技巧 觉得不错 转一下 Web开发中用的工具(不断更新) C#实现Web文件上传的两种方法 (转) document,event javascript中常用对象介绍 带中文说明的 (转) ASP.Net实现将Word转换PDF格式 (转自:思绪随风~~~) 用C#实现生成PDF文档和将WORD转换为PDF (转自海东的技术资料) C# 拆分word(根据标题或书签拆分) (转自zrx401558287) 如何:将文档发送到打印机(转) 利用.net替换Word的内容(从数据库中取数据来替换word里面的书签)(转) 在IIS上启用Gzip压缩(HTTP压缩) (转自子秋的博客) 如何采用Local方式连接到ArcGIS Server(转) VS2005 直接调试网页不能显示
完成类似QQ邮箱中‘HTML方式查看’功能查看Office文件 (转自zellzhang)
Candyxiaoqiang · 2009-05-21 · via 博客园 - Candyxiaoqiang

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