zoukankan      html  css  js  c++  java
  • asp.net word ecxel类型文件在线预览

    asp.net word ecxel类型文件在线预览

    首先得引用COM:

    Microsoft Excel 10 Object Library

    Microsoft Word 10 Object Library

    或者是 10以上的类库

    我现在用的是:资源下载: http://download.csdn.net/detail/panfuy/3247641 或者附件

    Microsoft Excel 10 Object Library

    Microsoft Word 10 Object Library

    代码如下:

    C#代码  收藏代码
    1. using System;    
    2. using System.Data;    
    3. using System.Configuration;    
    4. using System.Collections;    
    5. using System.Web;    
    6. using System.Web.Security;    
    7. using System.Web.UI;    
    8. using System.Web.UI.WebControls;    
    9. using System.Web.UI.WebControls.WebParts;    
    10. using System.Web.UI.HtmlControls;    
    11. using System.IO;    
    12. using System.Diagnostics;    
    13. using Word = Microsoft.Office.Interop.Word;    
    14. using Excel = Microsoft.Office.Interop.Excel;    
    15. using System.Reflection;    
    16. using Microsoft.Office.Interop.Excel;    
    17.     
    18.     
    19. public partial class upload_preview : System.Web.UI.Page    
    20. {    
    21.     protected void Page_Load(object sender, EventArgs e)    
    22.     {    
    23.     
    24.         GenerationWordHTML("E://20110502.doc", "E://20110502.html");    
    25.         GenerationExcelHTML("E://20110502.xls", "E://20110502.html");    
    26.     }    
    27.     
    28.     /// <summary>     
    29.     /// Ecxel文件生成HTML并保存     
    30.     /// </summary>     
    31.     /// <param name="FilePath">需要生成的ecxel文件的路径</param>     
    32.     /// <param name="saveFilePath">生成以后保存HTML文件的路径</param>     
    33.     /// <returns>是否生成成功,成功为true,反之为false</returns>     
    34.     protected bool GenerationExcelHTML(string FilePath, string saveFilePath)    
    35.     {    
    36.         try    
    37.         {    
    38.             Excel.Application app = new Excel.Application();    
    39.             app.Visible = false;    
    40.             Object o = Missing.Value;    
    41.     
    42.             ///打开文件     
    43.             /*下面是Microsoft Excel 9 Object Library的写法: */    
    44.             /*_Workbook xls = app.Workbooks.Open(FilePath, o, o, o, o, o, o, o, o, o, o, o, o);*/    
    45.     
    46.             /*下面是Microsoft Excel 10 Object Library的写法: */    
    47.             _Workbook xls = app.Workbooks.Open(FilePath, o, o, o, o, o, o, o, o, o, o, o, o, o, o);    
    48.     
    49.             ///转换格式,另存为 HTML     
    50.             /*下面是Microsoft Excel 9 Object Library的写法: */    
    51.             /*xls.SaveAs(saveFilePath, Excel.XlFileFormat.xlHtml, o, o, o, o, XlSaveAsAccessMode.xlExclusive, o, o, o, o);*/    
    52.     
    53.             /*下面是Microsoft Excel 10 Object Library的写法: */    
    54.             xls.SaveAs(saveFilePath, Excel.XlFileFormat.xlHtml, o, o, o, o, XlSaveAsAccessMode.xlExclusive, o, o, o, o, o);    
    55.     
    56.             ///退出 Excel     
    57.             app.Quit();    
    58.             return true;    
    59.         }    
    60.         catch    
    61.         {    
    62.             return false;    
    63.         }    
    64.         finally    
    65.         {    
    66.             //最后关闭打开的excel 进程     
    67.             Process[] myProcesses = Process.GetProcessesByName("EXCEL");    
    68.             foreach (Process myProcess in myProcesses)    
    69.             {    
    70.                 myProcess.Kill();    
    71.             }    
    72.         }    
    73.     }    
    74.     
    75.     /// <summary>     
    76.     /// WinWord文件生成HTML并保存     
    77.     /// </summary>     
    78.     /// <param name="FilePath">需要生成的word文件的路径</param>     
    79.     /// <param name="saveFilePath">生成以后保存HTML文件的路径</param>     
    80.     /// <returns>是否生成成功,成功为true,反之为false</returns>     
    81.     private bool GenerationWordHTML(string FilePath, string saveFilePath)    
    82.     {    
    83.         try    
    84.         {    
    85.             Word.ApplicationClass word = new Word.ApplicationClass();    
    86.             Type wordType = word.GetType();    
    87.             Word.Documents docs = word.Documents;    
    88.     
    89.             /// 打开文件      
    90.             Type docsType = docs.GetType();    
    91.             Word.Document doc = (Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { FilePath, true, true });    
    92.     
    93.             /// 转换格式,另存为 HTML      
    94.             Type docType = doc.GetType();    
    95.     
    96.             /*下面是Microsoft Word 9 Object Library的写法: */    
    97.             /*docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFilePath, Word.WdSaveFormat.wdFormatHTML });*/    
    98.     
    99.             /*下面是Microsoft Word 10 Object Library的写法: */    
    100.             docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,    
    101.             null, doc, new object[] { saveFilePath, Word.WdSaveFormat.wdFormatFilteredHTML });    
    102.     
    103.             /// 退出 Word     
    104.             wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);    
    105.             return true;    
    106.         }    
    107.         catch    
    108.         {    
    109.             return false;    
    110.         }    
    111.         finally    
    112.         {    
    113.             //最后关闭打开的winword 进程     
    114.             Process[] myProcesses = Process.GetProcessesByName("WINWORD");    
    115.             foreach (Process myProcess in myProcesses)    
    116.             {    
    117.                 myProcess.Kill();    
    118.             }    
    119.         }    
    120.     }    
    121. }    
  • 相关阅读:
    提高PHP程序运行效率的方法
    必须知道的sql编写技巧。多条件查询不拼接字符串·的写法
    数据库SQL优化大总结之 百万级数据库优化方案
    编程一开始就应该养成的好习惯
    php图像处理
    jqurey 简单的,我也简单
    菜单上下级 (全国地区)
    think php v5.0
    正则表达式
    有感赠朵朵
  • 原文地址:https://www.cnblogs.com/ranran/p/3833444.html
Copyright © 2011-2022 走看看