在线预览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