Renato, no worries mate. It's been some interesting learning for me..
I've re-written the source to make it neater, clearer and more functional.
I've made a new function that reads in a specified excel file, copying the data in the desired cells into a html table. The cell colours are copied from the spreadsheet.
You may reference the same file multiple times, you may reference several files.
Each call to the function returns a new table.
The code is still fairly generalized - you may want to alter the logic if you are reading from several different sections of the same file - at the moment, each call to the function
- starts excel
- opens the specified file
- reads the data
- quits excel
It does seem rather slow - I suspect that it's just the interaction between javascript and the COM object - you could try to read a large section of data from 1 file to gague the effect of (a) regular operations (b) initializing/quiting excel.
Hope it's useful! :)
Code:
<html>
<head>
<title>Get data from excel sheet(s)</title>
<script language="javascript" >
var excelApp=null, excelFile=null, excelSheet=null;
function initExcel(filename)
{
excelApp = new ActiveXObject("Excel.Application");
excelFile = excelApp.Workbooks.Open(filename);
excelSheet = excelApp.Worksheets('Sheet1');
}
function myShutdownExcel()
{
excelApp.Quit();
excelApp=null;
excelFile=null;
excelSheet=null;
}
function myGetData(column, row)
{
return excelSheet.Cells(column, row).Value;
}
function myGetCellColor(column, row)
{
return excelSheet.Cells(column, row).Interior.Color;
}
function byId(e) {return document.getElementById(e);}
function excelToHtmlColor(decNum)
{
var hexStrVal, r, g, b
hexStrVal = decNum.toString(16);
while (hexStrVal.length < 6)
{
hexStrVal = "0" + hexStrVal;
}
r = hexStrVal.substr(4,2);
g = hexStrVal.substr(2,2);
b = hexStrVal.substr(0,2);
return r+g+b;
}
function createTableFromExcel(excelFilename, tableId, firstCol,firstRow, lastCol,lastRow)
{
var table, tbody, tRow, tCell;
var curRow, curCol;
initExcel(excelFilename);
table = document.createElement('table');
table.setAttribute('id', tableId);
tbody = document.createElement('tbody');
table.appendChild(tbody);
for (curRow=firstRow; curRow<=lastRow; curRow++)
{
tRow = document.createElement('tr');
for (curCol=firstCol; curCol<=lastCol; curCol++)
{
tCell = document.createElement('td');
curVal = myGetData(curRow, curCol);
if (curVal==undefined)
curVal = " ";
curColor = myGetCellColor(curRow, curCol);
curColorStr = "#" + excelToHtmlColor(curColor);
tCell.style.backgroundColor = curColorStr;
cellText = document.createTextNode(curVal);
tCell.appendChild(cellText);
tRow.appendChild(tCell);
}
tbody.appendChild(tRow);
}
myShutdownExcel();
return table;
}
function mOnLoadFunc()
{
var filename1 = "C:\\Users\\enhzflep\\Documents\\Book2.xlsm";
var parent = byId('tableHolder');
var child = createTableFromExcel(filename1, 'table1', 1,1, 7,11);
parent.appendChild(child);
parent.appendChild(document.createElement('br'));
child = createTableFromExcel(filename1, 'table2', 1,7, 3,11);
parent.appendChild(child);
parent.appendChild(document.createElement('br'));
child = createTableFromExcel(filename1, 'table3', 4,7, 4,11);
parent.appendChild(child);
parent.appendChild(document.createElement('br'));
child = createTableFromExcel(filename1, 'table4', 5,7, 7,11);
parent.appendChild(child);
}
</script>
<style>
table
{
border: solid 1px #555;
}
td
{
width: 32px;
border: solid 1px #aaa;
height: 16px;
}
</style>
</head>
<body onload="mOnLoadFunc();">
<b>Get data from excel sheets</b>
<div id="tableHolder"></div>
</body>
</html>
Result:
| B | C | D | E | F | G |
2 | b | c | d | e | f | g |
3 | 9 | 10 | 11 | 12 | 13 | 14 |
4 | i | j | k | l | m | n |
5 | 16 | 17 | 18 | 19 | 20 | 21 |
6 | | | | | | |
7 | B | C | D | E | F | G |
8 | 13 | 12 | 11 | 10 | 9 | 8 |
9 | I | J | K | L | M | N |
10 | 6 | 5 | 4 | 3 | 2 | 1 |
11 | P | Q | R | S | T | U |