Subject: Reading from Excel Files
Posted By: interrupt Post Date: 10/2/2004 6:35:15 AM
Hi Guys,

Can Javascript read data from an excel file and then outpuyt the data to a web page?

cheers

Reply By: joefawcett Reply Date: 10/2/2004 6:42:45 AM
Yes easily, do you want to run the code server-side or client-side?
If server-side then Excel will need to be installed on the server and all users will need a licence. If on the client the users will need Excel installed and your site will need to be in the "Local intranet" or "Trusted sites" zone.

--

Joe
Reply By: interrupt Reply Date: 10/2/2004 6:54:52 AM
Hi Joe,

The code will be run client side, we are covered by a site license so no worries there and my site is in the trusted zone.

Thanks
interrupt

Reply By: joefawcett Reply Date: 10/2/2004 7:37:53 AM
Where is the Excel file, on the server or the client?

--

Joe

Reply By: interrupt Reply Date: 10/2/2004 8:47:41 AM
Excel file will be on the server.

cheers

Reply By: joefawcett Reply Date: 10/2/2004 9:24:47 AM
That's the most awkward combination because as far as I know even the latest version cannot open a file by URL, you would need to copy the file to the client first then manipulate it.
First a function to copy files from server to client:

function saveRemoteFile(From, To)
{
  var oXmlHttp = new ActiveXObject("Msxml2.XmlHttp.4.0");  //Change to version 3 if necessary
  oXmlHttp.open("GET", From, false);
  oXmlHttp.send();
  if (oXmlHttp.status == 200)
  {
    var oStream = new ActiveXObject("Adodb.Stream");
    oStream.type = 1; //Binary
    oStream.open();
    oStream.write(oXmlHttp.responseBody);
    oStream.saveToFile(To, 1 && 2); //Create if needed and overwrite if necessary
    oStream.close();
  }
  else
  {
    throw new Error(oXmlHttp.statusText);  //Needs improving
  }
}


Now when web page needs Excel file you need to call
saveRemoteFile("http://myserver/myfolder/myfile.xls", "C:\\myfile.xls");
.
Now to work with file, let's display the value in cell A1 on first sheet in a div with the id of divOutput.

var oExcel = new ActiveXObject("Excel.Application");
var oWB = oExcel.workbooks.open("C:\\myfile.xls");
//oExcel.visible = true;
var oWS = oWB.worksheets(1);
document.getElementById("divOutput").innerText = oWS.Range("A1").value;
oExcel.quit();


--

Joe
Reply By: interrupt Reply Date: 10/2/2004 10:03:56 AM
How the hell do you do that???? lol! Thats just the ticket! Thanks Joe.

interrupt

Reply By: kamrag Reply Date: 9/28/2006 9:31:02 AM
I am having a problem when reading xls file. I dont want to upload the file on the server. I just want to read the file which the user is going to post.
Note: This file in not on the server and i want the file format as .xls (not .csv)
Using Request.BinaryRead(whatever may be the size of file) it gives me junk characters.

Please Help
Raghav

Reply By: kamrag Reply Date: 9/28/2006 9:31:35 AM
More details about the problem
I have a web site in which any user(client) can upload the excel file.
The user is going to select file from his machine.
When the user clicks on upload the page(asp page) is posted back.
By using request.binaryread i will get all the contents of the page(asp) including the excel file data contents.
But i am not able to convert it in to readable format.

My main purpose is that i want the Excel data in the recordset.
I have the code to create the recordset when the file resides on the server.
And to do that i will have to upload that file on the server and then read it.
And after reading the file delete the file from the server. But that is not the solution.

By using request.binaryread i am able to read the file by converting it to string. But the same is not working for excel file.

Raghav


Go to topic 50334

Return to index page 162
Return to index page 161
Return to index page 160
Return to index page 159
Return to index page 158
Return to index page 157
Return to index page 156
Return to index page 155
Return to index page 154
Return to index page 153