import win.ui;
import win.ui.tooltip;
/*DSG{{*/
var winform = win.form(text="Excel輸出 TXT文件";right=502;bottom=358;acceptfiles=1;bgcolor=15780518;border="dialog frame";max=false;mode="popup")
winform.add(
button2={cls="button";text="生成文件";left=383;top=275;right=458;bottom=302;z=5};
fgf={cls="combobox";left=79;top=125;right=226;bottom=147;edge=1;items={"|"};mode="dropdown";z=11};
lb={cls="listbox";left=304;top=117;right=467;bottom=266;border=1;edge=1;items={};msel=1;vscroll=1;z=6};
msg={cls="edit";text="信息提示";left=11;top=309;right=497;bottom=351;bgcolor=8421376;edge=1;font=LOGFONT(h=-16);multiline=1;readonly=1;z=2};
sel_sfile={cls="button";text="...";left=445;top=87;right=467;bottom=109;z=9};
selfd={cls="static";text="點(diǎn)擊右邊選擇要輸出的字段";left=28;top=169;right=290;bottom=260;border=1;transparent=1;z=12};
sfile={cls="edit";text="Edit";left=79;top=86;right=438;bottom=110;acceptfiles=1;edge=1;z=8};
static={cls="static";text="Excel輸出 TXT文件";left=11;top=16;right=493;bottom=67;align="center";bgcolor=32896;color=8388608;font=LOGFONT(h=-32;weight=700);transparent=1;z=1};
static1={cls="static";text="源文件:";left=24;top=88;right=82;bottom=109;transparent=1;z=7};
static2={cls="static";text="分隔符:";left=24;top=127;right=82;bottom=150;transparent=1;z=10};
static3={cls="static";text="輸出路徑:";left=23;top=275;right=114;bottom=292;transparent=1;z=4};
tfile={cls="static";text="#";left=85;top=273;right=352;bottom=294;border=1;notify=1;transparent=1;z=3}
)
/*}}*/
import fsys.dlg;//對(duì)話框庫
var selfdtxt="點(diǎn)擊右邊選擇要輸出的字段"+'\n'+"按住CTRL可以多選,按住SHIFT可以連選" +'\n'+"———————下面是已選字段———————"+'\n';
var sv={};//點(diǎn)選字段列表
initme=function(){//初始化
var tip=win.ui.tooltip(winform);//提示信息
winform.sfile.text="";
winform.msg.text="今天是:"+string.format("%4d年%2d月%2d號(hào)", time.now().year,time.now().month,time.now().day);
tip.addTool(winform.tfile,"點(diǎn)擊設(shè)置輸出文件名",0x10);//幫助提示
tip.addTool(winform.sfile,"可以把要轉(zhuǎn)換的文件拖放到程序窗體上",0x10);//幫助提示
winform.selfd.text=selfdtxt;
}
outpath=function(){//選擇輸出文件
var path = fsys.dlg.save("文本文件(TXT)|*.txt|所有文件|*.*",,,winform);
if(path){
return(path);
}
return ("#");
}
selfile=function(){//選擇源文件
var path = fsys.dlg.open('Excel文件|*.xls;*.xlsx;*.csv|所有文件|*.*||',,,winform);
if(path){
return(path);
}
return null;
}
deltoo=function(t){//刪除重復(fù)項(xiàng)
if(type(t)=="table"){
var rtn={};
for(i=1;#t;1){
if(!table.find(rtn,t[i])){table.push(rtn,t[i]);}
}
return(rtn);
}
}
build2txt=function(sf,tf){//生成txt文件? sf 源文件 tf目標(biāo)文件
if(io.exist(sf)){
import com.excel;
var rn=0
var excel = com.excel();
excel.Visible = false; //使Excel窗口可見
excel.alerts = false;? //關(guān)閉所有操作提示
var book = excel.Open(sf) //打開基礎(chǔ)數(shù)據(jù)文件 默認(rèn)哦 #/db/20201020.xlsx;
? ? ? ? var tf1=io.open(tf,"w+");//以寫和追加模式打開文件
? ? ? ? var sheet1 = excel.ActiveWorkbook.Sheets(1);//打開第一個(gè)工作表
? ? ? ? var ot="";
? ? ? ? if(#sv>0){
? ? ? ? for(i=2;sheet1.UsedRange.Rows.Count-1;1){//如果要表頭 i=1
? ? ? ? tf1.write(sheet1.Cells(i,sv[#sv]).value);//因?yàn)閑achSelected()是反著的,所以這里要倒著寫出
? ? ? ? rn++;
? ? ? ? for(j=#sv-1;1;-1){
? ? ? ? tf1.write(winform.fgf.text++sheet1.Cells(i,sv[j]).value);
? ? ? ? }
? ? ? ? tf1.write('\n');
? ? ? ? ? }
? ? ? ? }
tf1.close();//保存文件 默認(rèn)是? utf-8格式,如需修改請(qǐng)自行用記事本打開 另存為 別的格式
book.close();//關(guān)閉工作表
excel.Quit();//關(guān)閉 Excel或Wps..
return (rn); //返回生成記錄數(shù)
}
return(0);
}
winform.tfile.oncommand = function(id,event){//點(diǎn)擊 "..."
winform.tfile.text=outpath();
}
winform.sel_sfile.oncommand = function(id,event){//點(diǎn)擊 # 行
winform.sfile.text=selfile();
}
winform.sfile.onChange = function(){ //源文件
if(owner.onModified)owner.onModified(true);
winform.lb.clear();//清空字段列表
if(io.exist(winform.sfile.text)){
import com.excel;
var excel = com.excel();
excel.Visible = false; //使Excel窗口可見
excel.alerts = false; //關(guān)閉所有操作提示
var cv="";
var book = excel.Open(winform.sfile.text) //打開基礎(chǔ)數(shù)據(jù)文件 默認(rèn)哦 #/db/20201020.xlsx;
var sheet1 = excel.ActiveWorkbook.Sheets(1);//打開第一個(gè)表單
? ? ? ? for(i=1;sheet1.UsedRange.Cells.Count;1){//如果要表頭 i=1
? ? ? ? cv=sheet1.Cells(1,i).value;
? ? ? ? if(cv!=null){//標(biāo)題行里不能有空單元格,否則后的字段將不顯示
? ? ? ? winform.lb.add(cv); //顯示點(diǎn)選字段
? ? ? ? }
? ? ? ? else{i=sheet1.UsedRange.Cells.Count;}//結(jié)束循環(huán)
}
book.close();
excel.Quit();
}
}
winform.lb.oncommand = function(id,event){//點(diǎn)擊 字段名
var xx="";
sv={};
for(k,v in winform.lb.eachSelected()){
if(xx==""){xx=v;}
else{xx=v++winform.fgf.text++xx;}
table.push(sv,k);
}
sv=deltoo(sv);//刪除重復(fù)項(xiàng)
winform.selfd.text=selfdtxt++xx;//顯示已選 字段 不能自定義生成順序,如果想自定義,請(qǐng)自己修改 build2txt函數(shù)
}
winform.button2.oncommand = function(id,event){//生成目標(biāo)文件
if(winform.tfile.text=="#"){winform.tfile.text=outpath();}//如果未設(shè)置生成文件則提示,否則生成 "#"文件
var x=build2txt(winform.sfile.text,winform.tfile.text);
if(x>0){
import process;
winform.msg.text="已經(jīng)完成,共 "++x++" 條記錄,文件為:"++winform.tfile.text;
process.exploreSelect(winform.tfile.text);//瀏覽生成的文件并選定
}
}
winform.sfile.wndproc = function(hwnd,message,wParam,lParam){
select( message ) {
case 0x233/*_WM_DROPFILES*/{ //拖入文件
? ? ? ? var path = win.getDropFile(wParam );
? ? ? ? ? winform.sfile.text=path[1];
? ? ? ? }
case 0x205/*_WM_RBUTTONUP*/{
var xf=selfile();
winform.sfile.text=xf;
}
else{
}
}
}//拖入目標(biāo)文件
winform.wndproc = function(hwnd,message,wParam,lParam){
select( message ) {
case 0x233/*_WM_DROPFILES*/{ //拖入文件
? ? ? ? var path = win.getDropFile(wParam );
? ? ? ? ? winform.sfile.text=path[1];
? ? ? ? }
case 0x205/*_WM_RBUTTONUP*/{
var xf=selfile();
winform.sfile.text=xf;
}
else{
}
}
}//拖入目標(biāo)文件
initme();
winform.show();
win.loopMessage();