网页中嵌入Excel控件

前提, 客户端必须装windows office Excel,我机器上装的时office 2003,如果你机器装的是office 2007只要把 object id="_obj_Excel" classid="clsid:0002E559-0000-0000-C000-000000000046"的classid改一下,是多少你自己到网 上去查一下。

 
aspx页面代码

< %@ 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>

后台代码
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;
using System.IO;
using System.Text;
 
public partial class ExcelEdit : System.Web.UI.Page
{
    private string _Targ_file = "test.xml";
 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == true)
            return;
 
        //取得Excel内容------------------------------------------
        string file_name = Server.MapPath("Excel/" + _Targ_file);
        XmlDocument xml = new XmlDocument();
        xml.Load(file_name);
        if (xml != null)
            _hf_ExcelXmlData.Value = xml.OuterXml;
 
        //取得Excel设置------------------------------------------
        XmlNode node = get_ExcelSetting(_Targ_file);
        if (node != null)
            _hf_ExcelSetting.Value = node.OuterXml;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
 
        //OWC.Spreadsheet sheet = new OWC.Spreadsheet();
        //sheet.XMLData = _hf_ExcelXmlData.Value;
 
            //---------------------------------------------------
            string file = Server.MapPath("Excel/" + _Targ_file);
            //删除文件
            //if (File.Exists(file))
            //    File.Delete(file);
 
            ////---------------------------------------------------
            //sheet.Export(file, OWC.SheetExportActionEnum.ssExportActionNone, OWC.SheetExportFormat.ssExportXMLSpreadsheet);
 
            File.WriteAllText(file, _hf_ExcelXmlData.Value, Encoding.Unicode);
    }
 
    static public XmlNode get_ExcelSetting(string key)
    {
        string file = HttpContext.Current.Server.MapPath("App_Data/Excels.xml");
        XmlDocument doc = new XmlDocument();
        doc.Load(file);
        //-------------------------------------------------------
        if (doc != null)
        {
            XmlNode root = doc.DocumentElement;
            if (root != null)
            {
                string query = string.Format("sheet[@filename='{0}']", key);
                XmlNode node = root.SelectSingleNode(query);
                if (node != null)
                    return node;
            }
        }
        //-------------------------------------------------------
        return null;
    }
}

你可能感兴趣的:(Excel)