office文档上传后预览,是一个很常用的功能,在这里记录一下自己的研究成果!
第一种方式:
上传word文档,转换成html页面直接显示
/// <summary>
/// word转成html[把word文档换成同名的html文件,并返回其路径]
/// </summary>
/// <param name="wordFileName"></param>
private string WordToHtml(object wordFileName)
{
//在此处放置用户代码以初始化页面
ApplicationClass word = new ApplicationClass();
Type wordType = word.GetType();
Documents docs = word.Documents;
//打开文件
Type docsType = docs.GetType();
Document doc = (Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { wordFileName, true, true });
//转换格式,另存为
Type docType = doc.GetType();
string wordSaveFileName = wordFileName.ToString();
string extendName = Path.GetExtension (wordSaveFileName);
string strSaveFileName = null;
if (extendName == ".docx")
{
strSaveFileName = wordSaveFileName.Substring(0, wordSaveFileName.Length - 4) + "html";
}
else {
strSaveFileName = wordSaveFileName.Substring(0, wordSaveFileName.Length - 3) + "html";
}
object saveFileName = (object)strSaveFileName;
docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, WdSaveFormat.wdFormatFilteredHTML });
docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
//退出 Word
wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
return saveFileName.ToString();
}
/// <summary>
/// 调用转换
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button2_Click(object sender, EventArgs e)
{
string HtmlPath = WordToHtml(Server.MapPath("UpLoadWord/07用户操作手册.docx"));
string rePath = HtmlPath.Substring(HtmlPath.IndexOf("UpLoadWord\\") + 11, HtmlPath.Length - HtmlPath.IndexOf("UpLoadWord\\") - 11);
Response.Redirect("UpLoadWord/" + rePath);
}
显示效果:
这样的效果,使得文档没有格式,很不美观!但实现方法简单!
第二种方式:
使用flashpaper,效果比较好看,不过只支持32位操作系统,不支持64位;固实现时一直不成功,原理是使用flashpaper插件,将word转换成swf。在执行cmd命令时一直没有任何反应也不提示错误!
/// <summary>
/// 上传word并转换成swf
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnOK_Click(object sender, EventArgs e)
{
//向服务器上传图片
if (FileUpload1.HasFile)
{
//判断上传文件类型
string fileType = System.IO.Path.GetExtension(FileUpload1.FileName);
if (fileType == ".doc" || fileType == ".docx" || fileType == ".ppt" || fileType == ".pptx")
{
//try
//{
//指定上传文件在服务器上的保存路径
string savePath = Server.MapPath("~/UpLoadWord");
//检查服务器上是否存在这个物理路径,如果不存在则创建
if (!System.IO.Directory.Exists(savePath))
{
//需要注意的是,需要对这个物理路径有足够的权限,否则会报错
System.IO.Directory.CreateDirectory(savePath);
}
//重新命名文件名:当前时间 + fileType(文件类型)
string time = DateTime.Now.ToString("yyyyMMddHHmmssffff");
string fileName = time + fileType;
//文件上传实际物理路径
savePath = savePath + "\\" + fileName;
//上传服务器
FileUpload1.PostedFile.SaveAs(savePath);
//通过Web.config获取FlashPaper.exe路径
string flashpaper = System.Configuration.ConfigurationManager.AppSettings["FlashPaper"].ToString();
//转化后文件名
fileName = time + ".swf";
//转化后输出的完整路径(包括文件名)
string outpath = Server.MapPath("~/flash") + "\\" + fileName;
//flashpaper命令行:格式(FlashPaper.exe物理地址 原文件完整物理地址 -o 转化后输出的物理地址)
string param = flashpaper + " " + savePath + " -o " + outpath;
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
try
{
//执行进程
p.Start();
string strOutput = null;
p.StandardInput.WriteLine(param);
p.StandardInput.WriteLine("exit");
strOutput = p.StandardOutput.ReadToEnd();
Console.WriteLine(strOutput);
p.WaitForExit();
p.Close();
}
catch (Exception ex)
{
throw ex;
}
}
}
}
在web.config中:
<!--
FlashPrinter.exe文件在服务器硬盘上的物理地址,即把FlashPrinter安装在D盘
-->
<add key="FlashPaper" value="D:\"Program Files"\FlashPaper2.2\FlashPrinter.exe"/>
第三种方式:
使用print2flash,效果跟flashpaper差不多;支持64位系统。
具体使用如下:
1、下载print2flash,这里是64位,原理,通过把从客户端上传的word/Excel/ppt文件,调用print2flash官方提供的dll,转化成swf格式文件,最后浏览swf文件。
2,配置步骤:打开print2flash安装目录,64位操作系统默认在C:\ProgramFiles (x86)\Print2Flash3下,右键管理员方式运行print2flash.exe,点击option—>Print2FlashService Configuration,如下图:
3,在程序里引用Interop.Print2Flash3.dll,并调用如下代码,进行转换。
4,对于转换好的swf文件,直接调用预览即可!
<embed id="flashContainer" style="width:100%;height:100%;" src="<%=SwfPath %>" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
/// <summary>
/// 支持64位的word转换成swf
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnConvert_Click(object sender, EventArgs e)
{
if (!FileUpload2.HasFile)
{
Label1.Text = "Please select a file to convert";
return;
}
Label1.Text = "";
// upload
String fs_filename = Server.MapPath("UpLoadWord/") + FileUpload2.FileName;
String fs_convertedfilename = Server.MapPath("flash/") + FileUpload2.FileName + ".swf";
try
{
FileUpload2.PostedFile.SaveAs(fs_filename);
}
catch (Exception ex)
{
Label1.Text += "File could not be uploaded. " + ex.Message;
return;
}
// convert
try
{
int interfaceOptions = 0;
for (int i = 0; i < cblDocOptions.Items.Count; i++)
if (cblDocOptions.Items[i].Selected)
interfaceOptions |= Int32.Parse(cblDocOptions.Items[i].Value);
Print2Flash3.Server2 p2fServer = new Print2Flash3.Server2();
p2fServer.DefaultProfile.InterfaceOptions = interfaceOptions;
p2fServer.DefaultProfile.ProtectionOptions = (int)Print2Flash3.PROTECTION_OPTION.PROTENAPI;
p2fServer.ConvertFile(fs_filename, fs_convertedfilename, null, null, null);
Label1.Text += "File converted.";
swfUrl = "flash/" + FileUpload2.FileName + ".swf";
}
catch (Exception ex)
{
Label1.Text += "File could not be converted. " + ex.Message;
return;
}
}
显示效果:
第四种方式:
将上传的word文档转换为pdf,经pdf转换成swf格式(未实现)这种方式需要使用两种插件,pdf2swf,flexpaper
a.使用flashpaper将需要的文档通过简单的设置转换为SWF格式的Flash,扩展阅读:http://baike.baidu.com/view/917746.htm,flashpaper又不支持win7,放弃!
b.在网上发现可以使用swftools(http://www.swftools.org/谢天谢地啊,它支持win7)将pdf格式的文件转换为flash,但是不能讲office文档转换为flash,固先将doc转换成pdf后再将pdf转换成swf
public void PDFConvertToSWF(string sourcePath, string targetPath)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe ";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
string cmd = "pdf2swf.exe" + " " + sourcePath + " -o " + targetPath;
p.StandardInput.WriteLine(cmd);
p.Close();
}
这种方式的转换,前两步都很成功,但从pdf-swf格式转换时调用pdf2swf.exe时,一直提示不是内部或外部命令,也不是可运行的程序。没能解决!期待高手!
学习博客:
http://www.cnblogs.com/expectszc/archive/2012/04/04/2432149.html