|
|
Line 1: |
Line 1: |
- | | + | <html> |
- | <html> | + | |
| <head> | | <head> |
- | <title>Read from Excel or Access</title> | + | <title> |
- | <script language="JavaScript"> | + | Style Get data from excel sheet |
- | function getCount() | + | </title> |
- | { | + | <script language="javascript" > |
- | | + | function GetData(cell,row){ |
- | var fso = new ActiveXObject("Scripting.FileSystemObject"); | + | var excel = new ActiveXObject("Excel.Application"); |
- | var s = fso.CreateTextFile("C:\\ASPConnection\\test.txt", true); | + | var excel_file = excel.Workbooks.Open("C:\\amitdata.xls"); |
- | var t = fso.CreateTextFile("C:\\ASPConnection\\test1.txt", true); | + | var excel_sheet = excel.Worksheets("Sheet1"); |
- | var r = fso.CreateTextFile("C:\\ASPConnection\\test2.txt", true); | + | var data = excel_sheet.Cells(cell,row).Value; |
- | var cn = new ActiveXObject("ADODB.Connection");
| + | document.getElementById('div1').innerText =data; |
- | var strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = C:\\ASPConnection\\Barcodes_VOCS_2011.xls;Persist Security Info=False;Extended Properties=Excel 8.0;";
| + | } |
- | cn.Open(strConn);
| + | </script> |
- | var rs = new ActiveXObject("ADODB.Recordset");
| + | |
- | var SQL = "select * from [Sheet7$]";
| + | |
- | rs.Open(SQL, cn);
| + | |
- | if(rs.bof)
| + | |
- | {
| + | |
- | document.write('No records available for this query'); | + | |
- | }
| + | |
- | if(!rs.bof)
| + | |
- | {
| + | |
- | rs.MoveFirst()
| + | |
- | while(!rs.eof)
| + | |
- | {
| + | |
- | var store = rs.fields(2).value;
| + | |
- | for(var i=0; i!= rs.fields.count; ++i)
| + | |
- | {
| + | |
- | document.write(rs.fields(i).value + ",");
| + | |
- | t.Write(rs.fields(i).value + ","); }
| + | |
- | s.WriteLine(rs.fields(2).value + ",");
| + | |
- | t.WriteLine(",");
| + | |
- | document.write("<br />");
| + | |
- | rs.MoveNext() }
| + | |
- | }
| + | |
- | rs.Close();
| + | |
- | cn.Close();
| + | |
- | s.Close();
| + | |
- | t.Close();
| + | |
- | r.Close();
| + | |
- | }
| + | |
- | | + | |
- | var ForReading = 1, ForWriting = 2, ForAppending = 8;
| + | |
- | var TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0;
| + | |
- | var fso = new ActiveXObject("Scripting.FileSystemObject");
| + | |
- | | + | |
- | // Open the files for input and output
| + | |
- | //CHANGE THESE LOCATIONS TO ENSURE THAT :
| + | |
- | // a) testfile1.txt exists and
| + | |
- | // b) testfile2.txt can be created and is writable
| + | |
- | var filename1 = "C:\\ASPConnection\\test.txt";
| + | |
- | var filename2 = "C:\\ASPConnection\\test2.txt";
| + | |
- | | + | |
- | var f1 = fso.OpenTextFile(filename1, ForReading, true);
| + | |
- | // Open the file for input.
| + | |
- | f2 = fso.OpenTextFile(filename2, ForWriting);
| + | |
- | | + | |
- | var r = f1.ReadLine(); //This line will read the data to the input file
| + | |
- | var g = r;
| + | |
- | // Read from the file and display the results.
| + | |
- | var r = f1.ReadLine(); //This line will read the data to the input file
| + | |
- | f2.WriteLine(r); //This line will write the data to the output file
| + | |
- | document.write(r + "<br />");
| + | |
- | // Read from the file and display the results.
| + | |
- | while (!f1.AtEndOfStream)
| + | |
- | {
| + | |
- | if(r!=g)
| + | |
- | {var r = f1.ReadLine(); //This line will read the data to the input file
| + | |
- | document.write (r + "<br />"); //This line will display the data on the screen
| + | |
- | f2.WriteLine(r); //This line will write the data to the output file
| + | |
- | var g = r;
| + | |
- | } | + | |
- | else
| + | |
- | {var r = f1.ReadLine(); //This line will read the data to the input file
| + | |
- | }
| + | |
- | /*var r = f1.ReadLine(); //This line will read the data to the input file
| + | |
- | document.write (r + "<br />"); //This line will display the data on the screen
| + | |
- | f2.WriteLine(r); //This line will write the data to the output file*/
| + | |
- | }
| + | |
- | f1.Close();
| + | |
- | f2.Close();
| + | |
- | </script> | + | |
| </head> | | </head> |
| <body> | | <body> |
- | <input type="button" value="Get count" onClick="getCount()"> | + | <p> </p> |
| + | <div style="background: #009955; width:'100%';" align="center"> |
| + | <font color="#000080" size="12pt"> |
| + | <b>Get data from excel sheets</b> |
| + | </font> |
| + | </div> |
| + | <center> |
| + | <p> </p> |
| + | <div id="div1" style="background: #DFDFFF; width:'100%';" align="center"> |
| + | Click buttons to fetch data from c:\amitdata.xls |
| + | </div> |
| + | <input type="button" value="cell(1),row(1)" onClick="GetData(1,1);" /> |
| + | <input type="button" value="cell(1),row(2)" onClick="GetData(1,2);" /> |
| + | <input type="button" value="cell(2),row(1)" onClick="GetData(2,1);" /> |
| + | <input type="button" value="cell(2),row(2)" onClick="GetData(2,2);" /> |
| + | </center> |
| </body> | | </body> |
| </html> | | </html> |
- |
| |
- | Reply With Quote
| |