ActiveX的前身是OLE,后身是COM,是微软建立的一套组件技术,只不过在不同的发展阶段使用了不同的名称。COM全称是组件对象模型。COM组件的优势在于,只要你遵循COM的规范,所有组件之间就可以很方便的相互调用,而不用关心组件开发所用的语言,不用关心组件最初是为哪个应用写的。因此,在Windows平台下,很多厂商及开发者提供了各种各样的COM组件,可被用户使用。比如微软自己的Office提供的Word、Excel,除了作为独立运行的应用外,同时也提供COM组件,因此,你可以在你的程序里嵌入一个Word文档或者Excel表格,当其被激活时,可以自动切换到Word或Excel的界面进行编辑。为了使JavaScript也能获得这些组件带来的便利,微软的JavaScript引擎提供了一个ActiveXObject,用来包装COM组件以被JavaScript代码调用。
node-activex简介
node-activex项目就是这样一个能在node.js中调用COM/ActiveXObject对象的组件库。可直接通过COM连接数据库,操作Word/Excel/PPT等Excel组件。
项目地址:
https://github.com/durs/node-activex
打开OLEDB数据库
var winax = require('winax');
var con = new winax.Object('ADODB.Connection');
con.Open('Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\tmp;Extended Properties="DBASE IV;"', '', '');
con.Execute("Create Table persons.dbf (Name char(50), City char(50), Phone char(20), Zip decimal(5))");
con.Execute("Insert into persons.dbf Values('John', 'London','123-45-67','14589')");
con.Execute("Insert into persons.dbf Values('Andrew', 'Paris','333-44-55','38215')");
con.Execute("Insert into persons.dbf Values('Romeo', 'Rom','222-33-44','54323')");
新建一个Excel并写入内容
var excel = new winax.Object("Excel.Application", { activate: true });
var wbk = excel.Workbooks.Add(template_filename);
var wsh = wbk.Worksheets.Item(1);
wsh.Range("C3:E4").Value = [ ["C3", "D3", "E3" ], ["C4", "D4", "E4" ] ];
wsh.Range("C3:E4").Value = [ ["C3", "D3", "E3" ], "C4" ]; // will let D4 and E4 empty
wsh.Range("C3:E4").Value = [ [null, "D3", "E3" ], "C4" ]; // will let C3, D4 and E4 empty
wsh.Range("C3:F4").Value = [ [100], [200] ]; // will duplicate the two rows in colums C, D, E, and F
wsh.Range("C3:F4").Value = [ [100, 200, 300, 400] ]; // will duplicate the for cols in rows 3 and 4
wsh.Range("C3:F4").Value = [ [100, 200] ]; // Will correctly duplicate the first two cols, but col E and F with contains "#N/A"
const data = wsh.Range("C3:E4").Value.valueOf();
console.log("Cell E4 value is", data[1][2]);
打开Excel执行VBA宏,并另存为网页
const workbook = excel.Workbooks.Open('C:\\Uxxxxxxxxxxx\\example.xlsm')
const worksheet = workbook.Worksheets.Item("Home Page")
worksheet.Cells.Item(7,6).value = "Mar";
const application = excel.Application;
pplication.Run('Calculate_NAME');
workbook.SaveAs('C:\\Uxxxxxxxxxxx\\output.html', 44, undefined, undefined, false, false, 1, 2);
回复 (0)
微信扫码 立即评论