首页 > 代码库 > flexpaper在线预览office文件,通过iis浏览没问题
flexpaper在线预览office文件,通过iis浏览没问题
项目快结项了,就差最后一个在线预览功能了。研究了两天。不通过office组件就可以把office文件转换pdf,废话不说,上图
public class Helper { /// <summary> /// word文件转换pdf /// </summary> /// <param name="wordPath">word的存储地址</param> /// <param name="wordSavePath">pdf的存储地址</param> /// <returns>返回转换成功pdf的名字</returns> public static string Word2Pdf(string wordPath, string wordSavePath) { string strPdfName=Path .GetFileName(wordSavePath); try { //此处这样写是为了防止SaveForMat不能识别是属于Aspose.Words还是Aspose.Cells。 Aspose.Words.Document dc = new Aspose.Words.Document(wordPath); dc.Save(wordSavePath, Aspose.Words.SaveFormat.Pdf); } catch (Exception) { throw; } return strPdfName; } /// <summary> /// excel文件转换pdf /// </summary> /// <param name="excelPath">excel的存储地址</param> /// <param name="excelSavePath">pdf的存储地址</param> /// <returns>返回转换成功pdf的名字</returns> public static string Excel2Pdf(string excelPath, string excelSavePath) { string strPdfName = Path.GetFileName(excelSavePath); try { Aspose.Cells.Workbook wb = new Aspose.Cells.Workbook(excelPath); wb.Save(excelSavePath, Aspose.Cells.SaveFormat.Pdf); } catch(Exception) { throw; } return strPdfName; } /// <summary> /// Pdf文件转化为Swf /// </summary> /// <param name="swfTools">转化工具路径。例如:...\02_SWFTools\pdf2swf.exe</param> /// <param name="pdfPath">pdf文件目录。例如:...\UploadFiles\Test.pdf</param> /// <param name="pdfFileName">pdf文件名。例如:Test.pdf</param> /// <param name="desPath">保存swf路径。例如:...\SwfFiles\test.swf</param> /// <returns>转换成功的swf文件。例如:Test.swf</returns> public static string PdfToSwf(string swfTools, string pdfPath, string pdfFileName, string swfsavePath) { //转换成功之后的swf文件名字。例如:test.swf string strSwf = Path.GetFileNameWithoutExtension(pdfFileName) + ".swf"; //命令语句 string strCmdStr = " -t \"" + pdfPath + "\" -s flashversion=9 -o \"" + swfsavePath + "\""; try { PdfToSwfHelper(swfTools, strCmdStr); } catch (Exception) { throw; } return strSwf; } /// <summary> /// /// </summary> /// <param name="toolsPath"></param> /// <param name="cmd"></param> /// <returns></returns> public static bool PdfToSwfHelper(string toolsPath, string cmd) { bool iss = false; //判断是否转换成功,默认失败 try { using (Process p = new Process()) { ProcessStartInfo psi = new ProcessStartInfo(toolsPath, cmd); psi.WindowStyle = ProcessWindowStyle.Hidden; p.StartInfo = psi; p.Start(); p.WaitForExit(); iss = true; //转换成功 } } catch (Exception) { iss = false; throw; } return iss; } }
protected void BtnToPdf_Click(object sender, EventArgs e) { #region 属性 string strPdfName = string.Empty; //生成的pdf文件名 string strSwfName = string.Empty; //生成的swf文件名 string strOfficeOrPdfPilePath = Server.MapPath("~/UploadFiles/"); //上传的Office或者Pdf文件的存储路径 string strSwfFilePath = Server.MapPath("~/SwfFiles/"); //pdf文件通过pdf2swf.exe转换成swf文件的存储路径 string strPdf2swfToolPath = Server.MapPath("~/02_SWFTools/pdf2swf.exe"); //pdf2swf.exe的路径,为pdf文件转swf文件做准备 string strUploadFileFullName = string.Empty; //上传文件的全名 string strPdfSaveFullPath = string.Empty; //office文件转换成pdf的保存地址 string strSwfSaveFullPath = string.Empty; //pdf文件转换成swf的保存地址 #endregion //如果此控件有文件 if (FileUpload1.HasFile) { //纯文件名。例如:zjq.doc string strUploadFileName = this.FileUpload1.FileName; //获取文件名字,不包括格式。例如:zjq string strUploadFileNameWithoutEt = Path.GetFileNameWithoutExtension(strUploadFileName).ToLower(); //获取上传的文件格式。例如:.doc string strUploadFileType = Path.GetExtension(strUploadFileName).ToLower(); //判断上传文件类型 if (strUploadFileType == ".doc" || strUploadFileType == ".docx" || strUploadFileType == ".xls" || strUploadFileType == ".xlsx" || strUploadFileType == ".pdf") { //创建OfficeOrPdf文件夹 if (!Directory.Exists(strOfficeOrPdfPilePath)) { Directory.CreateDirectory(strOfficeOrPdfPilePath); } //创建swf文件夹 if (!Directory.Exists(strSwfFilePath)) { Directory.CreateDirectory(strSwfFilePath); } //TODO:上传文件的全地址,为了保存使用。此处修改时间戳(maybe?) strUploadFileFullName = strOfficeOrPdfPilePath + strUploadFileName; //TODO:生成的pdf文件要保存的目标地址,此处修改时间戳(maybe?) strPdfSaveFullPath = strOfficeOrPdfPilePath + strUploadFileNameWithoutEt + ".pdf"; //TODO:生成的swff文件要保存的目标地址,此处修改时间戳(maybe?) strSwfSaveFullPath = strSwfFilePath + strUploadFileNameWithoutEt + ".swf"; //第一步:保存文件 FileUpload1.PostedFile.SaveAs(strUploadFileFullName); try { //上传的文件是word if (strUploadFileType == ".doc" || strUploadFileType == ".docx") { //第二步:word转换成pdf strPdfName = Helper.Word2Pdf(strUploadFileFullName, strPdfSaveFullPath); } else if (strUploadFileType == ".xls" || strUploadFileType == ".xlsx") { //excel转换成pdf strPdfName = Helper.Excel2Pdf(strUploadFileFullName, strPdfSaveFullPath); } else if (strUploadFileType == ".pdf") { strPdfName = Path.GetFileName(strUploadFileFullName); } //判断Pdf中是否有指定的pdf文件 if (File.Exists(strPdfSaveFullPath)) { //第三步:pdf转换swf,最后存储到SwfFiles文件夹中 strSwfName = Helper.PdfToSwf(strPdf2swfToolPath, strPdfSaveFullPath, strPdfName, strSwfSaveFullPath); } //如果swf存在并且不为空 if (!string.IsNullOrEmpty(strSwfName) && File.Exists(strSwfFilePath + strSwfName)) { //显示swf文件 this.HiddenField1.Value = http://www.mamicode.com/"SwfFiles/" + strSwfName; } } catch (Exception ex) { Response.Write("<script>alert(‘" + ex.Message.ToString() + "‘)</script>"); } } } } }
flexpaper在线预览office文件,通过iis浏览没问题
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。