Lua calls an external program excel through COM and calls a Windows API

Source: Internet
Author: User

From http://sunxiunan.com /? P = 1258

For convenience, it is best to install Lua for Windows, which already contains many useful third-party modules.

Require ('luacom')-luacom
Ie = luacom. Createobject ("internetexplorer. application ")
IE: navigate2 ("http://sunxiunan.com ")

Ie. Visible = true

Use Lua to call Excel and fill in some data in the cell.

Require ('luacom')-luacom
-Excel Activation
Excel = luacom. Createobject ("Excel. application ")
Excel. Visible = true-portable labels
-Zookeeper has been added.
Local book = excel. workbooks: add ()

Local sheet = book. worksheets (1)
-When the number of users exceeds 100
For ROW = 1,100 do
Sheet. cells (row, 1). value2 = math. Floor (math. Random () * 20)
End

Slightly more complexCode

Require "luacom"
Excel = luacom. Createobject ("Excel. application ")
Local book = excel. workbooks: add ()
Local sheet = book. worksheets (1)
Excel. Visible = true

-When the primary Primary Secondary
For ROW = 1, 30 do
For Col = 1, 30 do
Sheet. cells (row, col). value2 = math. Floor (math. Random () * 100)
End
End
-Please refer to the following link for more than 50 characters: Please refer to the Yellow
Local range = sheet: range ("A1 ″)
For ROW = 1, 30 do

For Col = 1, 30 do
Local v = sheet. cells (row, col). value2
If v> 50 then
Local cell = range: offset (Row-1, col-1)
Cell: Select ()
Excel. selection. Interior. Color = 65535
End
End

End

Excel. displayalerts = false-confirm that the specified parameter has been specified.

Excel: Quit ()
Excel = Nil

What should I do if I want to add a chart to excel?

Require "luacom"
Excel = luacom. Createobject ("Excel. application ")
Local book = excel. workbooks: add ()
Local sheet = book. worksheets (1)
Excel. Visible = true

For ROW = 1, 30 do
Sheet. cells (row, 1). value2 = math. Floor (math. Random () * 100)
End

Local chart = excel. Charts: add ()
Chart. charttype = 4-xlline
Local range = sheet: range ("A1: A30 ″)

Chart: setsourcedata (range)

To call a Windows API, use the following code:

Require "alien"

MessageBox = alien. user32.messageboxa
MessageBox: types {ret = 'long', abi = 'stdcall', 'long', 'string ',
'String', 'long '}

MessageBox (0, "title for test", "LUA call Windows API", 0)

How to Implement the callback function? The following example shows the callback.

Require 'alien'
-Two functions, enumwindows and getclassname, are declared.
Enumwindows = alien. user32.enumwindows
Enumwindows: types {"callback", "pointer", abi = "stdcall "}

Getclassname = alien. user32.getclassnamea
Getclassname: types {"long", "pointer", "Int", abi = "stdcall "}

Local Buf = alien. buffer (512)

-It will be repeatedly called by enumwindows to pass in Windows handle
Local function enum_func (hwnd, P)

Getclassname (hwnd, Buf, 511)
Print (hwnd .. ":" .. tostring (BUF ))
Return 1
End
Local callback_func = alien. Callback (
Enum_func,
{"Int", "pointer", abi = "stdcall "})

Enumwindows (callback_func, nil)

The function prototype is

 
Bool enumwindows (wndenumprocLpenumfunc, LparamLparam);
Int getclassname (hwndHwnd, LptstrLpclassname, IntNmaxcount);
 
 
 
The prototype of the first parameter of enumwindows is as follows: this function is input when the user calls it, And enumwindows uses it to return
 
Bool callback enumwindowsproc (hwndHwnd, LparamLparam);
 
For other complex usage methods, refer to the alien documentation.
 
 

These codes all come fromWww.hakaku.net/articles/20090615-459

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.