浏览器在线预览word execl ppt image txt pdf

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="LocalhostOpenFile.aspx.cs" Inherits="LocalhostOpenFile" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div>
            <asp:Button ID="btnWord" runat="server" Text="WordPreivew" OnClick="btnWord_Click" />
        </div>
        <br />
        <div>
            <asp:Button ID="btnExel" runat="server" Text="ExcelPreivew" OnClick="btnExcel_Click" />
        </div>
        <br />
        <div>
            <asp:Button ID="btnPPt" runat="server" Text="PPtPreivew" OnClick="btnPPt_Click" />
        </div>
        <br />
        <div>
            <asp:Button ID="btnPDF" runat="server" Text="PDFPreivew" OnClick="btnPDF_Click" />
        </div>
        <br />
        <div>
            <asp:Button ID="btnTxt" runat="server" Text="TxtPreivew" OnClick="btnTxt_Click" />
        </div>
        <br />
        <div>
            <asp:Button ID="btnImage" runat="server" Text="ImagePreivew" OnClick="btnImage_Click" />
        </div>
    </div>
    </form>
</body>
</html>

cs后台代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FilePreView.Common;

/// <summary>
/// 预览本地文件
/// </summary>
public partial class LocalhostOpenFile : System.Web.UI.Page
{
    string outputDirPath = @"F:\项目\Cai\FilePreView\FilePreView\"; //Word和Excel转换成Html,Html文件存放的位置

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    /// <summary>
    /// 预览Word
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnWord_Click(object sender, EventArgs e)
    {
        string filePath = Server.MapPath("~") + @"\DemoFiles\Test.docx";
        WordPreview.Priview(this, filePath, outputDirPath);
    }

    /// <summary>
    /// 预览Execl
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnExcel_Click(object sender, EventArgs e)
    {
        string filePath = Server.MapPath("~") + @"\DemoFiles\Test.xlsx";
        ExcelPreview.Priview(this, filePath, outputDirPath);
    }

    /// <summary>
    /// 预览PPT
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnPPt_Click(object sender, EventArgs e)
    {
        string filePath = Server.MapPath("~") + @"\DemoFiles\文档浏览.pptx";
        PPTPreview.Priview(this, filePath, outputDirPath);
    }

    /// <summary>
    /// 预览Image
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnImage_Click(object sender, EventArgs e)
    {
        string filePath = Server.MapPath("~") + @"\DemoFiles\Test.png";
        ImagePreview.Preview(this, filePath);
    }
    /// <summary>
    /// 预览PDf
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnPDF_Click(object sender, EventArgs e)
    {
        string filePath = Server.MapPath("~") + @"\DemoFiles\Test.pdf";
        PDFPreview.Priview(this, filePath);
    }
    /// <summary>
    /// 预览Txt
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnTxt_Click(object sender, EventArgs e)
    {
        string filePath = Server.MapPath("~") + @"\DemoFiles\Test.txt";
        TextFilePreview.Preview(this, filePath);
    }

}

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();

    }

}

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

/// <summary>
/// 图片预览
/// </summary>
public class ImagePreview
{
    public static void Preview(System.Web.UI.Page p, string inFilePath)
    {
        p.Response.ContentType = "image/png";

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

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

/// <summary>
/// ppt预览
/// </summary>
public class PPTPreview
{
    public static void Priview(System.Web.UI.Page p, string inFilePath, string outDirPath = "")
    {
       

        #region "转换html"

        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();
        #endregion
    }

}

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

/// <summary>
/// Txt预览
/// </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 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 = "")
    {

        #region "下载Word"
        //p.Response.ContentType = "Application/msword";

        //string fileName = inFilePath.Substring(inFilePath.LastIndexOf('\\') + 1);
        //p.Response.AddHeader("content-disposition", "filename=" + fileName);
        //p.Response.WriteFile(inFilePath);
        //p.Response.End();
        #endregion

        #region "准换html"
        //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
        #endregion
        
    }
}

猜你喜欢

转载自blog.csdn.net/tone1128/article/details/89396880
今日推荐