Js code to read EXCEL file

js file to read EXCEL implementation code, we can compare all the self-test.
Home to one a Chinese illustration, the following examples are many more than we can test.
Copy the code the code is as follows:

<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  Value
excelSheet.usedrange.rows.count;//  The number of lines used
excelWorkBook.Worksheets.count;//  Be the number of 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>

Copy the code the code is as follows:
If the page open EXCEL files, then closed, the process where there EXCEL.EXE, it must be closed, refresh this page!

<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" /><input type="button" value="read">
<br>
<textarea cols=50 rows=10></textarea>
</html>

II
js code to read excel file copy the code below:

<script>
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;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){
//alert(e);
document.all.txtArea.value = tempStr;
}
document.all.txtArea.value = tempStr; oXL.Quit();
CollectGarbage();
}
</script>
<html>
<input type="file" />
<input type="button" value="  Read  "><br>
<textarea cols=50 rows=10></textarea>
</html>

III
I have vs2005 platform to realize such a function, click a button once to a large number of excel file data into sqlserver2005 in I'm using ajax technology to do gymnastics in the foreground with javascript excel file, loop to read all the excel files, each read a line into an array via the web service with c # language reached back to a row of data into the database. Thinking about what happened.
Now function has been achieved, the specific code as the definition of a function with javascript, loop to read excel file data copy the code code:

function readExcel()
{
try
{
var ExcelNum=new Array();
//  Repeated before importing, delete the data over the same period last imported
WebServiceExcel.deleteOldNumber();
var oXL = new ActiveXObject( "Excel.Application ");
\\r_c_num[5]  The value of the name of excel file
var path=document.all.excelpath.value+ "\\ "+r_c_num[5]
var oWB = oXL.Workbooks.open(path);
\\  If you have multiple excel files  sheet  Then read from the first sheet loop
for(var x=1;x <=oWB.worksheets.count;x++)
{
oWB.worksheets(x).select();
var oSheet =oWB.ActiveSheet;
\\  Starting at the specified row and column start reading the excel file data
for(var i=parseInt(r_c_num[6]);i <=parseInt(r_c_num[7]);i++)
{
for(var j=parseInt(r_c_num[8]);j <=parseInt(r_c_num[9]);j++)
{
if(typeof(oSheet.Cells(i,j).value)== "undefined ")
{
ExcelNum[j-parseInt(r_c_num[8])+6]= " ";
}
else
{
switch_letter(j);
ExcelNum[j-parseInt(r_c_num[8])+6]=oSheet.Cells(i,j).value:
}
}
//  The line will read the data transmitted back into the database
WebServiceExcel.insert_From_Excel(ExcelNum);
}
}
}
}
catch(err)
{
alert( "  Wrong  , "+err.message);
}
}

This is the key to the code front.
The problem now is that if the excel file data too much, then import process should such a long time, poor performance, and do not know how to improve? ? ? If you lead thousands of lines of data on to die, so I can not stand the time. Please enlighten me master, I am anxious to use, thank you! ! !
A combination of using JavaScript to read local Excel.Application excel file and present a simple example of a table copy the code code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!--
var excelFileName = "E:/project/eomstools/ShowTaskCodeWorkbook/test.xls";
var oWB;
function showExcel(targetDIVID){
//objID  To form  ID
//  Need to set the browser security level to enable  ActiveX
// Start Excel and get Application object.
var oXL=null;
try{
oXL = new ActiveXObject("Excel.Application");
}catch(e){
alert(e.message);
return ;
}
if (oXL == null){
alert("  Create Excel file failed  , Possible that your computer is not properly installed Microsoft Office Excel software or browser security level is set too high  !");
return;
}
try{
// Get a new workbook.
oWB = oXL.Workbooks.Open(excelFileName);
for (i = 1; i <= oWB.Sheets.Count; i++){
if (oWB.Sheets(i).name.lastIndexOf("  Months ") != -1){
showSheet(i);
}
}
}
catch (e){
alert(e.message);
}
oWB.Close();   //  If the workbook does not close  ,  Or very serious consequences  .
oWB = null;
oXL = null;
}
function showSheet(sheetNO){
var oSheet = oWB.Sheets(sheetNO);
document.write("<table border=1>");
for (i = 1; i < oSheet.UsedRange.Rows.Count; i++){
document.write("<tr>");
for (j = 1; j < oSheet.UsedRange.Columns.Count; j++){
value = oSheet.Cells(i, j).Value;
if (value == undefined){
value = " ";
}
document.write(i == 1 ? "<th nowrap=true><b>" : "<td>");
document.write(value);
document.write(i == 1 ? "</b></th>" : "</td>");
}
document.write("</tr>");
}
document.write("</table>");
oSheet = null;
}
//-->
</SCRIPT>
</HEAD>
<BODY onLoad="showExcel();">
</BODY>
</HTML>

js file to read EXCEL implementation code, we can compare all the self-test.

Read excel with the example of JS code copy the code below:

<%
''
'*********************************************************
'   Objective  :  After reading the excel data into the database also records the number of successful and unsuccessful
'   Afferent  :
'   Back  :
'*********************************************************
Function GetExcel()
Dim conn
Dim StrConn
Dim rs
Dim Sql
file=""
Set conn=Server.CreateObject("ADODB.Connection")
StrConn="Driver={Microsoft Excel Driver (*.xls)};DBQ="& Server.MapPath("EXCEL_DATA.xls")
''StrConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=dd.xls;Extended Properties=Excel 8.0"
conn.Open StrConn
Set rs = Server.CreateObject("ADODB.Recordset")
Sql="select * from [Sheet1$]"
rs.Open Sql,conn,2,2
''  Read excel in the field name  ,  Prosecutor field order is correct and
for i=0 to rs.Fields.Count-1
FILE_HEAD=FILE_HEAD&rs(i).Name
next
''response.write FILE_HEAD
IF trim(FILE_HEAD)<>"  Class schema version using the unit section that outline description of class projects that save the life of the common file name Key words  " THEN
RESPONSE.WRITE "<SCRIPT LANGUAGE='JAVASCRIPT'>alert('EXCEL  File field order wrong or right number of fields  !!')</SCRIPT>"
exit Function
END IF
''  Read the data in excel
do while Not rs.EOF
''  INSERT the data will be read to  oracle  Database
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="  General case  "
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& "<BR>"
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 file<>"" then
CALL CreateFolder()
call SetFile(file)
strpath=server.mappath("EXCEL_DATA.xls")
call DeleteFolder(strpath)
file=""
end if
End Function
'*********************************************************
'   Objective  :   Create a new file specified, if there is no new  ,  Additional records to the file
'   Afferent  : file:  The data to be appended
'   Back  :
'*********************************************************
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 "  Import data records failed  "
filetemp.close
END IF
''  Additional data and information failure  OpenTextFile
set filetemp=fstemp.OpenTextFile(file_path,8,true)
filetemp.writeLine file
filetemp.close
set filetemp=Nothing
set fstemp=Nothing
End Function
'*********************************************************
'   Objective  :   Create a specific folder, if there is no new
'   Afferent  :
'   Back  :
'*********************************************************
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
'*********************************************************
'   Objective  :  Delete uploaded file  ,
'   Afferent  :  Incoming virtual path to the uploaded file
'   Back  :
'*********************************************************
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
%> 

  Copy the code below code  :
<script>
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;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){
//alert(e);
document.all.txtArea.value = tempStr;
}
document.all.txtArea.value = tempStr;
oXL.Quit();
CollectGarbage();
}
</script>
<html>
<input type="file" /><input type="button" value="  Read  ">
<br>
<textarea cols=50 rows=10></textarea>
</html>
分类:Web 时间:2010-08-25 人气:368
分享到:
blog comments powered by Disqus

相关文章

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.388 (s). 12 q(s)