Asp.net MVC 利用(aspose+pdfobject.js) 實現在線預覽word、excel、ppt、pdf文件


在線預覽word、excel、ppt利用aspose動態生成html

主要代碼

private bool OfficeDocumentToHtml(string sourceDoc, string saveDoc)
{
bool result = false;

//獲取文件擴展名
string docExtendName = System.IO.Path.GetExtension(sourceDoc).ToLower();
switch (docExtendName)
{
case ".doc":
case ".docx":
Aspose.Words.Document doc = new Aspose.Words.Document(sourceDoc);
doc.Save(saveDoc, Aspose.Words.SaveFormat.Html);

result = true;
break;
case ".xls":
case ".xlsx":
Workbook workbook = new Workbook(sourceDoc);
workbook.Save(saveDoc, SaveFormat.Html);

result = true;
break;
case ".ppt":
case ".pptx":
//templateFile = templateFile.Replace("/", "\\");
//string templateFile = sourceDoc;
//templateFile = templateFile.Replace("/", "\\");
PresentationEx pres = new PresentationEx(sourceDoc);
pres.Save(saveDoc, Aspose.Slides.Export.SaveFormat.Html);

result = true;
break;
default:
break;
}

return result;
}
在線預覽pdf利用pdfobject.js生成(aspose也可以做到,但效果不理想)

主要代碼

private bool PdfToHtml(string fileName, string tempFile, string saveDoc)
{
//---------------------讀html模板頁面到stringbuilder對象里----
StringBuilder htmltext = new StringBuilder();
using (StreamReader sr = new StreamReader(tempFile)) //模板頁路徑
{
String line;
while ((line = sr.ReadLine()) != null)
{
htmltext.Append(line);
}
sr.Close();
}

fileName = fileName.Replace("\\", "/");
//----------替換htm里的標記為你想加的內容
htmltext.Replace("$PDFFILEPATH", fileName);

//----------生成htm文件------------------――
using (StreamWriter sw = new StreamWriter(saveDoc, false,
System.Text.Encoding.GetEncoding("utf-8"))) //保存地址
{
sw.WriteLine(htmltext);
sw.Flush();
sw.Close();

}

return true;
}
效果圖
預覽Word

 

 

預覽excel

 

 

預覽ppt

 

 

預覽pdf

 

 


原文:https://blog.csdn.net/feter1992/article/details/53337548


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM