office转换为还原度高的html,使用Aspose把office文件转换为Html文件及生成文件浏览乱码的解决...

使用Aspose把office文件转换为Html 文件转换方法如下:

/// /// office文件转换为Html

///

/// 扩展名

/// 源文件路径

/// 目标文件

///

public static string FileToHtmlFile(string extFileName, string sourceFile, string desFile)

{

if (string.IsNullOrEmpty(sourceFile))

{

return "0";//没有文件

}

switch (extFileName.ToUpper())

{

case "PPT":

case "PPTX":

Aspose.Slides.Presentation ppt = new Aspose.Slides.Presentation(sourceFile);

ppt.Save(desFile, Aspose.Slides.Export.SaveFormat.Html);

break;

case "DOC":

case "DOCX":

Aspose.Words.Document doc = new Aspose.Words.Document(sourceFile);

doc.Save(desFile, Aspose.Words.SaveFormat.Html);

break;

//case "XLS":

//case "XLSX":

// break;

}

return "ok";

}

PPT文件生成的html 会出现编码识别错误 导致浏览器网页乱码

解决方法如下,

System.IO.StreamReader sr = new System.IO.StreamReader(strFilePath);

string html = sr.ReadToEnd();

sr.Close();

System.IO.StreamWriter sw = new System.IO.StreamWriter(strFilePath, false);

//System.IO.StreamWriter sw = new System.IO.StreamWriter(strFilePath, false, Encoding.UTF8);

//特别重要:添加编码标志,解决浏览器识别错误

html = html.Replace("", "");

//去除试用标志

html = html.Replace("Evaluation only", "");

html = html.Replace("Created with Aspose.Slides for .NET 2.0 14.8.1.0.", "");

html = html.Replace("Copyright 2004-2014 Aspose Pty Ltd.", "");

//html = html.Replace("Evaluation Only. Created with Aspose.Words. Copyright 2003-2014 Aspose Pty Ltd.", "");

html = html.Replace("Evaluation Only. Created with Aspose.Words. Copyright 2003-2014 Aspose Pty Ltd.", "");

html = html.Replace("This document was truncated here because it was created using Aspose.Words in Evaluation Mode.", "");

// < meta http - equiv = "X-UA-Compatible" content = "IE=9" >< meta http - equiv = "Content-Type" content = "text/html; charset=utf-8" />

sw.Write(html);

sw.Close();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值