首頁 >web前端 >js教程 >可以读取EXCEL文件的js代码_javascript技巧

可以读取EXCEL文件的js代码_javascript技巧

PHP中文网
PHP中文网原創
2016-05-16 18:47:391345瀏覽

js读取 EXCEL 文件 的实现代码,比较全了大家可以自行测试了。

首页给个有中文说明的例子,下面的例子很多大家可以多测试。

<script language="javascript" type="text/javascript"><!-- 
function readExcel() { 
var excelApp; 
var excelWorkBook; 
var excelSheet; 
try{ 
excelApp = new ActiveXObject("Excel.Application"); 
excelWorkBook = excelApp.Workbooks.open("C:\\XXX.xls"); 
excelSheet = oWB.ActiveSheet; //WorkSheets("sheet1") 
excelSheet.Cells(6,2).value;//cell的值 
excelSheet.usedrange.rows.count;//使用的行数 
excelWorkBook.Worksheets.count;//得到sheet的个数 
excelSheet=null; 
excelWorkBook.close(); 
excelApp.Application.Quit(); 
excelApp=null; 
}catch(e){ 
if(excelSheet !=null || excelSheet!=undefined){ 
excelSheet =nul; 
} 
if(excelWorkBook != null || excelWorkBook!=undefined){ 
excelWorkBook.close(); 
} 
if(excelApp != null || excelApp!=undefined){ 
excelApp.Application.Quit(); 
excelApp=null; 
} 
} 
// --></script>

如果是在网页上打开EXCEL 文件,那么在关闭的时候,进程里还有EXCEL.EXE,所以必须关闭后,刷新本页面!

<script> 
function ReadExcel() 
{ 
var tempStr = ""; 
var filePath= document.all.upfile.value; 
var oXL = new ActiveXObject("Excel.application"); 
var oWB = oXL.Workbooks.open(filePath); 
oWB.worksheets(1).select(); 
var oSheet = oWB.ActiveSheet; 
try{ 
for(var i=2;i<46;i++) 
{ 
if(oSheet.Cells(i,2).value =="null" || oSheet.Cells(i,3).value =="null" ) 
break; 
var a = oSheet.Cells(i,2).value.toString()=="undefined"?"":oSheet.Cells(i,2).value; 
tempStr+=(" "+oSheet.Cells(i,2).value+ 
" "+oSheet.Cells(i,3).value+ 
" "+oSheet.Cells(i,4).value+ 
" "+oSheet.Cells(i,5).value+ 
" "+oSheet.Cells(i,6).value+"\n"); 
} 
}catch(e) 
{ 
document.all.txtArea.value = tempStr; 
} 
document.all.txtArea.value = tempStr; 
oXL.Quit(); 
CollectGarbage(); 
} 
</script> 
<html> 
<input type="file" id="upfile" /><input type="button" onclick="ReadExcel();" value="read"> 
<br> 
<textarea id="txtArea" cols=50 rows=10></textarea> 
</html>

二、
js读取excel文件 

3f1c4e4b6b16bbbd69b2ee476dc4f83a
function readThis(){
var tempStr = "";
var filePath= document.all.upfile.value;
var oXL = new ActiveXObject("Excel.application");
var oWB = oXL.Workbooks.open(filePath);
oWB.worksheets(1).select();
var oSheet = oWB.ActiveSheet;
try{
for(var i=2;i8a850a13a7d4f6304fd432c6bbfe3440
100db36a723c770d327fc0aef2ce13b1
385593bf7209582d73c2d3d650334bb8
a7e1e84ae9ae90c7575fa0f1aa364ee80c6dc11e160d3b678d68754cc175188a
17753354e4c2f2d0ae75f3fe89c62dc240587128eee8df8f03d0b607fe983014
73a6ac4ed44ffec12cee46588e518a5e


三、
我在vs2005平台上要实现这么一个功能,点击一个按钮一次将大量的excel文件数据导入到sqlserver2005中
我用的是ajax技术,在前台用javascript操做excel文件,循环读取所有的excel文件,每读取一行就放进一个数组里通过web服务传到后台用c#语言将一行数据插入到数据库。思路大概就是这样。
现在功能已经实现了,具体代码如下
用javascript定义一个函数,循环读取excel文件数据 

function readExcel()
{
try
{
var ExcelNum=new Array();
//重复导入之前,删除上次导入的同期数据
WebServiceExcel.deleteOldNumber();
var oXL = new ActiveXObject( "Excel.Application ");
\\r_c_num[5]的值为excel文件的名字
var path=document.all.excelpath.value+ "\\ "+r_c_num[5]
var oWB = oXL.Workbooks.open(path);
\\如果excel文件有多个sheet的话从第一个sheet循环读取
for(var x=1;x b251e1704a85ad66178e0d84f5ea89ce
6a74014ee44f5deb5894267f99b68016
ef1cac8df639bc9110cb13b8b5ab3dd7
08eb756f81b28a01d33212cce3fbe742 New Document 0e94189e2178ffcfb346fb78b3f60691
78ad79ac276283930dbc623975e8fefd
e388781bef789b60660cfbf5aae009ec");
for (i = 1; i 3f63cf61a49ed7ba7167d1a699beeee5");
for (j = 1; j c059d93c87ab4e4fc2d90e46c83c5fd7a4b561c25d9afb9ac8dc4d70affff419" : "b6c5a531a458a2e790c1fd6421739d1c");
document.write(value);
document.write(i == 1 ? "0d36329ec37a2cc24d42c7229b69747a01c3ce868d2b3d9bce8da5c1b7e41e5b" : "b90dd5946f0946207856a8a37f441edf");
}
document.write("fd273fcf5bcad3dfdad3c41bd81ad3e5");
}
document.write("f16b1740fad44fb09bfe928bcc527e08");
oSheet = null;
}
//-->
db271416853c42fd247a57c1a2c29eb6
34934661d7147ca926b095899343bad0
8fe789005baeba2b19438194d25444e6
f2475bc0d02a524246eacdb398ec424e
6eac519e8537e4205ddd37e30a7b548a


用JS讀取excel的例子 

4c55a87bbfcf0ccc58b6b746d044ff4b"版本使用單位類綱目節類說明綱說明目說明檔名保存年限共同分類號" THEN
RESPONSE.WRITE "eed1be1ec933ff042ba67abb41b92abcalert('EXCEL文件字段順序錯誤或字段數不對!!')db271416853c42fd247a57c1a2c29eb6"
exit Function
END IF
''讀取excel中的資料
do while Not rs.EOF
''將讀取的資料INSERT到oracle數據庫
for i=0 to rs.Fields.Count-1
EDITION=rs(0)
FILE_CODE=rs(2)+rs(3)+rs(4)+rs(5)
FILE_NAME=rs(9)
KIND1_DESC=rs(6)
KIND2_DESC=rs(7)
KIND3_DESC=rs(8)
KIND4_DESC=rs(9)
SAVE_YEAR=rs(10)
FILE_UNIT=rs(1)
COM_FILE_CODE=rs(11)
''==============================================
CHECED_SQL="Select nvl(FILE_CASE,'') FILE_CASE FROM ODM67 where EDITION='"&TRIM(EDITION)&"' and FILE_CODE='"&TRIM(FILE_CODE)&"' "
If mobjDB.OpenSQL(CHECED_SQL) Then
If mobjDB.IsEmpty Then
FILE_CASE="0001"
CASE_DESC="總案"
INS_SQL=""
INS_SQL=INS_SQL & " INSERT INTO ODM67(" & VBCRLF
INS_SQL=INS_SQL & " EDITION,FILE_CODE,FILE_CASE," & VBCRLF
INS_SQL=INS_SQL & " CASE_DESC,CRT_USER,CRT_DATE," & VBCRLF
INS_SQL=INS_SQL & " CRT_TIME,MDF_USER,MDF_DATE,MDF_TIME)" & VBCRLF
INS_SQL=INS_SQL & " VALUES(" & VBCRLF
INS_SQL=INS_SQL & " '"&TRIM(EDITION)&"','"&TRIM(FILE_CODE)&"'," & VBCRLF
INS_SQL=INS_SQL & " '"&TRIM(FILE_CASE)&"','"&TRIM(CASE_DESC)&"'," & VBCRLF
INS_SQL=INS_SQL & " '"&TRIM(SESSION("USER_ID"))&"','"&TRIM(TODAY)&"'," & VBCRLF
INS_SQL=INS_SQL & " '"&TRIM(NOWTIME)&"','"&TRIM(SESSION("USER_ID"))&"'," & VBCRLF
INS_SQL=INS_SQL & " '"&TRIM(TODAY)&"','"&TRIM(NOWTIME)&"')"
CALL mobjDB.ExecSQL(INS_SQL)
End If
End If
''==============================================
INS_SQL=""
INS_SQL=INS_SQL & " INSERT INTO ODM61( " & VBCRLF
INS_SQL=INS_SQL & " EDITION,FILE_CODE,FILE_NAME,KIND1_DESC," & VBCRLF
INS_SQL=INS_SQL & " KIND2_DESC,KIND3_DESC,KIND4_DESC,SAVE_YEAR," & VBCRLF
INS_SQL=INS_SQL & " FILE_UNIT,COM_FILE_CODE,CRT_USER,CRT_DATE," & VBCRLF
INS_SQL=INS_SQL & " CRT_TIME,MDF_USER,MDF_DATE,MDF_TIME)" & VBCRLF
INS_SQL=INS_SQL & " VALUES(" & VBCRLF
INS_SQL=INS_SQL & " '"&TRIM(EDITION)&"','"&TRIM(FILE_CODE)&"'," & VBCRLF
INS_SQL=INS_SQL & " '"&TRIM(FILE_NAME)&"','"&TRIM(KIND1_DESC)&"'," & VBCRLF
INS_SQL=INS_SQL & " '"&TRIM(KIND2_DESC)&"','"&TRIM(KIND3_DESC)&"'," & VBCRLF
INS_SQL=INS_SQL & " '"&TRIM(KIND4_DESC)&"','"&TRIM(SAVE_YEAR)&"'," & VBCRLF
INS_SQL=INS_SQL & " '"&TRIM(FILE_UNIT)&"','"&TRIM(COM_FILE_CODE)&"'," & VBCRLF
INS_SQL=INS_SQL & " '"&TRIM(SESSION("USER_ID"))&"','"&TRIM(TODAY)&"'," & VBCRLF
INS_SQL=INS_SQL & " '"&TRIM(NOWTIME)&"','"&TRIM(SESSION("USER_ID"))&"'," & VBCRLF
INS_SQL=INS_SQL & " '"&TRIM(TODAY)&"','"&TRIM(NOWTIME)&"')"
''RESPONSE.WRITE INS_SQL& "82c2f54f4cf245029ffb326befd49b3f"
IF mobjDB.ExecSQL(INS_SQL) THEN
InCount=InCount+1
ELSE
NoCount=NoCount+1
file=file&TODAY&" "&NOWTIME&" "&EDITION&" "&FILE_CODE & VBCRLF
END IF
exit for
next
rs.MoveNext
Loop
rs.close
set rs=nothing
Conn.close
set StrConn=nothing
if filea8093152e673feb7aba1828c43532094"" then
CALL CreateFolder()
call SetFile(file)
strpath=server.mappath("EXCEL_DATA.xls")
call DeleteFolder(strpath)
file=""
end if
End Function
'*********************************************************
' 目的: 新建一個指定的文件,如果存在就不新建,並向文件追加紀錄
' 傳入: file:要追加的數據
' 返回:
'*********************************************************
Function SetFile(file)
file_path="C:\LOG\OD60err.log"
set fstemp=server.CreateObject("Scripting.FileSystemObject")
IF (fstemp.FileExists(file_path)) THEN
ELSE
set filetemp=fstemp.CreateTextFile(file_path,true)
filetemp.writeLine "紀錄匯入失敗資料"
filetemp.close
END IF
''追加失敗資料信息OpenTextFile
set filetemp=fstemp.OpenTextFile(file_path,8,true)
filetemp.writeLine file
filetemp.close
set filetemp=Nothing
set fstemp=Nothing
End Function
'*********************************************************
' 目的: 新建一個指定的文件夾,如果存在就不新建
' 傳入:
' 返回:
'*********************************************************
Function CreateFolder()
Dim fso, f
folder="c:\LOG"
Set fso = CreateObject("Scripting.FileSystemObject")
IF fso.FolderExists(folder) THEN
ELSE
Set f = fso.CreateFolder(folder)
CreateFolderDemo = f.Path
END IF
End Function
'*********************************************************
' 目的:刪除上傳的文件,
' 傳入:傳入上傳文件的虛擬路徑
' 返回:
'*********************************************************
Function DeleteFolder(filepath)
Dim fso, f
folder="EXCEL_DATA.xls"
Set fso = CreateObject("Scripting.FileSystemObject")
''response.write fso.FileExists(filepath)
IF fso.FileExists(filepath) THEN
fso.DeleteFile filepath
END IF
End Function
%>


复制代码 代码如下:


3f1c4e4b6b16bbbd69b2ee476dc4f83a
function readThis(){
var tempStr = "";
var filePath= document.all.upfile.value;
var oXL = new ActiveXObject("Excel.application");
var oWB = oXL.Workbooks.open(filePath);
oWB.worksheets(1).select();
var oSheet = oWB.ActiveSheet;
try{
for(var i=2;i12c35a5d4d9d9ebb2a1a2728a430b9c8
100db36a723c770d327fc0aef2ce13b1
385593bf7209582d73c2d3d650334bb8a7e1e84ae9ae90c7575fa0f1aa364ee8
0c6dc11e160d3b678d68754cc175188a
17753354e4c2f2d0ae75f3fe89c62dc240587128eee8df8f03d0b607fe983014
73a6ac4ed44ffec12cee46588e518a5e

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn