Can read Excel file JS code 1th/2 page _javascript tips

Source: Internet
Author: User
Home to a Chinese description of the example, the following example a lot of people can test more.
Copy Code code 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 of Value;//cell
Number of rows used by excelsheet.usedrange.rows.count;//
excelworkbook.worksheets.count;//get 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 Code code as follows:

If you open an Excel file on a Web page, then there is a EXCEL.EXE in the process, so you must close it and 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"? "": O Sheet.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>
<input type= "File" id= "Upfile"/><input type= "button" onclick= "Readexcel ();" value= "read" >
<br>
<textarea id= "Txtarea" cols=50 rows=10></textarea>

Two
JS Read Excel File
Copy Code code as follows:

<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"? "": O Sheet.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>
<input type= "File" id= "Upfile"/>
<input type= "button" onclick= "Readthis ();" value= "read" ><br>
<textarea id= "Txtarea" cols=50 rows=10></textarea>

Three
I am on the VS2005 platform to achieve such a function, click a button to import a large number of Excel file data into the sqlserver2005
I am using AJAX technology, in the foreground using JavaScript to do Excel files, looping read all the Excel files, each read a row into an array in a Web service to the background in the C # language to insert a row of data into the database. That's probably the way it is.
Now the function has been implemented, the specific code is as follows
Use JavaScript to define a function that iterates through Excel file data
Copy Code code as follows:

function Readexcel ()
{
Try
{
var excelnum=new Array ();
Delete the same contemporaneous data that was last imported before repeating the import
Webserviceexcel.deleteoldnumber ();
var oXL = new ActiveXObject ("Excel.Application");
The value of \\r_c_num[5] is the name of the Excel file
var path=document.all.excelpath.value+ "\" +r_c_num[5]
var owb = oXL.Workbooks.open (path);
\ If the Excel file has multiple sheet, read from the first sheet loop
for (var x=1;x <=owb.worksheets.count;x++)
{
Owb.worksheets (x). Select ();
var osheet =owb.activesheet;
\ \ \ Reads the data for the Excel file by the specified start line and start column
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;
}
}
Upload a read row of data to the background to insert into the database
Webserviceexcel.insert_from_excel (Excelnum);
}
}
}
}
catch (Err)
{
Alert ("A mistake," +err.message);
}
}

This is just the key code for the front desk.
Now the problem is that if there are too many Excel file data, the import process to wait for a long time, poor performance, do not know how to improve??? If the guide thousands of line data is not good, time let me unbearable. Please enlighten me, very anxious to use, thank you!!!
A simple example of using JavaScript and excel.application to read local Excel files and render them in tabular form
Copy Code code as follows:

<! 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 for table ID
Need to enable ActiveX in browser security level settings
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 ("The creation of an Excel file failed, it may be that your computer does not install the Microsoft Office Excel software correctly or the browser's security level setting is 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 ("month")!=-1) {
Showsheet (i);
}
}
}
catch (e) {
alert (e.message);
}
Owb.close (); Not close to the workbook, the consequences are quite serious.
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>

current 1/2 page   1 2 Next read the full text

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.