在線預覽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
