You can read the JS Code Page 1/2 of an Excel file.

Source: Internet
Author: User

The home page provides an example with Chinese instructions. Many of the following examples can be tested. CopyCode 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; // number of rows used
Excelworkbook. worksheets. Count; // obtain 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 The Code is as follows: if you open an Excel file on a webpage, there is Excel. EXE in the process when you close the file. Therefore, you must close the file and refresh the 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.txt area. value = tempstr;
}
Document.all.txt area. 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>

II,
JS reads Excel files Copy code The Code is 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 "? "": 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.txt area. value = tempstr;
}
Document.all.txt area. value = tempstr; oxl. Quit ();
Collectgarbage ();
}
</SCRIPT>
<HTML>
<Input type = "file" id = "upfile"/>
<Input type = "button" onclick = "readthis ();" value = "read"> <br>
<Textarea id = "txtarea" Cols = 50 rows = 10> </textarea>
</Html>

III,
To implement this function on the vs2005 platform, click a button to import a large amount of Excel file data to sqlserver2005 at a time.
I use Ajax technology. I use JavaScript on the front-end to create an Excel file and read all the Excel files cyclically, each row is read into a number of groups and uploaded to the background using the Web service to insert a row of data into the database in C # language. The idea is probably like this.
The function has been implemented. The Code is as follows:
Define a function using JavaScript to read Excel file data cyclically Copy code The Code is as follows: function readexcel ()
{
Try
{
VaR excelnum = new array ();
// Delete the same-period data of the last import before repeated Import
Webserviceexcel. deleteoldnumber ();
VaR oxl = new activexobject ("Excel. application ");
\ 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 an Excel file has multiple sheets, read them cyclically from the first sheet.
For (VAR x = 1; x <= owb. worksheets. Count; X ++)
{
Owb. worksheets (x). Select ();
VaR osheet = owb. activesheet;
\ Read Excel file data by specified start row 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 the read data row to the background and insert it to the database
Webserviceexcel. insert_from_excel (excelnum );
}
}
}
}
Catch (ERR)
{
Alert ("error," + err. Message );
}
}

This is only the key code of the front-end.
The problem is that if there is too much data in the Excel file, the import process will take a long time and the performance will be poor. I don't know how to improve it ??? If thousands of lines of data are exported, the time is unbearable. Please kindly advise me. Please try again. Thank you !!!
A simple example of using JavaScript and Excel. Application to read local Excel files and present them in tables Copy code The Code is 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 ){
// The objid is the table ID.
// Enable ActiveX in browser security 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 ("An error occurred while creating the Excel file. It may be that Microsoft Office Excel software is not correctly installed on your computer or your browser's security level settings are 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 (); // if you do not close the workbook, the consequences are 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>

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.