- 浏览: 27463 次
- 性别:
- 来自: 西安
文章分类
最新评论
-
jianghebo:
我的多数据源切换时成功的,我使用的是JNDI数据源,想知道怎么 ...
Spring JTA应用JOTM & Atomikos II JOTM -
Mr.TianShu:
flash的听说不太完美,pdf的我找到一个。。。此时还是个测 ...
可以读取EXCEL文件的js代码 -
joehe:
楼主生成了mysql的没?
通过PowerDesigner生成数据库表 -
basakasky:
ctrl+c和ctrl+v来的文章?
通过PowerDesigner生成数据库表 -
yibunengjing:
DB designer工具用来用去,还是powerDesign ...
通过PowerDesigner生成数据库表
可以读取EXCEL文件的js代码
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文件
复制代码 代码如下:
<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" id="upfile" />
<input type="button" onclick="readThis();" value="读取"><br>
<textarea id="txtArea" cols=50 rows=10></textarea>
</html>
三、
我在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 <=oWB.worksheets.count;x++)
{
oWB.worksheets(x).select();
var oSheet =oWB.ActiveSheet;
\\按指定开始行和开始列读取excel文件的数据
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;
}
}
//将读取的一行数据传到后台插入到数据库
WebServiceExcel.insert_From_Excel(ExcelNum);
}
}
}
}
catch(err)
{
alert( "出错了, "+err.message);
}
}
这只是前台的关键代码。
现在的问题是,如果excel文件数据太多的话,导入过程要等好长时间,性能太差了,不知道该怎么改进???如果导几千行数据就不行了,时间让我无法忍受。请高手赐教,很着急用,谢谢了!!!
一个用JavaScript结合Excel.Application读取本地excel文件并以表格呈现的简单例子
复制代码 代码如下:
<!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为表格ID
//需要在浏览器安全级别设置中启用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("创建Excel文件失败,可能是您的计算机上没有正确安装Microsoft Office Excel软件或浏览器的安全级别设置过高!");
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("月") != -1){
showSheet(i);
}
}
}
catch (e){
alert(e.message);
}
oWB.Close(); //不close工作簿的话,后果还是挺严重的。
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读取 EXCEL 文件 的实现代码,比较全了大家可以自行测试了。
用JS讀取excel的例子
复制代码 代码如下:
<%
''
'*********************************************************
' 目的:讀取excel資料後插入到數據庫中同時紀錄成功和失敗的數目
' 傳入:
' 返回:
'*********************************************************
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
''讀取excel中的字段名稱,並檢察字段順序是否正確
for i=0 to rs.Fields.Count-1
FILE_HEAD=FILE_HEAD&rs(i).Name
next
''response.write FILE_HEAD
IF trim(FILE_HEAD)<>"版本使用單位類綱目節類說明綱說明目說明檔名保存年限共同分類號" THEN
RESPONSE.WRITE "<SCRIPT LANGUAGE='JAVASCRIPT'>alert('EXCEL文件字段順序錯誤或字段數不對!!')</SCRIPT>"
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& "<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
'*********************************************************
' 目的: 新建一個指定的文件,如果存在就不新建,並向文件追加紀錄
' 傳入: 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
%>
复制代码 代码如下:
<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" id="upfile" /><input type="button" onclick="readThis();" value="读取">
<br>
<textarea id="txtArea" cols=50 rows=10></textarea>
</html>
首页给个有中文说明的例子,下面的例子很多大家可以多测试。
复制代码 代码如下:
<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文件
复制代码 代码如下:
<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" id="upfile" />
<input type="button" onclick="readThis();" value="读取"><br>
<textarea id="txtArea" cols=50 rows=10></textarea>
</html>
三、
我在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 <=oWB.worksheets.count;x++)
{
oWB.worksheets(x).select();
var oSheet =oWB.ActiveSheet;
\\按指定开始行和开始列读取excel文件的数据
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;
}
}
//将读取的一行数据传到后台插入到数据库
WebServiceExcel.insert_From_Excel(ExcelNum);
}
}
}
}
catch(err)
{
alert( "出错了, "+err.message);
}
}
这只是前台的关键代码。
现在的问题是,如果excel文件数据太多的话,导入过程要等好长时间,性能太差了,不知道该怎么改进???如果导几千行数据就不行了,时间让我无法忍受。请高手赐教,很着急用,谢谢了!!!
一个用JavaScript结合Excel.Application读取本地excel文件并以表格呈现的简单例子
复制代码 代码如下:
<!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为表格ID
//需要在浏览器安全级别设置中启用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("创建Excel文件失败,可能是您的计算机上没有正确安装Microsoft Office Excel软件或浏览器的安全级别设置过高!");
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("月") != -1){
showSheet(i);
}
}
}
catch (e){
alert(e.message);
}
oWB.Close(); //不close工作簿的话,后果还是挺严重的。
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读取 EXCEL 文件 的实现代码,比较全了大家可以自行测试了。
用JS讀取excel的例子
复制代码 代码如下:
<%
''
'*********************************************************
' 目的:讀取excel資料後插入到數據庫中同時紀錄成功和失敗的數目
' 傳入:
' 返回:
'*********************************************************
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
''讀取excel中的字段名稱,並檢察字段順序是否正確
for i=0 to rs.Fields.Count-1
FILE_HEAD=FILE_HEAD&rs(i).Name
next
''response.write FILE_HEAD
IF trim(FILE_HEAD)<>"版本使用單位類綱目節類說明綱說明目說明檔名保存年限共同分類號" THEN
RESPONSE.WRITE "<SCRIPT LANGUAGE='JAVASCRIPT'>alert('EXCEL文件字段順序錯誤或字段數不對!!')</SCRIPT>"
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& "<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
'*********************************************************
' 目的: 新建一個指定的文件,如果存在就不新建,並向文件追加紀錄
' 傳入: 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
%>
复制代码 代码如下:
<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" id="upfile" /><input type="button" onclick="readThis();" value="读取">
<br>
<textarea id="txtArea" cols=50 rows=10></textarea>
</html>
评论
1 楼
Mr.TianShu
2012-12-11
flash的听说不太完美,
pdf的我找到一个。。。此时还是个测试版,
如果有完美的js可操纵office全套和pdf就好了,可以把flash踢掉。
# PDF.JS
pdf.js is an HTML5 technology experiment that explores building a faithful
and efficient Portable Document Format (PDF) renderer without native code
assistance.
pdf.js is community-driven and supported by Mozilla Labs. Our goal is to
create a general-purpose, web standards-based platform for parsing and
rendering PDFs, and eventually release a PDF reader extension powered by
pdf.js. Integration with Firefox is a possibility if the experiment proves
successful.
# Getting started
### Online demo
For an online demo, visit:
+ http://mozilla.github.com/pdf.js/web/viewer.html
This demo provides an interactive interface for displaying and browsing PDFs
using the pdf.js API.
### Extension
A Firefox extension is availble in two places:
+ Stable Version: https://addons.mozilla.org/firefox/addon/pdfjs
+ Development Version: http://mozilla.github.com/pdf.js/extensions/firefox/pdf.js.xpi
The development extension should be quite stable but still might break from time to time.
Also, note that the development extension is updated on every merge and by default Firefox will
auto-update extensions on a daily basis (you can change this through the
`extensions.update.interval` option in `about:config`).
For an experimental Chrome extension, get the code as explained below and issue `node make extension`.
Then open Chrome, go to `Tools > Extension` and load the (unpackaged) extension
from the directory `build/chrome`.
### Getting the code
To get a local copy of the current code, clone it using git:
$ git clone git://github.com/mozilla/pdf.js.git pdfjs
$ cd pdfjs
Next, you need to start a local web server as some browsers don't allow opening
PDF files for a file:// url:
$ node make server
You can install Node via [nvm](https://github.com/creationix/nvm) or the
[official package](http://nodejs.org). If everything worked out, you can now serve
+ http://localhost:8888/web/viewer.html
You can also view all the test pdf files on the right side serving
+ http://localhost:8888/test/pdfs/?frame
### Building pdf.js.
In order to bundle all `src/` files into a final `pdf.js` and build the generic viewer, issue:
$ node make generic
This will generate the file `build/generic/build/pdf.js` that can be included in your final project. The pdf.js file is large and should be minified for production. Also, if you would like to support more browsers than firefox you'll also need to include `compatibility.js` from `build/generic/web/`.
# Learning
You can play with the PDF.js API directly from your browser through the live demos below:
+ Hello world: http://jsbin.com/pdfjs-helloworld-v2/edit#html,live
+ Simple reader with prev/next page controls: http://jsbin.com/pdfjs-prevnext-v2/edit#html,live
The repo contains a hello world example that you can run locally:
+ [examples/helloworld/](https://github.com/mozilla/pdf.js/blob/master/examples/helloworld/)
For an introduction to the PDF.js code, check out the presentation by our contributor Julian Viereck:
+ http://www.youtube.com/watch?v=Iv15UY-4Fg8
Additional learning resources can be found at:
+ https://github.com/mozilla/pdf.js/wiki/Additional-Learning-Resources
# Contributing
pdf.js is a community-driven project, so contributors are always welcome.
Simply fork our repo and contribute away. Good starting places for picking
a bug are the top error messages and TODOs in our corpus report:
+ http://people.mozilla.com/~bdahl/corpusreport/test/ref/
and of course our open Github issues:
+ https://github.com/mozilla/pdf.js/issues
For better consistency and long-term stability, please do look around the
code and try to follow our conventions.
More information about the contributor process can be found on the
[contributor wiki page](https://github.com/mozilla/pdf.js/wiki/Contributing).
If you don't want to hack on the project or have little spare time, __you still
can help!__ Just open PDFs in the
[online demo](http://mozilla.github.com/pdf.js/web/viewer.html) and report
any breakage in rendering.
Our Github contributors so far:
+ https://github.com/mozilla/pdf.js/contributors
+ https://github.com/mozilla/pdf.js/blob/master/LICENSE
You can add your name to it!
# Running the tests
pdf.js comes with browser-level regression tests that allow one to probe
whether it's able to successfully parse PDFs, as well as compare its output
against reference images, pixel-by-pixel.
More information about running the tests can be found on the
[contributor wiki page](https://github.com/mozilla/pdf.js/wiki/Contributing).
# Additional resources
Gallery of user projects and modifications:
+ https://github.com/mozilla/pdf.js/wiki/Gallery-of-user-projects-and-modifications
You can read more about pdf.js here:
+ http://andreasgal.com/2011/06/15/pdf-js/
+ http://blog.mozilla.com/cjones/2011/06/15/overview-of-pdf-js-guts/
+ https://github.com/mozilla/pdf.js/wiki/Additional-Learning-Resources
Talk to us on IRC:
+ #pdfjs on irc.mozilla.org
Join our mailing list:
+ dev-pdf-js@lists.mozilla.org
Subscribe either using lists.mozilla.org or Google Groups:
+ https://lists.mozilla.org/listinfo/dev-pdf-js
+ https://groups.google.com/group/mozilla.dev.pdf-js/topics
Follow us on twitter: @pdfjs
+ http://twitter.com/#!/pdfjs
pdf的我找到一个。。。此时还是个测试版,
如果有完美的js可操纵office全套和pdf就好了,可以把flash踢掉。
# PDF.JS
pdf.js is an HTML5 technology experiment that explores building a faithful
and efficient Portable Document Format (PDF) renderer without native code
assistance.
pdf.js is community-driven and supported by Mozilla Labs. Our goal is to
create a general-purpose, web standards-based platform for parsing and
rendering PDFs, and eventually release a PDF reader extension powered by
pdf.js. Integration with Firefox is a possibility if the experiment proves
successful.
# Getting started
### Online demo
For an online demo, visit:
+ http://mozilla.github.com/pdf.js/web/viewer.html
This demo provides an interactive interface for displaying and browsing PDFs
using the pdf.js API.
### Extension
A Firefox extension is availble in two places:
+ Stable Version: https://addons.mozilla.org/firefox/addon/pdfjs
+ Development Version: http://mozilla.github.com/pdf.js/extensions/firefox/pdf.js.xpi
The development extension should be quite stable but still might break from time to time.
Also, note that the development extension is updated on every merge and by default Firefox will
auto-update extensions on a daily basis (you can change this through the
`extensions.update.interval` option in `about:config`).
For an experimental Chrome extension, get the code as explained below and issue `node make extension`.
Then open Chrome, go to `Tools > Extension` and load the (unpackaged) extension
from the directory `build/chrome`.
### Getting the code
To get a local copy of the current code, clone it using git:
$ git clone git://github.com/mozilla/pdf.js.git pdfjs
$ cd pdfjs
Next, you need to start a local web server as some browsers don't allow opening
PDF files for a file:// url:
$ node make server
You can install Node via [nvm](https://github.com/creationix/nvm) or the
[official package](http://nodejs.org). If everything worked out, you can now serve
+ http://localhost:8888/web/viewer.html
You can also view all the test pdf files on the right side serving
+ http://localhost:8888/test/pdfs/?frame
### Building pdf.js.
In order to bundle all `src/` files into a final `pdf.js` and build the generic viewer, issue:
$ node make generic
This will generate the file `build/generic/build/pdf.js` that can be included in your final project. The pdf.js file is large and should be minified for production. Also, if you would like to support more browsers than firefox you'll also need to include `compatibility.js` from `build/generic/web/`.
# Learning
You can play with the PDF.js API directly from your browser through the live demos below:
+ Hello world: http://jsbin.com/pdfjs-helloworld-v2/edit#html,live
+ Simple reader with prev/next page controls: http://jsbin.com/pdfjs-prevnext-v2/edit#html,live
The repo contains a hello world example that you can run locally:
+ [examples/helloworld/](https://github.com/mozilla/pdf.js/blob/master/examples/helloworld/)
For an introduction to the PDF.js code, check out the presentation by our contributor Julian Viereck:
+ http://www.youtube.com/watch?v=Iv15UY-4Fg8
Additional learning resources can be found at:
+ https://github.com/mozilla/pdf.js/wiki/Additional-Learning-Resources
# Contributing
pdf.js is a community-driven project, so contributors are always welcome.
Simply fork our repo and contribute away. Good starting places for picking
a bug are the top error messages and TODOs in our corpus report:
+ http://people.mozilla.com/~bdahl/corpusreport/test/ref/
and of course our open Github issues:
+ https://github.com/mozilla/pdf.js/issues
For better consistency and long-term stability, please do look around the
code and try to follow our conventions.
More information about the contributor process can be found on the
[contributor wiki page](https://github.com/mozilla/pdf.js/wiki/Contributing).
If you don't want to hack on the project or have little spare time, __you still
can help!__ Just open PDFs in the
[online demo](http://mozilla.github.com/pdf.js/web/viewer.html) and report
any breakage in rendering.
Our Github contributors so far:
+ https://github.com/mozilla/pdf.js/contributors
+ https://github.com/mozilla/pdf.js/blob/master/LICENSE
You can add your name to it!
# Running the tests
pdf.js comes with browser-level regression tests that allow one to probe
whether it's able to successfully parse PDFs, as well as compare its output
against reference images, pixel-by-pixel.
More information about running the tests can be found on the
[contributor wiki page](https://github.com/mozilla/pdf.js/wiki/Contributing).
# Additional resources
Gallery of user projects and modifications:
+ https://github.com/mozilla/pdf.js/wiki/Gallery-of-user-projects-and-modifications
You can read more about pdf.js here:
+ http://andreasgal.com/2011/06/15/pdf-js/
+ http://blog.mozilla.com/cjones/2011/06/15/overview-of-pdf-js-guts/
+ https://github.com/mozilla/pdf.js/wiki/Additional-Learning-Resources
Talk to us on IRC:
+ #pdfjs on irc.mozilla.org
Join our mailing list:
+ dev-pdf-js@lists.mozilla.org
Subscribe either using lists.mozilla.org or Google Groups:
+ https://lists.mozilla.org/listinfo/dev-pdf-js
+ https://groups.google.com/group/mozilla.dev.pdf-js/topics
Follow us on twitter: @pdfjs
+ http://twitter.com/#!/pdfjs
相关推荐
以`SheetJS`为例,可以使用以下代码读取Excel文件: ```javascript var reader = new FileReader(); reader.onload = function(e) { var data = e.target.result; var workbook = XLSX.read(data, {type: 'binary...
在本文中,我们将深入探讨如何使用js-xlsx来读取Excel文件,以及相关的技术细节。 首先,我们需要理解Excel文件的基本结构。Excel文件通常是基于XML的结构化格式,如`.xlsx`文件实际上是基于ZIP打包的一系列XML文档...
对于JavaScript读取Excel文件的跨平台解决方案,可以考虑使用像` SheetJS`、`js-xlsx`或`papaparse`这样的库,它们提供了一套API来解析Excel文件,并且支持多种浏览器。 综上所述,JavaScript通过ActiveXObject在IE...
##### 2.1 使用ActiveXObject读取Excel文件 在IE浏览器环境下,可以通过`ActiveXObject`对象来操作Office应用程序,例如Excel。这种方式适用于客户端环境,并且要求用户的浏览器支持ActiveX控件。 ##### 2.2 代码...
以下是一个使用ActiveX对象的JavaScript代码示例,它允许在Internet Explorer中打开并读取Excel文件的内容: ```javascript function readExcel() { var excelApp; var excelWorkBook; var excelSheet; try { ...
然而,在某些特定情况下,如在客户端处理用户上传的Excel文件,我们可能需要实现JavaScript读取Excel文件的功能。下面将详细介绍如何在JavaScript中实现这一功能,以及相关知识点。 1. **使用FileReader API** ...
以下是一个简单的示例代码片段,展示了如何使用FileReader和`js-xls`库读取Excel文件: ```javascript <input type="file" id="excelFile" accept=".xls"> <script src="path/to/js-xls.min.js"> document....
JavaScript(JS)作为前端开发的主要语言,通过特定的技术手段,能够实现对本地Excel文件的读取与解析。本文将深入探讨如何利用JS来读取和解析Excel文件,以及如何进行数据验证,确保上传的数据符合预设的格式要求。...
3. **JavaScript接口**:由于Unity WebGL与Web浏览器之间的通信是通过JavaScript接口(JSIL)完成的,你需要创建一个JavaScript文件,暴露一个方法供Unity调用,以实现读取Excel文件的功能。 4. **用户上传文件**:...
本篇文章将详细探讨如何在uniapp中读取Excel文件,特别是xlsx格式的文件内容。 首先,我们需要了解xlsx文件的结构。xlsx是Microsoft Office Excel 2007及更高版本使用的文件格式,基于Open XML标准。它是由一系列...
在读取Excel文件时,我们可能需要先将其转换为ArrayBuffer,然后才能进一步解析。 5. Excel文件格式: Excel文件通常是二进制的,常见的格式有`.xls`(老式BIFF格式)和`.xlsx`(基于Open XML标准)。`.xlsx`文件...
在Web开发中,有时我们...综上所述,通过使用`FileReader API`和前端库如SheetJS,我们可以实现Web端直接读取Excel文件,简化了以往需要服务器端处理的流程。这种方法虽然简单,但需注意其适用范围和潜在的安全问题。
9. **使用示例**:一个简单的使用`js-xlsx`读取Excel文件并显示在HTML表格的示例: ```javascript var reader = new FileReader(); reader.onload = function(e) { var binaryString = e.target.result; var ...
它能够读取Excel文件,并将其内容转换为JSON对象,这对于数据的处理和展示非常方便。通过使用`XLSX.read`方法,我们可以加载二进制文件或者base64编码的字符串,并将它们解析成工作表对象。例如: ```javascript ...
3. **读取Excel文件**:能够快速读取Excel文件中的数据,并将其转换为易于处理的数据格式,如数组或列表等。 4. **跨平台兼容性**:**UNI-Excel插件**经过优化,能够在不同的操作系统和设备上运行,包括Windows、...
4. `demo.jsx`:这是一个示例脚本,可能包含了如何使用`xlsx.extendscript.js`库来读取Excel文件的代码。开发者可以通过阅读和运行这个脚本来学习如何在Photoshop的JSX环境中操作Excel文件。 5. `sheetjs.xlsx`:...
- 代码示例展示了如何在读取Excel文件时进行基本的数据提取和格式化。 通过上述知识点,我们可以了解到,虽然可以使用JavaScript通过ActiveX对象与Excel交互,但这种方法存在兼容性问题,仅适用于IE浏览器,并且...
Python+JavaScript+html编写的免费刷题工具,可以实现浏览器读取本地excel文件,自定义题库。 软件特色: 1.完全免费使用,没有任何广告插件; 2.实现了浏览器读取本地excel文件,非IE浏览器也可以轻松读取本地文件...
上述代码是一个基础的操作示例,展示了如何在Node.js中通过node-xlsx模块进行简单的Excel文件读取和写入操作。为了更好地理解和使用这些模块,开发者需要深入阅读各自模块的官方文档,理解其API的细节,以及如何处理...
3. **读取Excel文件**:使用ADO(ActiveX Data Objects)的Recordset对象,可以连接到Excel文件并读取数据。首先,创建一个ADODB.Connection对象连接到Excel文件,然后打开一个ADODB.Recordset对象来获取数据。数据...