编写 main.aardio 如下
- import win.ui;
- /*DSG{{*/
- winform = win.form(text="excel1";right=801;bottom=500)
- winform.add(
- button1={cls="button";text="读取Excel文件";left=19;top=14;right=126;bottom=44;z=1};
- button2={cls="button";text="导出Excel文件";left=168;top=17;right=291;bottom=45;z=3};
- listview={cls="listview";left=2;top=62;right=801;bottom=498;edge=1;gridLines=1;hscroll=1;vscroll=1;z=2}
- )
- /*}}*/
-
- import io;
- import sys;
- import fsys.dlg;
- import win.ui.grid;
- win.ui.grid(winform.listview);
-
- import console;
- import com.excel;
-
- console.showLoading(" 正在启动 Excel ");
- var excel,err = com.excel();
- assert(excel,err);
-
- excel.alerts = false; //关闭界面提示与屏幕更新
- excel.Visible = false;
-
- // 读取Excel文件
- winform.button1.oncommand = function(id,event){
- winform.button1.disabled = true;
- var xls = fsys.dlg.open("*.xls|*.xlsx");
- if (!io.exist(xls)){
- winform.msgbox(".xls not found.");
- winform.button1.disabled = false;
- return ;
- }
- winform.listview.clear();
- try{
- // 读取.xls 或 .xlsx
- var book = excel.Open(xls);
- var sheet = book.Sheets(1);
-
- var rows = sheet.UsedRange.Rows.count;
- console.log("rows=",rows)
- var cols = sheet.UsedRange.Columns.Count;
- console.log("cols=",cols)
-
- var fields = {};
- for (j=1; cols; 1){
- v = sheet.Cells(1, j).value;
- if (type(v) == type.string) table.push(fields, v);
- elseif (type(v) == type.number){
- if (v == math.modf(v)) table.push(fields, string.format("%d",v));
- else table.push(fields, string.format("%.4f",v));
- } // date 字段格式化
- elseif (type(v) == type.table) table.push(fields, string.left(tostring(v),10));
- elseif (type(v) == type.null) table.push(fields, '');
- else table.push(fields, type(v));
- }
- //console.dump(fields);
- winform.listview.setColumns(fields);
- for (i=2; rows; 1){
- if (i >100) break;
- values = {};
- for (j=1; cols; 1){
- v = sheet.Cells(i, j).value;
- if (type(v) == type.string) table.push(values, v);
- elseif (type(v) == type.number){
- if (v == math.modf(v)) table.push(values, string.format("%d",v));
- else table.push(values, string.format("%.4f",v));
- } // date 字段格式化
- elseif (type(v) == type.table) table.push(values, string.left(tostring(v),10));
- elseif (type(v) == type.null) table.push(values, '');
- else table.push(values, type(v));
- }
- //console.dump(values);
- winform.listview.addItem(values);
- }
-
- book.close();
- }
- catch(e){
- winform.msgbox(e)
- }
- winform.button1.disabled = false;
- }
-
- // 鼠标左键点击事件
- winform.listview.onClick = function(item,subItem,nmListView){
- //var index = winform.listview.selIndex; //选择的行
- }
- // 鼠标双击修改事件
- winform.listview.onEditChanged = function(text,iItem,iSubItem){
- winform.msgbox(string.join({iItem,iSubItem,text},",")); //修改成功
- }
- // 鼠标右键事件
- winform.listview.onRightClick = function(item,subItem,nmListView){
- var v = winform.listview.getItemText(item,subItem); //取值
- winform.msgbox(string.join({item,subItem,v},",")); //显示行、列、值信息
- }
-
- // 导出Excel文件
- winform.button2.oncommand = function(id,event){
-
- }
-
- winform.show();
- return win.loopMessage();
- excel.Quit();
参考:aardio开发语言Excel数据表读取修改保存实例练习
import godking.libxl; 在 蓝奏云 下载 libxl.rar 密码:123
解压缩将文件放于:aardio\lib\godking\ ,先要 cd lib; mkdir godking