网页中嵌入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;
    }
}
分类:  ASP.NET
本文转自左正博客园博客,原文链接:http://www.cnblogs.com/soundcode/archive/2012/11/13/2768297.html ,如需转载请自行联系原作者
相关文章
|
移动开发 前端开发 Unix
复制excel数据到网页表格上
在前端开发过程中,有的时候我们会希望可以直接将excel上的数据复制粘贴到页面上的表格中,所以也就有了这一篇博客。
519 0
复制excel数据到网页表格上
|
6月前
|
JSON 前端开发 JavaScript
实用!Excel在线网页版表格Luckysheet源码
实用!Excel在线网页版表格Luckysheet源码
208 0
|
7月前
|
Web App开发 开发者
SAP UI5 SmartTable 控件本地运行时进行 Excel 导出的单步调试(二)
SAP UI5 SmartTable 控件本地运行时进行 Excel 导出的单步调试
30 0
|
7月前
|
API UED
SAP UI5 SmartTable 控件本地运行时进行 Excel 导出的单步调试(一)
SAP UI5 SmartTable 控件本地运行时进行 Excel 导出的单步调试
47 0
|
7月前
|
XML 数据格式
关于 SAP UI5 控件内容的 Excel 导出功能,如何加载所需的导出工具库
关于 SAP UI5 控件内容的 Excel 导出功能,如何加载所需的导出工具库
38 0
|
7月前
SAP UI5 Table 控件数据进行 Excel 导出时如何进行格式控制
本教程的前一步骤,我们成功的将 sap.m.Table 控件里显示的数据导出到了本地 Excel 文件中。
40 0
|
11月前
|
C#
C#导出到Excel——无法嵌入互操作类型“Microsoft.Office.Interop.Excel.ApplicationClass”。请改用适用的接口。
C#导出到Excel——无法嵌入互操作类型“Microsoft.Office.Interop.Excel.ApplicationClass”。请改用适用的接口。
154 0
|
数据挖掘
网页上的表格转Excel
大家好,我是南南 昨晚有个好朋友找我帮个忙,想了想就来开个新专栏,记录一下平常用的技巧
89 0
|
Web App开发 开发者
SAP UI5 SmartTable 控件本地运行时进行 Excel 导出的单步调试(2)
SAP UI5 SmartTable 控件本地运行时进行 Excel 导出的单步调试
|
数据可视化 IDE 测试技术
Jupyter Notebooks嵌入Excel并使用Python替代VBA宏
Jupyter Notebooks嵌入Excel并使用Python替代VBA宏
527 0
Jupyter Notebooks嵌入Excel并使用Python替代VBA宏