#region 在线预览功能
string filePath = _filePhysicalAddress;
if (System.IO.File.Exists(filePath) == false)
{
HttpContext.Current.Response.Write("下载失败:文件不存在。");
HttpContext.Current.Response.End();
return false;
}
//替换成功后的在线预览地址
string url = ConfigurationManager.AppSettings["FileHttp"] + System.IO.Path.GetFileName(filePath);
//文件转换失败之后的地址
string missUrl = url;
//文件后缀名
string extension = Path.GetExtension(filePath);
switch (extension.ToUpper())
{
case ".TXT":
case ".PNG":
case ".JPG":
case ".GIF":
case ".JPEG":
HttpContext.Current.Response.Redirect(url);
break;
case ".PDF":
url = PDFConvertToHtml.PDFHtml(filePath);
if (string.IsNullOrEmpty(url) == false)
{
HttpContext.Current.Response.Redirect(url);
}
else
{
HttpContext.Current.Response.Redirect(missUrl);
}
break;
case ".DOC":
case ".DOCX":
url = WordConvertToHtml.Word2Html(filePath);
if (string.IsNullOrEmpty(url) == false)
{
HttpContext.Current.Response.Redirect(url);
}
else
{
HttpContext.Current.Response.Redirect(missUrl);
}
break;
case ".XLSX":
case ".XLS":
url = ExcelConvertToHtml.Excel2Html(filePath);
if (string.IsNullOrEmpty(url) == false)
{
HttpContext.Current.Response.Redirect(url);
}
else
{
HttpContext.Current.Response.Redirect(missUrl);
}
break;
case ".PPT":
case ".PPTX":
url = PPTConvertToHtml.PPT2Html(filePath);
if (string.IsNullOrEmpty(url) == false)
{
HttpContext.Current.Response.Redirect(url);
}
else
{
HttpContext.Current.Response.Redirect(missUrl);
}
break;
}
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Configuration;
namespace BRC.Web.Modules.Uploader.AppCode.FileConvert
{
public class ExcelConvertToHtml
{
/// <summary>
/// Excel转成Html
/// </summary>
/// <param name="sourcePath">文件路径</param>
/// <returns></returns>
public static string Excel2Html(string sourcePath)
{
//新地址
string newUrl = "";
Microsoft.Office.Interop.Excel.Application repExcel = null;
Microsoft.Office.Interop.Excel.Workbook workbook = null;
Microsoft.Office.Interop.Excel.Worksheet worksheet = null;
try
{
//目录
string directtory = Path.GetDirectoryName(sourcePath);
//名字
string fileName = Path.GetFileName(sourcePath);
//扩展名
string ext = Path.GetExtension(sourcePath);
//无扩张名名字
string noExtName = fileName.Replace(ext, "");
//新目录
string newDir = directtory + "\\" + noExtName;
//新地址
newUrl = noExtName + "/" + FileConvertEmun.HtmlExcel_.ToString() + noExtName + ".html";
//新名字,接口
string saveName = Path.Combine(newDir, FileConvertEmun.HtmlExcel_.ToString() + noExtName + ".html");
if (System.IO.File.Exists(saveName))
{
return ConfigurationManager.AppSettings["FileHttp"] + newUrl;
}
if (!System.IO.Directory.Exists(newDir))
{
System.IO.Directory.CreateDirectory(newDir);
}
repExcel = new Microsoft.Office.Interop.Excel.Application();
workbook = repExcel.Application.Workbooks.Open(sourcePath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];
workbook.SaveAs(saveName, Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
workbook.Close(false, Type.Missing, Type.Missing);
repExcel.Quit();
}
catch (Exception)
{
return "";
}
finally
{
repExcel = null;
workbook = null;
worksheet = null;
System.GC.Collect();
}
return ConfigurationManager.AppSettings["FileHttp"] + newUrl;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace BRC.Web.Modules.Uploader.AppCode.FileConvert
{
/// <summary>
///
/// </summary>
public enum FileConvertEmun
{
/// <summary>
/// 水印文件前缀
/// </summary>
WaterPDF_,
/// <summary>
/// 预览PDF
/// </summary>
HtmlPDF_,
/// <summary>
/// 预览Excel
/// </summary>
HtmlExcel_,
/// <summary>
/// 预览Word
/// </summary>
HtmlWord_,
/// <summary>
/// 预览PPT
/// </summary>
HtmlPPT_
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
namespace BRC.Web.Modules.Uploader.AppCode.FileConvert
{
public class FilesNameComparer : IComparer
{
// Calls CaseInsensitiveComparer.Compare with the parameters reversed.
///<summary>
///比较两个字符串,如果含用数字,则数字按数字的大小来比较。
///</summary>
///<param name="x"></param>
///<param name="y"></param>
///<returns></returns>
int IComparer.Compare(Object x, Object y)
{
if (x == null || y == null)
throw new ArgumentException("Parameters can't be null");
string fileA = x as string;
string fileB = y as string;
fileA = fileA.Split('\\').Last();
fileB = fileB.Split('\\').Last();
char[] arr1 = fileA.ToCharArray();
char[] arr2 = fileB.ToCharArray();
int i = 0, j = 0;
while (i < arr1.Length && j < arr2.Length)
{
if (char.IsDigit(arr1[i]) && char.IsDigit(arr2[j]))
{
string s1 = "", s2 = "";
while (i < arr1.Length && char.IsDigit(arr1[i]))
{
s1 += arr1[i];
i++;
}
while (j < arr2.Length && char.IsDigit(arr2[j]))
{
s2 += arr2[j];
j++;
}
if (int.Parse(s1) > int.Parse(s2))
{
return 1;
}
if (int.Parse(s1) < int.Parse(s2))
{
return -1;
}
}
else
{
if (arr1[i] > arr2[j])
{
return 1;
}
if (arr1[i] < arr2[j])
{
return -1;
}
i++;
j++;
}
}
if (arr1.Length == arr2.Length)
{
return 0;
}
else
{
return arr1.Length > arr2.Length ? 1 : -1;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using O2S.Components.PDFRender4NET;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections;
using System.Configuration;
using HtmlAgilityPack;
namespace BRC.Web.Modules.Uploader.AppCode.FileConvert
{
public class PDFConvertToHtml
{
/// <summary>
/// pdf转成Html
/// </summary>
/// <param name="sourcePath">原文件</param>
/// <returns></returns>
public static string PDFHtml(string sourcePath)
{
//目录
string directtory = Path.GetDirectoryName(sourcePath);
//名字
string fileName = Path.GetFileName(sourcePath);
//扩展名
string ext = Path.GetExtension(sourcePath);
//无扩张名名字
string noExtName = fileName.Replace(ext, "");
//新目录
string newDir = directtory + "\\" + noExtName;
string newUrl = noExtName + "/" + FileConvertEmun.HtmlPDF_.ToString() + noExtName + ".html";
//新名字,接口
string saveName = Path.Combine(newDir, FileConvertEmun.HtmlPDF_.ToString() + noExtName + ".html");
if (System.IO.File.Exists(saveName))
{
return ConfigurationManager.AppSettings["FileHttp"] + newUrl;
}
PDFFile pdfFile = null;
try
{
pdfFile = PDFFile.Open(sourcePath);
if (!System.IO.Directory.Exists(newDir))
{
System.IO.Directory.CreateDirectory(newDir);
}
int startPageNum = 1;
int endPageNum = pdfFile.PageCount;
// start to convert each page
for (int i = startPageNum; i <= endPageNum; i++)
{
//设置图片的清晰度,数字越大越清晰 One = 1, Two = 2, Three = 3, Four = 4, Five = 5, Six = 6, Seven = 7, Eight = 8, Nine = 9, Ten = 10
Bitmap pageImage = pdfFile.GetPageImage(i - 1, 56 * 2);
pageImage.Save(newDir + "\\Page" + i + "." + ImageFormat.Png.ToString(), ImageFormat.Png);
pageImage.Dispose();
}
pdfFile.Dispose();
}
catch (Exception)
{
return "";
}
finally
{
pdfFile = null;
System.GC.Collect();
}
try
{
var directoryInfo = new System.IO.DirectoryInfo(newDir);
var pics = new Queue<string>();
var fileList = directoryInfo.GetFiles();
ArrayList fileArray = new ArrayList();
foreach (var dir in fileList)
{
if (dir.Name.ToUpper().Contains(".PNG") == false)
{
continue;
}
fileArray.Add(newDir + "\\" + dir.Name);
}
fileArray.Sort(new FilesNameComparer());
foreach (var dir in fileArray)
{
//地址
pics.Enqueue(ConfigurationManager.AppSettings["FileHttp"] +noExtName+"/"+ System.IO.Path.GetFileName(dir.ToString()));
}
//图片生成了需要拼成为网站
string html = "<!DOCTYPE html><html lang=\"en\" xmlns=\"http://www.w3.org/1999/xhtml\"><head><title>HtmlPDF</title><meta charset=\"utf-8\" /></head><body></body></html>";
var htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(html);
var node = htmlDocument.DocumentNode.Descendants("body").FirstOrDefault();
if (node != null)
{
foreach (var pic in pics)
{
HtmlNode node0 = htmlDocument.CreateElement("hr");
HtmlNode node1 = htmlDocument.CreateElement("div");
HtmlNode node2 = htmlDocument.CreateElement("img");
node2.Attributes.Add("src", pic);
node1.AppendChild(node2);
node.AppendChild(node1);
node.AppendChild(node0);
}
}
htmlDocument.Save(saveName, System.Text.Encoding.UTF8);
}
catch (Exception)
{
return "";
}
return ConfigurationManager.AppSettings["FileHttp"] + newUrl;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Configuration;
using HtmlAgilityPack;
namespace BRC.Web.Modules.Uploader.AppCode.FileConvert
{
public class PPTConvertToHtml
{
/// <summary>
/// ppt转成Html
/// </summary>
/// <param name="sourcePath"></param>
/// <returns></returns>
public static string PPT2Html(string sourcePath)
{
//目录
string directtory = Path.GetDirectoryName(sourcePath);
//名字
string fileName = Path.GetFileName(sourcePath);
//扩展名
string ext = Path.GetExtension(sourcePath);
//无扩张名名字
string noExtName = fileName.Replace(ext, "");
//新目录
string newDir = directtory + "\\" + noExtName;
//新名字,接口
string saveName = Path.Combine(newDir, FileConvertEmun.HtmlPPT_.ToString() + noExtName + ".html");
string saveJPG = Path.Combine(newDir, FileConvertEmun.HtmlPPT_.ToString() + noExtName + ".jpg");
//新目录
string newUrl = noExtName + "/" + FileConvertEmun.HtmlPPT_.ToString() + noExtName + ".html";
if (System.IO.File.Exists(saveName))
{
return ConfigurationManager.AppSettings["FileHttp"] + newUrl;
}
if (!System.IO.Directory.Exists(newDir))
{
System.IO.Directory.CreateDirectory(newDir);
}
Microsoft.Office.Interop.PowerPoint.Application ppApp = null;
Microsoft.Office.Interop.PowerPoint.Presentation prsPres = null;
try
{
ppApp = new Microsoft.Office.Interop.PowerPoint.Application();
prsPres = ppApp.Presentations.Open(sourcePath, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse);
prsPres.SaveAs(saveJPG, Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsJPG, Microsoft.Office.Core.MsoTriState.msoTrue);
prsPres.Close();
ppApp.Quit();
}
catch (Exception)
{
return "";
}
finally
{
ppApp = null;
prsPres = null;
System.GC.Collect();
}
var directoryInfo = new System.IO.DirectoryInfo(newDir + "\\" + FileConvertEmun.HtmlPPT_.ToString() + noExtName);
var pics = new Queue<string>();
foreach (var dir in directoryInfo.GetFiles())
{
if (dir.Name.ToUpper().Contains(".JPG") == false)
{
continue;
}
pics.Enqueue(ConfigurationManager.AppSettings["FileHttp"] + noExtName + "/" + FileConvertEmun.HtmlPPT_.ToString() + noExtName + "/" + dir.Name);
}
//图片生成了需要拼成为网站
string html = "<!DOCTYPE html><html lang=\"en\" xmlns=\"http://www.w3.org/1999/xhtml\"><head><title>HtmlPPT</title><meta charset=\"utf-8\" /></head><body></body></html>";
var htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(html);
var node = htmlDocument.DocumentNode.Descendants("body").FirstOrDefault();
if (node != null)
{
foreach (var pic in pics)
{
HtmlNode node0 = htmlDocument.CreateElement("hr");
HtmlNode node1 = htmlDocument.CreateElement("div");
HtmlNode node2 = htmlDocument.CreateElement("img");
node2.Attributes.Add("src", pic);
node1.AppendChild(node2);
node.AppendChild(node1);
node.AppendChild(node0);
}
}
htmlDocument.Save(saveName, System.Text.Encoding.UTF8);
return ConfigurationManager.AppSettings["FileHttp"] + newUrl;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Configuration;
namespace BRC.Web.Modules.Uploader.AppCode.FileConvert
{
public class WordConvertToHtml
{
/// <summary>
/// Word转成Html
/// </summary>
/// <param name="sourcePath"></param>
/// <returns></returns>
public static string Word2Html(string sourcePath)
{
Microsoft.Office.Interop.Word.ApplicationClass word = null;
Microsoft.Office.Interop.Word.Documents docs = null;
Microsoft.Office.Interop.Word.Document doc = null;
Type wordType = null;
Type docType = null;
//新地址
string newUrl="";
try
{
//目录
string directtory = Path.GetDirectoryName(sourcePath);
//名字
string fileName = Path.GetFileName(sourcePath);
//扩展名
string ext = Path.GetExtension(sourcePath);
//无扩张名名字
string noExtName = fileName.Replace(ext, "");
//新目录
string newDir = directtory + "\\" + noExtName;
//新地址
newUrl = noExtName + "/" + FileConvertEmun.HtmlWord_.ToString() + noExtName + ".html";
//新名字,接口
string saveName = Path.Combine(newDir, FileConvertEmun.HtmlWord_.ToString() + noExtName + ".html");
if (System.IO.File.Exists(saveName))
{
return ConfigurationManager.AppSettings["FileHttp"] + newUrl;
}
if (!System.IO.Directory.Exists(newDir))
{
System.IO.Directory.CreateDirectory(newDir);
}
word = new Microsoft.Office.Interop.Word.ApplicationClass();
wordType = word.GetType();
docs = word.Documents;
Type docsType = docs.GetType();
doc = (Microsoft.Office.Interop.Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { (object)sourcePath, true, true });
docType = doc.GetType();
object saveFileName = (object)saveName;
docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML });
docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
}
catch (Exception)
{
return "";
}
finally
{
word = null;
docs = null;
doc = null;
wordType = null;
docType = null;
System.GC.Collect();
}
return ConfigurationManager.AppSettings["FileHttp"] + newUrl;
}
}
}
Mic office 在线预览
最新推荐文章于 2024-09-25 14:15:38 发布