在线预览word、excel、ppt
原理:主要是引用第三方Dll使本地word、excel、ppt文件转换成Html
需要引用 : Aspose.Cells.dll
Aspose.Slides.dll
Aspose.Words.dll
生成Html代码private bool OfficeDocumentToHtml(string sourceDocPath, string saveDocPath)
{
bool result = fa
//获取文件扩展名
string docExtendName = System.IO.Path.GetExtension(sourceDocPath).ToLower();
switch (docExtendName)
{
case ".doc":
case ".docx":
Aspose.Words.Document doc = new Aspose.Words.Document(sourceDocPath);
doc.Save(saveDocPath, Aspose.Words.SaveFormat.Ht
result = true;
break;
case ".xls":
case ".xlsx":
Workbook workbook = new Workbook(sourceDocPath);
workbook.Save(saveDocPath, SaveFormat.Ht
result = true;
break;
case ".ppt":
case ".pptx":
//templateFile = templateFile.Replace("/", "\\");
//string templateFile = sourceDoc;
//templateFile = templateFile.Replace("/", "\\");
PresentationEx pres = new PresentationEx(sourceDocPath);
pres.Save(saveDocPath, Aspose.Slides.Export.SaveFormat.Ht
result = true;
break;
default:
break;
return result;
}
在线预览pdf
原理:利用pdfobject.js将pdf转换成Html(Aspose也可以做到,但效果不理想)
生成Html主要代码private bool PdfToHtml(string fileName, string tempFile, string saveDocPath)
{
//---------------------读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", fileNa
//----------生成htm文件------------------――
using (StreamWriter sw = new StreamWriter(saveDocPath, false,
System.Text.Encoding.GetEncoding("utf-8"))) //保存地址
{
sw.WriteLine(htmltext);
sw.Flush();
sw.Clos
return true;
}