PDA

View Full Version : js open excel file



huyyy
17 May 2011, 09:13 PM
hi guys, im new to this community and i have a question

i currently got a script which loads a bunch of addresses on an excel file, but it does not work as soon as i load it onto a webserver, i tried it on a linux webhost and a local server. it does nothing when i load it when its on a server

any ideas?

it is wierd beacuse, it works 100% when i just run it outside of a server



function getAddrListFromXLS(xlsFile){
// open
/*一:主要是Scripting.FileSystemObject (FSO 文本文件读写)被关闭了,
开启FSO功能即可,在“运行”中执行regsvr32 scrrun.dll
二:如果javascript脚本中报这个错误是因为IE的安全设置不允许运行未标记为安全的activeX控件
更改IE的安全设置,把相应的选项打开即可。*/
var html = "" ;
_xlsAddrFileName = xlsFile ;
try{

var xls = new ActiveXObject("Excel.Application");
xls.Visible = false;
var x1Book = xls.Workbooks.Open(xlsFile);
// read
var idxColAddr = 6, idxColSub = 7, idxLst= 4, idxFst=5;
var allCells = xls.ActiveSheet.Cells;
var rowNum = xls.ActiveSheet.UsedRange.Rows.Count ;
var fstRow= 4 ;
var i=fstRow, clAddr, clSub;
var h = '<li onclick="onSelectAddr(this);return false;"><a href="void(0)">' ;
var t = '</a></li>\n';
_arr_addrs = [];
_arr_users = [];

for(; i <= rowNum ; i++){
var user = allCells(i, idxFst).Value ;
user += " "+ allCells(i, idxLst).Value ;
_arr_users[i] = user ;

var val = allCells(i, idxColAddr).Value ;
val += " "+ allCells(i, idxColSub).Value ;
_arr_addrs[i] = val ;


html += h;
html += val ;
html += t ;
}
// close
//x1Book.SaveAs(fname);
x1Book.Close;
//xls.visible = false;
xls.Quit();

}catch(e){
html = _txt_noAddrList ;
_xlsAddrFileName = null ;
}

return html ;
}

function selectAddrFile(ctrl) {
var file = ctrl.value ;
if (file == "" || file == undefined) {
return ;
}
// 加载xls文件,初始地址列表
var txt = getAddrListFromXLS(file) ;
if (txt == null) {
alert("fail to open xls file ! - " + file);
return ;
}

setAddrList(txt) ;
}

function setAddrList(txt) {
var ctrl = document.getElementById("lstAddr") ;
if (ctrl == undefined) {
return ;
}
if (txt == undefined) {
txt= _txt_noAddrList ;
}
ctrl.innerHTML = txt;
}


also is there any other way to get this to work on other browsers other than IE?