C#在线预览文档(word,excel,pdf,txt,png)

原创 2016年05月31日 17:14:57

                           C#在线预览文档(word,excel,pdf,txt,png)

1、预览方式:将word文件转换成html文件然后预览html文件
2、预览word文件:需要引入Interop.Microsoft.Office.Interop.Word.dll(Com组件)
3、预览Excel文件:需要引入Interop.Microsoft.Office.Interop.Excel.dll(Com组件,Microsoft Excel 12.0(or other version) Object Library)
4、PDF文件直接嵌入到浏览器中进行查看,无需转换(需安装pdf阅读器)
5、文本文件直接嵌入到浏览器进行查看,无需转换

6、图片文件直接嵌入到浏览器进行查看,无需转换

Excel预览方法
using Microsoft.Office.Interop.Excel;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for ExcelPreview
/// </summary>
public class ExcelPreview
{
    public static void Priview(System.Web.UI.Page p, string inFilePath, string outDirPath = "")
    {
        Microsoft.Office.Interop.Excel.Application excel = null;
        Microsoft.Office.Interop.Excel.Workbook xls = null;
        excel = new Microsoft.Office.Interop.Excel.Application();
        object missing = Type.Missing;
        object trueObject = true;
        excel.Visible = false;
        excel.DisplayAlerts = false;

        string randomName = DateTime.Now.Ticks.ToString();  //output fileName

        xls = excel.Workbooks.Open(inFilePath, missing, trueObject, missing,
                                    missing, missing, missing, missing, missing, missing, missing, missing,
                                    missing, missing, missing);

        //Save Excel to Html
        object format = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml;
        Workbook wsCurrent = xls;//(Workbook)wsEnumerator.Current;
        String outputFile = outDirPath + randomName + ".html";
        wsCurrent.SaveAs(outputFile, format, missing, missing, missing,
                          missing, XlSaveAsAccessMode.xlNoChange, missing,
                          missing, missing, missing, missing);
        excel.Quit();

        //Open generated Html
        Process process = new Process();
        process.StartInfo.UseShellExecute = true;
        process.StartInfo.FileName = outputFile;
        process.Start();
    }

}
Pdf类

using Microsoft.Office.Interop.Word;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for WordPreview
/// </summary>
public class PDFPreview
{
    public static void Priview(System.Web.UI.Page p, string inFilePath)
    {
        p.Response.ContentType = "Application/pdf";

        string fileName = inFilePath.Substring(inFilePath.LastIndexOf('\\') + 1);
        p.Response.AddHeader("content-disposition", "filename=" + fileName);
        p.Response.WriteFile(inFilePath);
        p.Response.End();
    }
}
Word预览方法

using Microsoft.Office.Interop.Word;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for WordPreview
/// </summary>
public class WordPreview
{
    public static void Priview(System.Web.UI.Page p, string inFilePath, string outDirPath = "")
    {
        object missingType = Type.Missing;
        object readOnly = true;
        object isVisible = false;
        object documentFormat = 8;
        string randomName = DateTime.Now.Ticks.ToString();
        object htmlFilePath = outDirPath + randomName + ".htm";
        string directoryPath = outDirPath + randomName + ".files";

        object filePath = inFilePath;
        //Open the word document in background
        ApplicationClass applicationclass = new ApplicationClass();
        applicationclass.Documents.Open(ref filePath,
                                        ref readOnly,
                                        ref missingType, ref missingType, ref missingType,
                                        ref missingType, ref missingType, ref  missingType,
                                        ref missingType, ref missingType, ref isVisible,
                                        ref missingType, ref missingType, ref missingType,
                                        ref missingType, ref missingType);
        applicationclass.Visible = false;
        Document document = applicationclass.ActiveDocument;

        //Save the word document as HTML file
        document.SaveAs(ref htmlFilePath, ref documentFormat, ref missingType,
                        ref missingType, ref missingType, ref missingType,
                        ref missingType, ref missingType, ref missingType,
                        ref missingType, ref missingType, ref missingType,
                        ref missingType, ref missingType, ref missingType,
                        ref missingType);

        //Close the word document
        document.Close(ref missingType, ref missingType, ref missingType);

        #region Read the Html File as Byte Array and Display it on browser
        //byte[] bytes;
        //using (FileStream fs = new FileStream(htmlFilePath.ToString(), FileMode.Open, FileAccess.Read))
        //{
        //    BinaryReader reader = new BinaryReader(fs);
        //    bytes = reader.ReadBytes((int)fs.Length);
        //    fs.Close();
        //}
        //p.Response.BinaryWrite(bytes);
        //p.Response.Flush();
        //p.Response.End();
        #endregion

        Process process = new Process();
        process.StartInfo.UseShellExecute = true;
        process.StartInfo.FileName = htmlFilePath.ToString();
        process.Start();

        #region Delete the Html File and Diretory 删除生成的文件
        //File.Delete(htmlFilePath.ToString());
        //foreach (string file in Directory.GetFiles(directoryPath))
        //{
        //    File.Delete(file);
        //}
        //Directory.Delete(directoryPath);
        #endregion
    }
}
文本预览方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

/// <summary>
/// Summary description for TextFilePreview
/// </summary>
public class TextFilePreview
{
    public static void Preview(System.Web.UI.Page p, string inFilePath)
    {
        string fileName = inFilePath.Substring(inFilePath.LastIndexOf('\\') + 1);

        p.Response.ContentType = "text/plain";
        p.Response.ContentEncoding = System.Text.Encoding.UTF8;  //保持和文件的编码格式一致
        p.Response.AddHeader("content-disposition", "filename=" + fileName);
        p.Response.WriteFile(inFilePath);
        p.Response.End();
    }
}
 

图片预览方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

/// <summary>
/// Summary description for TextFilePreview
/// </summary>
public class TextFilePreview
{
    public static void Preview(System.Web.UI.Page p, string inFilePath)
    {
        string fileName = inFilePath.Substring(inFilePath.LastIndexOf('\\') + 1);

        p.Response.ContentType = "images/*";
        p.Response.ContentEncoding = System.Text.Encoding.UTF8;  
        p.Response.AddHeader("content-disposition", "filename=" + fileName);
        p.Response.WriteFile(inFilePath);
        p.Response.End();
    }
}
 以上的pdf,txt,图片这个三种方式在MVC下不可用,在aspx界面可用。研究后进行了更改

 

 以上是转成html进行预览,预览效果不是太好。以下是转成pdf预览代码

  1 新建windows应用程序项目
  2 
  3 添加以下com组件的引用
  4 
  5 Microsoft Word 12.0 Object Library
  6 
  7 Microsoft PowerPoint 12.0 Object Library
  8 
  9 Microsoft Excel 12.0 Object Library
 10 
 11  
 12 
 13 ------------------------------------------------------
 14 
 15 using Word = Microsoft.Office.Interop.Word; using Excel = Microsoft.Office.Interop.Excel; using PowerPoint = Microsoft.Office.Interop.PowerPoint;
 16 
 17 using Microsoft.Office.Core;
 18 
 19  
 20 
 21 我们可以使用一个枚举类型来决定生成文件的类型
 22 
 23 Word.WdExportFormat wd = Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF;
 24 
 25 Excel.XlFixedFormatType excelType = Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF; PowerPoint.PpSaveAsFileType ppType = Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsPDF;
 26 
 27 
 28   //将word文档转换成PDF格式
 29     private bool Convert(string sourcePath, string targetPath, Word.WdExportFormat exportFormat)
 30     {
 31         bool result;
 32         object paramMissing = Type.Missing;
 33         Word.ApplicationClass wordApplication = new Word.ApplicationClass();
 34         Word.Document wordDocument = null;
 35         try
 36         {
 37             object paramSourceDocPath = sourcePath;
 38             string paramExportFilePath = targetPath;
 39 
 40             Word.WdExportFormat paramExportFormat = exportFormat;
 41             bool paramOpenAfterExport = false;
 42             Word.WdExportOptimizeFor paramExportOptimizeFor =
 43                     Word.WdExportOptimizeFor.wdExportOptimizeForPrint;
 44             Word.WdExportRange paramExportRange = Word.WdExportRange.wdExportA

后来又改了几次方式。可以采用点聚的weboffice对word和excel,ppt进行展示,这样展示效果比较好。而且可以进行编辑
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

[Asp.net]常见word,excel,ppt,pdf在线预览方案,有图有真相,总有一款适合你!

引言 之前项目需要,查找了office文档在线预览的解决方案,顺便记录一下,方便以后查询。 方案一 直接在浏览器中打开Office文档在页面上的链接。会弹出如下窗口:   优点:主流浏览...

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

C# office在线预览

最近做office文档(word、ppt、excel)和pdf文件的在线预览效果,有点类似百度文库。后来找到两种可行的办法: 1:将office文档转为swf,然后通过web页面预览...

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

C#中mvc模式在线预览Office(word 可编辑、txt)文件

/// /// 在线预览Office文件 /// public class OfficeViewController : Controller { ...

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

【C#功能实现】C# 输出pdf文件流在页面上显示

1 不调用itextsharp.dll的操作         ///         /// 生成pdf流         ///         ///         p...

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:C#在线预览文档(word,excel,pdf,txt,png)
举报原因:
原因补充:

(最多只允许输入30个字)