下面这段代码可以正常运行:点击button,把listview的数据,导入到E盘“aaa.xlsx”里。继续点击button,还能在原有数据后面逐行继续添加。
import win.ui;
import fsys;
import com;
import process;
/*DSG{{*/
mainForm = win.form(text="aardio form";right=410;bottom=619)
mainForm.add(
button={cls="button";text="导出到EXCEL";left=151;top=549;right=260;bottom=589;z=3};
button2={cls="button";text="关闭";left=277;top=548;right=386;bottom=588;z=4};
listview={cls="listview";left=20;top=66;right=388;bottom=518;bgcolor=16777215;edge=1;font=LOGFONT(h=-13;weight=700);fullRow=1;gridLines=1;hscroll=1;vscroll=1;z=1};
static={cls="static";text="欢迎!";left=25;top=19;right=379;bottom=58;font=LOGFONT(h=-24;weight=700);transparent=1;z=2}
)
/*}}*/
mainForm.listview.insertColumn("名称",70)
mainForm.listview.insertColumn("性别",50)
mainForm.listview.insertColumn("年龄",50)
mainForm.listview.insertColumn("手机",200)
mainForm.listview.addItem({"张三";"男";"28";"124 2346 1234"})
mainForm.listview.addItem({"李哲思";"男";"23";"123 2346 1267"})
mainForm.listview.addItem({"王金";"男";"29";"136 2111 1234"})
mainForm.listview.addItem({"柯伟平";"男";"3";"1344 2556 1034"})
mainForm.listview.addItem({"立马";"男";"25";"124 2326 1235"})
mainForm.listview.addItem({"吴金凤";"女";"22";"127 2006 1124"})
mainForm.listview.addItem({"张玲玲";"女";"27";"124 2355 5554"})
mainForm.button2.oncommand = function(id,event){//关闭窗体
mainForm.close()
}
mainForm.button.oncommand = function(id, event) {
mainForm.static.text = "正在导出数据,请耐心等待!";
fsys.createDir("E:\"); // 创建目录 E:\\
path = "E:\aaa.xlsx";
exce1 = com.CreateObject("Excel.Application");
var book = exce1.WorkBooks.Open(path);
exce1.visible = false;
exce1.DisplayAlerts = false;
sheet1 = book.sheets(1);
var lastRow = sheet1.UsedRange.Rows.Count + 1; // 获取下一行的行号
for (i = 1;mainForm.listview.count; 1) { // 将LISTVIEW中相应的数据赋值给EXCEL的对应格子
for (t = 1; mainForm.listview.columnCount; 1) {
sheet1.Cells(lastRow, t).value2 = mainForm.listview.getItemText(i, t);
}
lastRow++; // 每插入一行数据,增加行号
}
exce1.ActiveWorkbook.Save();
exce1.quit(); // 退出 Excel 进程
mainForm.static.text = "数据已经成功导出到EXCEL表格!";
}
mainForm.enableDpiScaling();
mainForm.show();
return win.loopMessage();
现在我想增加点功能,就是点击button后,先检查E盘是否有文件“aaa.xlsx”这个文件,如果没有,就新建一个;如果有,就执行代码,把listview的数据,添加到aaa.xlsx里。继续点击button,还能在原有数据后面逐行继续添加。现在前面的功能都能实现,就是最后一步,继续点击button,不能在数据后面逐行继续添加。请阿甘老师帮我看看错在哪地方,我的代码,只是修改了下面这一段(详见下)。谢谢了!
mainForm.button.oncommand = function(id, event) {
mainForm.static.text = "正在导出数据,请耐心等待!";
exce1 = com.CreateObject("Excel.Application");
fsys.createDir("E:\"); // 创建目录 E:\\
path = "E:\aaa.xlsx";
if( ! fsys.isDir(path) ){
var book = exce1.WorkBooks.Add();
exce1.visible = false;
exce1.DisplayAlerts = false;
sheet1 = book.sheets(1);
exce1.ActiveWorkbook.SaveAs(path);
} else {
book = exce1.WorkBooks.Open(path);
sheet1 = book.sheets(1);
}
var lastRow = sheet1.UsedRange.Rows.Count + 1; // 获取下一行的行号
for (i = 1; mainForm.listview.count; 1) {
for (t = 1; mainForm.listview.columnCount; 1) {
sheet1.Cells(lastRow, t).value2 = mainForm.listview.getItemText(i, t);
}
lastRow++; // 每插入一行数据,增加行号
}
exce1.ActiveWorkbook.Save();
exce1.quit(); // 退出 Excel 进程
mainForm.static.text = "数据已经成功导出到EXCEL表格!";
}