html 嵌入编辑excel 开源_网页中嵌入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>無標題頁 < 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(""); 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.value = sheet.XMLData; } function set_SheetXmlData() { var sheet = document.getElementById("_obj_Excel") var hf = document.getElementById(""); sheet.XMLData = hf.value; hf.value = ""; var xmldoc = load_XmlDocumentFromElement(""); 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;
}
}
總結
以上是生活随笔為你收集整理的html 嵌入编辑excel 开源_网页中嵌入Excel控件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 网页版excel服务器,用excel当数
- 下一篇: 消息中间件核心实体(1)