Web中打开Excel,并且进行编辑保存

1.首先新建网站

2.把设置好格式的excel保存为xml格式,点击另存。


3.xml文件添加到网站项目中

4.添加浏览页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>

5.aspx.cs文件代码如下

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 ExcelBrowse : System.Web.UI.Page
{
    private string _Targ_file = "市气象局.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;
    }
    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;
    }
}

运行效果

仅供参考,欢迎大家留言指导。


  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值