aardio 读取 Excel文件,显示在 listview 中

编写 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

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值