前提, 客户端必须装windows office Excel,我机器上装的时office 2003,如果你机器装的是office 2007只要把 object id="_obj_Excel" classid="clsid:0002E559-0000-0000-C000-000000000046"的classid改一下,是多少你自己到网 上去查一下。
< %@ Page Language="C#" AutoEventWireup="true" CodeFile="ExcelBrowse.aspx.cs" Inherits="ExcelBrowse" %> < !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 runat="server"> < title>无标题页</title> < script type="text/javascript"> function ScreenSpliter(header, footer, middle) { this._header = document.getElementById(header); this._footer = document.getElementById(footer); this._middle = document.getElementById(middle); document.body.style.margin = "0px"; document.body.style.overflow = "hidden"; this._middle.style.overflow = "auto"; this.resize(null); registerEventHandler(window, 'resize', getInstanceDelegate(this, "resize")); } function load_XmlDocumentFromElement(Id) { var hf = document.getElementById(Id); if(hf != null) { var xmldoc = new ActiveXObject("Microsoft.XMLDOM"); xmldoc.loadXML(hf.value); return xmldoc; } return null; } function get_NodeAttributeText(root, node_name, attr_name) { var node = root.selectSingleNode(node_name); if(node != null) return node.getAttribute(attr_name); else return null; } function set_ExcelDisplayMode(sheet) { sheet.AllowPropertyToolbox = false; sheet.DisplayToolbar = true; sheet.DisplayOfficeLogo = false; sheet.DisplayWorkbookTabs = false; sheet.DisplayTitleBar = false; } function set_ProtectModeForEdit(sheet) { var protection = sheet.ActiveSheet.Protection; protection.AllowInsertingRows = true; protection.AllowDeletingRows = true; protection.AllowFormattingColumns = true; protection.AllowSorting = true; protection.Enabled = true; sheet.activeWindow.enableResize = false; } function set_ProtectModeForBrowse(sheet) { var protection = sheet.ActiveSheet.Protection; protection.AllowFormattingRows = true; protection.AllowFormattingColumns = true; protection.AllowDeletingRows = false; protection.AllowInsertingRows = false; protection.AllowInsertingColumns = false; protection.AllowSorting = false; protection.Enabled = true; sheet.activeWindow.enableResize = false; } function get_SheetXmlData() { var sheet = document.getElementById("_obj_Excel"); sheet.ActiveSheet.Unprotect();//去除保护 var xmldoc = load_XmlDocumentFromElement("<%= _hf_ExcelSetting.ClientID%>"); if(xmldoc != null) { var temp = get_NodeAttributeText(xmldoc.lastChild, "ClearContents", "cols"); if(temp != null) { var range = sheet.ActiveSheet.Columns(temp); if(range != null) range.ClearContents(); } } var hf = document.getElementById("<%= _hf_ExcelXmlData.ClientID%>"); hf.value = sheet.XMLData; } function set_SheetXmlData() { var sheet = document.getElementById("_obj_Excel") var hf = document.getElementById("<%= _hf_ExcelXmlData.ClientID%>"); sheet.XMLData = hf.value; hf.value = ""; var xmldoc = load_XmlDocumentFromElement("<%= _hf_ExcelSetting.ClientID%>"); if(xmldoc != null) { var temp = get_NodeAttributeText(xmldoc.lastChild, "Viewable", "cols"); if(temp != null) sheet.ViewableRange = temp;//可见区 } sheet.ActiveSheet.Cells.Locked = true;//全部锁定
set_ProtectModeForBrowse(sheet); set_ExcelDisplayMode(sheet); } window.onload = function() { set_SheetXmlData(); } < /script> < /head> < body> < form id="form1" runat="server"> < div style="width:900px;height:600px"> < asp:HiddenField ID="_hf_ExcelXmlData" runat="server" /> < asp:HiddenField ID="_hf_ExcelSetting" runat="server" /> < object id="_obj_Excel" classid="clsid:0002E559-0000-0000-C000-000000000046" width="100%" height="500px" standby="Loading"> < /object> < /div> < /form> < /body> < /html>