|
復(fù)制代碼 代碼如下:
<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;//使用的行數(shù)
excelWorkBook.Worksheets.count;//得到sheet的個(gè)數(shù)
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>
復(fù)制代碼 代碼如下:
如果是在網(wǎng)頁上打開EXCEL 文件,那么在關(guān)閉的時(shí)候,進(jìn)程里還有EXCEL.EXE,所以必須關(guān)閉后,刷新本頁面!
<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文件
復(fù)制代碼 代碼如下:
<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平臺(tái)上要實(shí)現(xiàn)這么一個(gè)功能,點(diǎn)擊一個(gè)按鈕一次將大量的excel文件數(shù)據(jù)導(dǎo)入到sqlserver2005中
我用的是ajax技術(shù),在前臺(tái)用Javascript操做excel文件,循環(huán)讀取所有的excel文件,每讀取一行就放進(jìn)一個(gè)數(shù)組里通過web服務(wù)傳到后臺(tái)用c#語言將一行數(shù)據(jù)插入到數(shù)據(jù)庫。思路大概就是這樣。
現(xiàn)在功能已經(jīng)實(shí)現(xiàn)了,具體代碼如下
用Javascript定義一個(gè)函數(shù),循環(huán)讀取excel文件數(shù)據(jù)
復(fù)制代碼 代碼如下:
function readExcel()
{
try
{
var ExcelNum=new Array();
//重復(fù)導(dǎo)入之前,刪除上次導(dǎo)入的同期數(shù)據(jù)
WebServiceExcel.deleteOldNumber();
var oXL = new ActiveXObject( "Excel.Application ");
//r_c_num[5]的值為excel文件的名字
var path=document.all.excelpath.value+ "http:// "+r_c_num[5]
var oWB = oXL.Workbooks.open(path);
//如果excel文件有多個(gè)sheet的話從第一個(gè)sheet循環(huán)讀取
for(var x=1;x <=oWB.worksheets.count;x++)
{
oWB.worksheets(x).select();
var oSheet =oWB.ActiveSheet;
//按指定開始行和開始列讀取excel文件的數(shù)據(jù)
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;
}
}
//將讀取的一行數(shù)據(jù)傳到后臺(tái)插入到數(shù)據(jù)庫
WebServiceExcel.insert_From_Excel(ExcelNum);
}
}
}
}
catch(err)
{
alert( "出錯(cuò)了, "+err.message);
}
}
這只是前臺(tái)的關(guān)鍵代碼。
現(xiàn)在的問題是,如果excel文件數(shù)據(jù)太多的話,導(dǎo)入過程要等好長(zhǎng)時(shí)間,性能太差了,不知道該怎么改進(jìn)???如果導(dǎo)幾千行數(shù)據(jù)就不行了,時(shí)間讓我無法忍受。請(qǐng)高手賜教,很著急用,謝謝了?。。?
一個(gè)用JavaScript結(jié)合Excel.Application讀取本地excel文件并以表格呈現(xiàn)的簡(jiǎn)單例子
復(fù)制代碼 代碼如下:
<!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
//需要在瀏覽器安全級(jí)別設(shè)置中啟用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("創(chuàng)建Excel文件失敗,可能是您的計(jì)算機(jī)上沒有正確安裝Microsoft Office Excel軟件或?yàn)g覽器的安全級(jí)別設(shè)置過高!");
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工作簿的話,后果還是挺嚴(yán)重的。
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>
JavaScript技術(shù):可以讀取EXCEL文件的js代碼第1/2頁,轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。