office文件在线预览,模仿网易邮箱在线预览的

时间:2021-10-21 04:30:59

最近研究了半天,代码是倾情奉送啊,C#,asp.net的

这个原理是office文件转换为PDF文件,然后再转换成SWF文件,FlexPaper+swfTools。

有个问题,需要在web.config中加这么一行<identity impersonate="true" userName="administrator" password="你的服务器登录密码" />

  /// <summary>
/// 转换压缩文件,以便于预览(图片大小调整,office、pdf转成swf)
/// </summary>
/// <param name="sfilePath">真实文件路径(虚拟),含文件名,精确到网站根目录</param>
/// <param name="sfileExtention">真实的文件扩展名,要判断如何转换</param>
/// <param name="showWait">真是否显示提示等待转换信息,默认不提示</param>
public static void FileConvert(string sfilePath, string sfileExtention, bool showWait = false)
{
string PfilePath = HttpContext.Current.Server.MapPath(Path.GetDirectoryName(sfilePath));//当前操作的物理路径
string PfileName = Path.GetFileName(sfilePath);// 当前操作的文件名
string PfileExtention = sfileExtention.ToLower();// 当前操作的文件扩展名
if (File.Exists(PfilePath + "\\swf\\" + PfileName + ".swf"))
{ return; }
else
{
if (!Directory.Exists(PfilePath + "\\swf\\")) { Directory.CreateDirectory((PfilePath + "\\swf\\")); }
try
{
switch (DocuType(PfileExtention))
{
case TypeOfDocu.office:
if (showWait)
{
System.Text.StringBuilder StrB = new System.Text.StringBuilder();
StrB.Append("<div style='color: #0000CC; font-size: 14px;'>文档需转换格式,才可以在线预览。<br /><br />转换中,马上就好,请稍后……</div>");
StrB.Append("<div style='color:white;'>0000000000000000000000000000000000000000000000000您是第一个浏览的人</div>");//兼容IE256字符才显示,这里180就可以
HttpContext.Current.Response.Write(StrB.ToString());
HttpContext.Current.Response.Flush();
}
//将office文件转换成PDF,保存到swf文件夹下
string pdfFileName = PfileName + ".pdf";
string fileOutPath = PfilePath + "\\swf\\" + pdfFileName;
ExportPdf(PfilePath + "\\" + PfileName, fileOutPath, PfileExtention);
//切记,使用pdf2swf.exe 打开的文件名之间不能有空格,否则会失败
string cmdStr = HttpContext.Current.Server.MapPath("~/SWF/pdf2swf.exe");
string savePath = PfilePath + "\\swf\\";
//string saveSWFPath = HttpContext.Current.Server.MapPath("~/SWF/");
//将PDF文件转换成SWF格式文件
string sourcePath = @"""" + savePath + pdfFileName + @"""";//要转换的pdf文件路径
string targetPath = @"""" + savePath + PfileName + ".swf" + @"""";//转换之后swf文件存放的目标路径
//@"""" 四个双引号得到一个双引号,如果你所存放的文件所在文件夹名有空格的话,要在文件名的路径前后加上双引号,才能够成功
// -t 源文件的路径
// -s 参数化(也就是为pdf2swf.exe 执行添加一些窗外的参数(可省略))
string argsStr = " -p 1-100 -t " + sourcePath + " -s flashversion=9 -o " + targetPath;
//执行pdf到swf的转换
ExcutedCmd(cmdStr, argsStr);
File.Delete(savePath + pdfFileName);
break;
case TypeOfDocu.pdf:
cmdStr = HttpContext.Current.Server.MapPath("~/SWF/pdf2swf.exe");
sourcePath = @"""" + PfilePath + "\\" + PfileName + @"""";
targetPath = @"""" + PfilePath + "\\swf\\" + PfileName + ".swf" + @"""";
argsStr = " -p 1-100 -t " + sourcePath + " -s flashversion=9 -o " + targetPath;
ExcutedCmd(cmdStr, argsStr);
break;
case TypeOfDocu.jpg:
if (!Directory.Exists(PfilePath + "\\img\\")) { Directory.CreateDirectory((PfilePath + "\\img\\")); }
ImageSize(, , PfilePath + "\\" + PfileName, PfilePath + "\\img\\" + PfileName + ".jpg", false);
break;
}
}
catch (Exception ex)
{
throw ex;
}
}
} private static void ExcutedCmd(string cmd, string args)
{
using (Process p = new Process())
{
ProcessStartInfo psi = new ProcessStartInfo(cmd, args);
psi.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo = psi;
p.Start();
p.WaitForExit();
}
} private static bool ExportPdf(string fileName, string outputFileName, string fileExt)
{
if (string.IsNullOrEmpty(fileName) || string.IsNullOrEmpty(outputFileName))
return false;
if (!File.Exists(fileName))
return false;
string formatExtension = Path.GetExtension(outputFileName);
if (string.IsNullOrEmpty(fileExt) || string.IsNullOrEmpty(formatExtension))
return false;
if (formatExtension != ".pdf")
return false;
switch (fileExt)
{
case "doc":
case "docx":
return WordExportAsPdf(fileName, outputFileName);
case "xls":
case "xlsx":
return ExcelExportAsPdf(fileName, outputFileName);
case "ppt":
case "pptx":
return PowerPointExportAsPdf(fileName, outputFileName);
default:
return false;
}
} /// <summary>
/// 转换为pdf文件,适合(.doc、.docx、.mht、.htm文件类型)
/// </summary>
/// <param name="fileName"></param>
/// <param name="outputFileName"></param>
/// <returns></returns>
private static bool WordExportAsPdf(string fileName, string outputFileName)
{
bool isSucceed = false;
Word.WdExportFormat fileFormat = Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF;
Word._Application wordApp = null;
if (wordApp == null) wordApp = new Word.Application();
Word._Document wordDoc = null; try
{
wordDoc = wordApp.Documents.Open(fileName);
wordDoc.ExportAsFixedFormat(outputFileName, fileFormat);
isSucceed = true;
} finally
{
if (wordDoc != null)
{
wordDoc.Close();
wordDoc = null;
}
if (wordApp != null)
{
wordApp.Quit();
wordApp = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
}
return isSucceed;
} /// <summary>
/// 转换为pdf文件,适合(.xls、.xlsx文件类型)
/// </summary>
/// <param name="fileName"></param>
/// <param name="outputFileName"></param>
/// <returns></returns>
private static bool ExcelExportAsPdf(string fileName, string outputFileName)
{
bool isSucceed = false;
Excel.XlFixedFormatType fileFormat = Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF;
Excel.Application excelApp = null;
if (excelApp == null) excelApp = new Excel.Application();
Excel.Workbook workBook = null; try
{
workBook = excelApp.Workbooks.Open(fileName);
workBook.ExportAsFixedFormat(fileFormat, outputFileName);
isSucceed = true;
} finally
{
if (workBook != null)
{
workBook.Close();
workBook = null;
}
if (excelApp != null)
{
excelApp.Quit();
excelApp = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
KillExcel();
}
return isSucceed;
}
/// <summary>
/// EXCEL进程无法正常退出
/// </summary>
private static void KillExcel()
{
Process[] pp = Process.GetProcessesByName("EXCEL");
foreach (Process p in pp)
{
if (p.SessionId == )
{ p.Kill(); }
}
} /// <summary>
/// 转换为pdf文件,适合(.ppt、pptx文件类型)
/// </summary>
/// <param name="fileName"></param>
/// <param name="outputFileName"></param>
/// <returns></returns>
private static bool PowerPointExportAsPdf(string fileName, string outputFileName)
{
bool isSucceed = false;
PowerPoint.PpFixedFormatType fileFormat = PowerPoint.PpFixedFormatType.ppFixedFormatTypePDF; PowerPoint.Application pptxApp = null;
if (pptxApp == null) pptxApp = new PowerPoint.Application();
PowerPoint.Presentation presentation = null; try
{
presentation = pptxApp.Presentations.Open(fileName, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
presentation.ExportAsFixedFormat(outputFileName, fileFormat);
isSucceed = true;
} finally
{
if (presentation != null)
{
presentation.Close();
presentation = null;
}
if (pptxApp != null)
{
pptxApp.Quit();
pptxApp = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
}
return isSucceed;
}

这个东西在64位操作系统中通过,就是每次转换的时间很长,Excel没排版好,也乱打印成PDF。

还有一种是模仿网易邮箱的附件预览的。参考:http://www.officeweb365.com/docview.aspx,把office文档原版呈现,能把这个地址利用起来倒是挺好