string fileId = context.Request.QueryString["FileId"];
// fileId = "d084cf5ac0c44aa380558a7232da5385";
if (fileId != null && fileId.Length > 0)
{
IFileService fileComponent = ServiceContainer.GetService<IFileService>();
//获取文件头信息
MetaFileEntity metaFileEntity = fileComponent.GetMetaFile(fileId);
if (metaFileEntity == null)
throw new Exception();
string previewPath = "/"+ConfigurationManager.AppSettings["PreviewPath"];
string path = context.Server.MapPath(previewPath);
if (System.IO.Directory.Exists(path) == false)
{
Directory.CreateDirectory(path);
}
previewPath += fileId + ".html";
string strSaveFileName = path + fileId + ".html";

 

string strSaveFileNameTmp = path + "tmp-" + fileId + ".html";

string srcPath = metaFileEntity.FullName;
string suffixal = metaFileEntity.Suffixal.ToLower();

try
{
bool ok = false;
if (File.Exists(strSaveFileName) == false)
{

FileStream fs = new FileStream(srcPath, FileMode.Open);
if (suffixal == "doc" || suffixal == "docx")
{
Document doc = new Document(fs);
Aspose.Words.Saving.HtmlFixedSaveOptions hso = new Aspose.Words.Saving.HtmlFixedSaveOptions();
doc.Save(strSaveFileName, hso);
fs.Close();
ok = true;
}
else if (suffixal == "xls" || suffixal == "xlsx")
{
#region excel
try
{
Workbook wb = new Workbook(srcPath);
wb.Save(strSaveFileName, Aspose.Cells.SaveFormat.Html);
fs.Close();

string strAll = File.ReadAllText(strSaveFileName);
strAll = strAll.Replace("white-space:nowrap", "white-space:normal");
if (strAll.IndexOf("<style>") > 0)
{
strAll = strAll.Insert(strAll.IndexOf("<style>")+7, "tbody>tr:first-child>td:first-child {white-space: nowrap;}");
}
File.WriteAllText(strSaveFileName, strAll);
ok = true;
}
catch (Exception exx)
{
HY.Common.Utility.Utils.Logger.Log.Error(exx.Message, exx);
HY.Common.Utility.Utils.Logger.Log.Info("非正常的xls格式文件,尝试用TxtLoadOptions 方式加载重试.");

TxtLoadOptions options = new TxtLoadOptions(Aspose.Cells.LoadFormat.CSV);
options.Encoding = System.Text.Encoding.GetEncoding("GB2312");
options.ConvertNumericData = false;

Workbook wb = new Workbook(srcPath, options);

wb.Save(strSaveFileNameTmp, Aspose.Cells.SaveFormat.Html);
fs.Close();

StringBuilder strhtml = new StringBuilder();
try
{
//创建StreamReader对象
using (StreamReader sr = new StreamReader(strSaveFileNameTmp, System.Text.Encoding.GetEncoding("GB2312")))
{
String oneline;

//读取指定的HTML文件
while ((oneline = sr.ReadLine()) != null)
{
strhtml.Append(oneline);
}
sr.Close();
}
}
catch (Exception err)
{
HY.Common.Utility.Utils.Logger.Log.Error(err.Message, err);
}

strhtml = strhtml.Replace("&lt;", "<").Replace("&gt;", ">").Replace("&amp;nbsp;", " ").Replace("white-space:nowrap", "white-space:normal");

using (FileStream fs2 = new FileStream(strSaveFileName, FileMode.Create))
{
StreamWriter sw = new StreamWriter(fs2, System.Text.Encoding.UTF8);
//开始写入
sw.Write(strhtml);
//清空缓冲区
sw.Flush();
//关闭流
sw.Close();
fs2.Close();

File.Delete(strSaveFileNameTmp);//删除临时文件
}

ok = true;

}
#endregion
}
else if (suffixal == "txt" || suffixal == "log")
{
#region text
HY.Common.Utility.Utils.Logger.Log.Info("非正常的txt格式文件,尝试用TxtLoadOptions 方式加载重试.");

TxtLoadOptions options = new TxtLoadOptions(Aspose.Cells.LoadFormat.CSV);
options.Encoding = System.Text.Encoding.GetEncoding("GB2312");
options.ConvertNumericData = false;

Workbook wb = new Workbook(srcPath, options);

wb.Save(strSaveFileNameTmp, Aspose.Cells.SaveFormat.Html);
fs.Close();

StringBuilder strhtml = new StringBuilder();
try
{
//创建StreamReader对象
using (StreamReader sr = new StreamReader(strSaveFileNameTmp, System.Text.Encoding.GetEncoding("GB2312")))
{
String oneline;

//读取指定的HTML文件
while ((oneline = sr.ReadLine()) != null)
{
strhtml.Append(oneline);
}
sr.Close();
}
}
catch (Exception err)
{
HY.Common.Utility.Utils.Logger.Log.Error(err.Message, err);
}

strhtml = strhtml.Replace("&lt;", "<").Replace("&gt;", ">").Replace("&amp;nbsp;", " ");

using (FileStream fs2 = new FileStream(strSaveFileName, FileMode.Create))
{
StreamWriter sw = new StreamWriter(fs2, System.Text.Encoding.UTF8);
//开始写入
sw.Write(strhtml);
//清空缓冲区
sw.Flush();
//关闭流
sw.Close();
fs2.Close();

File.Delete(strSaveFileNameTmp);//删除临时文件
}

ok = true;
#endregion
}
else if (suffixal == "pdf")
{

string Key = ConfigurationManager.AppSettings["AsposePdfLicnese"];
Stream LStream = (Stream)new MemoryStream(Convert.FromBase64String(Key));
var l = new Aspose.Pdf.License();
l.SetLicense(LStream);

Aspose.Pdf.Document pdd = new Aspose.Pdf.Document(fs);
Aspose.Pdf.HtmlSaveOptions saveOptions = new Aspose.Pdf.HtmlSaveOptions();
saveOptions.FixedLayout = true;
saveOptions.SplitIntoPages = false;
saveOptions.RasterImagesSavingMode = Aspose.Pdf.HtmlSaveOptions.RasterImagesSavingModes.AsPngImagesEmbeddedIntoSvg;
pdd.Save(strSaveFileName, Aspose.Pdf.SaveFormat.Html);
fs.Close();
ok = true;
}
else
{
Directory.CreateDirectory(strSaveFileName.Substring(0, strSaveFileName.LastIndexOf("\\")));
File.Copy(srcPath, strSaveFileName);
ok = true;
}
}
else
{
//文件已存在
ok = true;
}
if (ok) //转换成功
{
context.Response.Redirect(previewPath,false);
}
else
{
context.Response.Write("不能解析的文件.");
}
}
catch (Exception ex)
{
Logger.Log.Error("文件预览错误:"+ex.ToString());
context.Response.Write("文件预览错误:"+ex.ToString());
}

}

posted on 2018-06-13 10:18  刘祥伟  阅读(504)  评论(0编辑  收藏  举报