MVC 中预览各种文件(word ppt execl png txt scv....)

本文介绍了一种文件预览方法,支持多种文件格式包括图片、文档、表格和演示文稿等,通过将非PDF格式文件转换为PDF,以便于在网页上进行预览。文章详细解释了如何根据不同文件类型设置响应头,以及使用第三方库进行文件转换的过程。
摘要由CSDN通过智能技术生成
    /// <summary>
    /// 文件预览
    /// </summary>
    /// <param name="fileInfo">文件名</param>
    /// <returns></returns>
    public void PreviewFile(string fileInfo)
    {
        string filepath = 文件路径+fileInfo;
        string filepath = DirFileHelper.GetAbsolutePath(fileInfo);                //路径 
        string FileType = Path.GetExtension(filename.ToLower()).Trim('.');              //文件类型
        filename = filepath;
        if (FileType == "xlsx" || FileType == "xls")
        {
            filename = filepath.Substring(0, filepath.LastIndexOf(".")) + ".pdf";       //新文件名
            if (!DirFileHelper.IsExistFile(filename))
            {
                GetExcelData(DirFileHelper.GetAbsolutePath(data.MODELPATH));
            }
        }
        if (FileType == "docx" || FileType == "doc")
        {
            filename = filepath.Substring(0, filepath.LastIndexOf(".")) + ".pdf";       //新文件名
            if (!DirFileHelper.IsExistFile(filename))
            {
                GetExcelData(DirFileHelper.GetAbsolutePath(data.MODELPATH));
            }
        }
        if (FileType == "ppt" || FileType == "pptx")
        {
            filename = filepath.Substring(0, filepath.LastIndexOf(".")) + ".pdf";       //新文件名
            if (!DirFileHelper.IsExistFile(filename))
            {
               GetExcelData(DirFileHelper.GetAbsolutePath(data.MODELPATH));
            }
        }
        Response.ClearContent();
        switch (FileType)
        {
            case "jpg":
                Response.ContentType = "image/jpeg";
                break;
            case "gif":
                Response.ContentType = "image/gif";
                break;
            case "png":
                Response.ContentType = "image/png";
                break;
            case "bmp":
                Response.ContentType = "application/x-bmp";
                break;
            case "jpeg":
                Response.ContentType = "image/jpeg";
                break;
            case "doc":
                Response.ContentType = "application/pdf";
                break;
            case "docx":
                Response.ContentType = "application/pdf";
                break;
            case "ppt":
                Response.ContentType = "application/pdf";
                break;
            case "pptx":
                Response.ContentType = "application/pdf";
                break;
            case "xls":
                Response.ContentType = "application/pdf";
                break;
            case "xlsx":
                Response.ContentType = "application/pdf";
                break;
            case "pdf":
                Response.ContentType = "application/pdf";
                break;
            case "txt":
                Response.ContentType = "text/plain";
                break;
            case "csv":
                Response.ContentType = "";
                break;
            default:
                Response.ContentType = "application/pdf";
                break;
        }
        Response.Charset = "GB2312";
        Response.WriteFile(filename);
    }

///
/// 获取EXCEL数据
///
///
public void GetExcelData(string path)
{
try
{
//office2Pdf.ExcelConvertPDF(path);//此处可在服务器安装office2007通过offic2007转换
//load Excel file
Workbook workbook = new Workbook();
workbook.LoadFromFile(path);
workbook.SaveToFile(path.Substring(0, path.LastIndexOf(".")) + “.pdf”, Spire.Xls.FileFormat.PDF);
}
catch (Exception)
{
throw (new Exception(“文件丢失”));
}
}

    /// <summary>
    /// 获取Word数据
    /// <summary>
    /// <returns></returns>
    public void GetWordData(string path)
    {
        try
        {
            //office2Pdf.WordConvertPDF(path);此处可在服务器安装office2007通过offic2007转换
            Document document = new Document();
            document.LoadFromFile(path);
            document.SaveToFile(path.Substring(0, path.LastIndexOf(".")) + ".pdf", Spire.Doc.FileFormat.PDF);
        }
        catch (Exception)
        {
            throw (new Exception("文件丢失"));
        }
    }
    /// <summary>
    /// 获取Word数据
    /// <summary>
    /// <returns></returns>
    public void GetPptData(string path)
    {
        try
        {
            // office2Pdf.PPTConvertPDF(path);office2Pdf.WordConvertPDF(path);此处可在服务器安装office2007通过offic2007转换
            Document document = new Document();
            document.LoadFromFile(path);
            document.SaveToFile(path.Substring(0, path.LastIndexOf(".")) + ".pdf", Spire.Doc.FileFormat.PDF);
        }
        catch (Exception)
        {
            throw (new Exception("文件丢失"));
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值