本文主要是介绍MVC 中预览各种文件(word ppt execl png txt scv....),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
/// <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("文件丢失"));}}
这篇关于MVC 中预览各种文件(word ppt execl png txt scv....)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!