近日项目中做到一个功用,须要上传附件后能够在线预览。之前也没做过这类似的,于是乎就查找了相干材料,.net完成Office文件预览也许有这几种体式格局:
运用Microsoft的Office组件将文件直接转换为html文件(长处:代码完成最简朴,事情强度最小。瑕玷:结果极差)
运用Microsoft的Office组件将文件转换为PDF花样文件,然后再运用pdf2swf转换为swf文件,也就是flash文件在运用FlexPaper展现出来(长处:预览结果能接收,瑕玷:代码量大)
运用Office online(长处:表现圆满,瑕玷:不适合中小企业运用)
由于开辟时间短而且另有其他功用点须要完成,所以临时先已第一种体式格局完成了,这里也主要讲第一种体式格局,结果以下图:
详细完成
这里简朴提一下结果图中的遮罩结果和上传完成,有喜好的朋侪也能够参考参考。
遮罩结果就是HTML+CSS+JS来完成的,悉数代码以下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>弹出层</title> <script src="jquery-1.6.2.min.js" type="text/javascript"></script> <style> .black_overlay{ display: none; position: absolute; top: 0%; left: 0%; width: 100%; height: 100%; background-color: black; z-index:1001; -moz-opacity: 0.8; opacity:.80; filter: alpha(opacity=80); } .white_content { display: none; position: absolute; top: 10%; left: 10%; width: 80%; height: 80%; border: 16px solid lightblue; background-color: white; z-index:1002; overflow: auto; } .white_content_small { display: none; position: absolute; top: 20%; left: 30%; width: 40%; height: 50%; border: 16px solid lightblue; background-color: white; z-index:1002; overflow: auto; } </style> <script type="text/javascript"> //弹出隐蔽层 function ShowDiv(show_div,bg_div){ document.getElementById(show_div).style.display='block'; document.getElementById(bg_div).style.display='block' ; var bgdiv = document.getElementById(bg_div); bgdiv.style.width = document.body.scrollWidth; // bgdiv.style.height = $(document).height(); $("#"+bg_div).height($(document).height()); }; //封闭弹出层 function CloseDiv(show_div,bg_div) { document.getElementById(show_div).style.display='none'; document.getElementById(bg_div).style.display='none'; }; </script> </head> <body> <input id="Button1" type="button" value="点击弹出层" onclick="ShowDiv('MyDiv','fade')" /> <!--弹出层时背景层DIV--> <div id="fade" class="black_overlay"> </div> <div id="MyDiv" class="white_content"> <div style="text-align: right; cursor: default; height: 40px;"> <span style="font-size: 16px;" onclick="CloseDiv('MyDiv','fade')">封闭</span> </div> 现在来讲,我照样喜好这个本身革新的弹出层。本身在项目中也用的是这个。 </div> </body> </html>
上传的话,由于文件比较小,所以采纳的是保留在服务器,在数据库中寄存途径的体式格局
前台代码
<div class="white_content" id="MyDiv" style="text-align: center; display: none;"> <div style="text-align: right; cursor: default; height: 40px;"> <span style="font-size: 16px;" onclick="CloseDiv('MyDiv','fade')">封闭</span> </div> <tr style="width: 50%" id="upload_Image"> <h1> 请上传常见问题附件</h1> <td align="right" class="Title"> 上传附件 </td> <td> <asp:FileUpload ID="FileUpload1" runat="server" /> <asp:Label ID="label1" runat="server" ForeColor="Red"></asp:Label> <asp:Button ID="UploadButton" runat="server" Text="上传附件" OnClick="UploadButton_Click" /> </td> </tr> <tr> <td colspan="2" align="center" id="show_image" style="visibility: hidden"> <asp:Image ID="Image1" runat="server" Height="118px" Width="131px" /> </td> </tr> </div>
背景要领
try { string FullName = FileUpload1.PostedFile.FileName;//猎取附件物理地址 FileInfo fi = new FileInfo(FullName); string name = fi.Name;//猎取附件称号 string type = fi.Extension;//猎取附件范例 if (type == ".xls" || type == ".xlsx" || type == ".doc" || type == ".docx" || type == ".pdf") { string SavePath = Server.MapPath("~\\uploadFile");//附件保留到文件夹下 if (!Directory.Exists(SavePath)) { Directory.CreateDirectory(SavePath); } this.FileUpload1.PostedFile.SaveAs(SavePath + "\\" + name);//保留途径 #region 将附件内容保留到数据库中 int showsuccess = CMSModelManager.Submitted_questionsDAO.Save_File(name,type,SavePath); if (showsuccess == 1) { this.label1.Text = "上传胜利"; } else { this.label1.Text = "服务器忙碌,请稍后重试"; } #endregion } else { this.label1.Text = "请挑选准确的花样附件"; } } catch (Exception ex) { Response.Write(ex.Message); }
图中所示的将Word转换成HTML的完成体式格局:
起首新建一个协助类
using System; using System.Collections.Generic; using System.Web; //using Microsoft.Office.Core; using Word = Microsoft.Office.Interop.Word; namespace Com.VanruPortal.Admin { public class Office2HtmlHelper { /// <summary> /// Word转成Html /// </summary> /// <param name="path">要转换的文档的途径</param> /// <param name="savePath">转换成html的保留途径</param> /// <param name="wordFileName">转换成html的文件名字</param> public static void Word2Html(string path, string savePath, string wordFileName) { Word.ApplicationClass word = new Word.ApplicationClass(); Type wordType = word.GetType(); Word.Documents docs = word.Documents; Type docsType = docs.GetType(); Word.Document doc = (Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { (object)path, true, true }); Type docType = doc.GetType(); string strSaveFileName = savePath + wordFileName + ".html"; object saveFileName = (object)strSaveFileName; docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML }); docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null); wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null); } /// <summary> /// Excel转成Html /// </summary> /// <param name="path">要转换的文档的途径</param> /// <param name="savePath">转换成html的保留途径</param> /// <param name="wordFileName">转换成html的文件名字</param> public static void Excel2Html(string path, string savePath, string wordFileName) { string str = string.Empty; Microsoft.Office.Interop.Excel.Application repExcel = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel.Workbook workbook = null; Microsoft.Office.Interop.Excel.Worksheet worksheet = null; workbook = repExcel.Application.Workbooks.Open(path, 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]; object htmlFile = savePath + wordFileName + ".html"; object ofmt = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml; workbook.SaveAs(htmlFile, ofmt, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); object osave = false; workbook.Close(osave, Type.Missing, Type.Missing); repExcel.Quit(); } } }
背景挪用要领
上传胜利后将文件转换 string physicalPath = Server.MapPath(Server.UrlDecode("/uploadFile"+"\\"+ name));//读取相对途径 string extension = Path.GetExtension(physicalPath);//猎取后缀名 string[] show_name = name.Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries); //此处的name是上面上传附件中的称号支解 string show_name_View = show_name[0];//拿到现实name switch (extension) { case ".doc": case ".docx": Office2HtmlHelper.Word2Html(MapPath("/uploadFile" + "\\" + name + ""), MapPath("/Html/"), "" + show_name_View + ""); //挪用协助类中生成WordHtml的要领,并保留起来 Response.Write("<script>window.open('/Html/" + show_name_View + ".html','_blank')</script>"); //跳转并翻开保留的相对途径中hmtl文件 break; case ".xls": case ".xlsx": Office2HtmlHelper.Excel2Html(MapPath("/uploadFile" + "\\" + name + ""), MapPath("/Html/"), "" + show_name_View + ""); Response.Write("<script>window.open('/Html/" + show_name_View + ".html','_blank')</script>"); break; default: break; }
至此,一个浅易的上传附件在线阅读已悉数完成
以上就是.NET编程中Word/Excel 在线预览的内容,更多相干内容请关注ki4网(www.ki4.cn)!