Когда-то давным давно, когда еще книжки были деревьями, довелось мне написать для различных нужд JavaScript, реализующий экспортирование табличного отчета с открытой HTML-страницы в Microsoft Office Excel (в ту пору еще 2003 версии). Работа данного скрипта была проверена даже в таком древнем создании, как Internet Explorer 6.0 и была оценена как успешная.
<SCRIPT language=javascript id=clientEventHandlersVBS>
<!--
function Unload_into_Excell() {
var ExcelAppl = new ActiveXObject("Excel.Application");
var ExcelSheet = new ActiveXObject("Excel.Sheet");
alert("Вам придется подождать преобразования отчета в Excell... Набритесь терпения...");
// Make Excel visible through the Application object.
ExcelSheet.Application.Visible = false;
function FindandDelete(AStr, Ai){
var StrofColsLeft;
var StrofColsRight;
var iii;
StrofColsLeft = AStr.substring(0,Ai);
StrofColsRight = AStr.substring(Ai+1,StrofCols.length);
iii = StrofColsRight.indexOf(',');
StrofColsRight = StrofColsRight.substring(iii,StrofColsRight.length);
StrofCols = StrofColsLeft + StrofColsRight
}
var dy = 1;
var maxCols = 0;
var curCols = 0;
var j = 0;
var it;
var StrofCols;
var cs;
var ss;
var ii;
var tb;
var kk = 0;
var ddj = 0;
var myH;
//first found maximum number of cols in all tables
for (it = 0; it < document.all.tags("TABLE").length; it++)
{
tb = document.all.tags("TABLE")[it];
curCols = 0;
j = 0;
do
{
curCols = curCols + tb.rows(0).cells(j).colSpan;
j = j + 1;
}
while ( j < tb.rows(0).cells.length);
if ( maxCols < curCols ) {
maxCols = curCols;
};
}
for (it = 0; it < document.all.length; it++)
{
if (("H1" == document.all[it].tagName) || ("H2" == document.all[it].tagName) || ("H3" == document.all[it].tagName) || ("H4" == document.all[it].tagName) || ("H5" == document.all[it].tagName))
{
//Main Caption
myH = document.all[it];
ExcelSheet.ActiveSheet.Range(ExcelSheet.ActiveSheet.Cells(dy,1),ExcelSheet.ActiveSheet.Cells(dy+2,maxCols)).MergeCells = true;
ExcelSheet.ActiveSheet.Cells(dy,1).value = myH.innerText ;
ExcelSheet.ActiveSheet.Cells(dy,1).VerticalAlignment = -4108;
ExcelSheet.ActiveSheet.Cells(dy,1).HorizontalAlignment = -4108;
ExcelSheet.ActiveSheet.Cells(dy,1).Font.Bold = true;
ExcelSheet.ActiveSheet.Cells(dy,1).Font.Size = 16;
dy = dy+2;
};
var wdt;
if ("TABLE" == document.all[it].tagName)
{
tb = document.all[it];
j = 0;
// Place some text in all cells of the sheet.
StrofCols = ',';
i = 0;
do
{
j = 0;
dj = 0;
do
{
cs = tb.rows(i).cells(j).colSpan;
rs = tb.rows(i).cells(j).rowSpan;
do
{
ss = ',' + dj + ',';
ii = StrofCols.indexOf(ss,0);
if ( ii > -1 )
{
FindandDelete(StrofCols, ii);
dj = dj + 1
};
} while ( ii > -1 );
ExcelSheet.ActiveSheet.Range(ExcelSheet.ActiveSheet.Cells(i+1+dy,dj+1),ExcelSheet.ActiveSheet.Cells(i+rs+dy,dj+cs)).MergeCells = true;
ExcelSheet.ActiveSheet.Cells(i+1+dy,dj+1).Value = tb.rows(i).cells(j).innerText;
ExcelSheet.ActiveSheet.Cells(i+1+dy,dj+1).VerticalAlignment = -4108;
ExcelSheet.ActiveSheet.Cells(i+1+dy,dj+1).RowHeight = tb.rows(i).cells(j).clientHeight;
wdt = tb.rows(i).cells(j).clientWidth/cs
if ( wdt > 255)
{
wdt = 255
};
ExcelSheet.ActiveSheet.Cells(i+1+dy,dj+1).ColumnWidth = wdt;
//When rowspan more than 1 do it
for (k = 1; k < rs; k++)
{
ddj = dj;
for (kk = 1; kk <= cs; kk++)
{
StrofCols = StrofCols + ddj + ',';
ddj = ddj + 1;
}
}
dj = dj + cs;
j = j+1;
}
while ( j < tb.rows(i).cells.length);
do
{
ss = ',' + dj + ',';
ii = StrofCols.indexOf(ss,0);
if ( ii > -1 )
{
FindandDelete(StrofCols, ii);
};
dj = dj + 1
} while ( dj < maxCols );
i = i+1;
}
while (i < tb.rows.length);
ExcelSheet.ActiveSheet.Range(ExcelSheet.ActiveSheet.Cells(dy+1,1),ExcelSheet.ActiveSheet.Cells(i+dy,maxCols)).Borders.weight = 2;
ExcelSheet.ActiveSheet.Range(ExcelSheet.ActiveSheet.Cells(dy+1,1),ExcelSheet.ActiveSheet.Cells(i+dy,maxCols)).Borders.LineStyle = 1;
ExcelSheet.ActiveSheet.Range(ExcelSheet.ActiveSheet.Cells(dy+1,1),ExcelSheet.ActiveSheet.Cells(i+dy,maxCols)).WrapText = true;
ExcelSheet.ActiveSheet.Range(ExcelSheet.ActiveSheet.Cells(dy+1,1),ExcelSheet.ActiveSheet.Cells(i+dy,maxCols)).Columns.AutoFit;
dy = dy + i + 1;
};
};
ExcelSheet.Application.Visible = true;
}
-->
</SCRIPT>
<!--
function Unload_into_Excell() {
var ExcelAppl = new ActiveXObject("Excel.Application");
var ExcelSheet = new ActiveXObject("Excel.Sheet");
alert("Вам придется подождать преобразования отчета в Excell... Набритесь терпения...");
// Make Excel visible through the Application object.
ExcelSheet.Application.Visible = false;
function FindandDelete(AStr, Ai){
var StrofColsLeft;
var StrofColsRight;
var iii;
StrofColsLeft = AStr.substring(0,Ai);
StrofColsRight = AStr.substring(Ai+1,StrofCols.length);
iii = StrofColsRight.indexOf(',');
StrofColsRight = StrofColsRight.substring(iii,StrofColsRight.length);
StrofCols = StrofColsLeft + StrofColsRight
}
var dy = 1;
var maxCols = 0;
var curCols = 0;
var j = 0;
var it;
var StrofCols;
var cs;
var ss;
var ii;
var tb;
var kk = 0;
var ddj = 0;
var myH;
//first found maximum number of cols in all tables
for (it = 0; it < document.all.tags("TABLE").length; it++)
{
tb = document.all.tags("TABLE")[it];
curCols = 0;
j = 0;
do
{
curCols = curCols + tb.rows(0).cells(j).colSpan;
j = j + 1;
}
while ( j < tb.rows(0).cells.length);
if ( maxCols < curCols ) {
maxCols = curCols;
};
}
for (it = 0; it < document.all.length; it++)
{
if (("H1" == document.all[it].tagName) || ("H2" == document.all[it].tagName) || ("H3" == document.all[it].tagName) || ("H4" == document.all[it].tagName) || ("H5" == document.all[it].tagName))
{
//Main Caption
myH = document.all[it];
ExcelSheet.ActiveSheet.Range(ExcelSheet.ActiveSheet.Cells(dy,1),ExcelSheet.ActiveSheet.Cells(dy+2,maxCols)).MergeCells = true;
ExcelSheet.ActiveSheet.Cells(dy,1).value = myH.innerText ;
ExcelSheet.ActiveSheet.Cells(dy,1).VerticalAlignment = -4108;
ExcelSheet.ActiveSheet.Cells(dy,1).HorizontalAlignment = -4108;
ExcelSheet.ActiveSheet.Cells(dy,1).Font.Bold = true;
ExcelSheet.ActiveSheet.Cells(dy,1).Font.Size = 16;
dy = dy+2;
};
var wdt;
if ("TABLE" == document.all[it].tagName)
{
tb = document.all[it];
j = 0;
// Place some text in all cells of the sheet.
StrofCols = ',';
i = 0;
do
{
j = 0;
dj = 0;
do
{
cs = tb.rows(i).cells(j).colSpan;
rs = tb.rows(i).cells(j).rowSpan;
do
{
ss = ',' + dj + ',';
ii = StrofCols.indexOf(ss,0);
if ( ii > -1 )
{
FindandDelete(StrofCols, ii);
dj = dj + 1
};
} while ( ii > -1 );
ExcelSheet.ActiveSheet.Range(ExcelSheet.ActiveSheet.Cells(i+1+dy,dj+1),ExcelSheet.ActiveSheet.Cells(i+rs+dy,dj+cs)).MergeCells = true;
ExcelSheet.ActiveSheet.Cells(i+1+dy,dj+1).Value = tb.rows(i).cells(j).innerText;
ExcelSheet.ActiveSheet.Cells(i+1+dy,dj+1).VerticalAlignment = -4108;
ExcelSheet.ActiveSheet.Cells(i+1+dy,dj+1).RowHeight = tb.rows(i).cells(j).clientHeight;
wdt = tb.rows(i).cells(j).clientWidth/cs
if ( wdt > 255)
{
wdt = 255
};
ExcelSheet.ActiveSheet.Cells(i+1+dy,dj+1).ColumnWidth = wdt;
//When rowspan more than 1 do it
for (k = 1; k < rs; k++)
{
ddj = dj;
for (kk = 1; kk <= cs; kk++)
{
StrofCols = StrofCols + ddj + ',';
ddj = ddj + 1;
}
}
dj = dj + cs;
j = j+1;
}
while ( j < tb.rows(i).cells.length);
do
{
ss = ',' + dj + ',';
ii = StrofCols.indexOf(ss,0);
if ( ii > -1 )
{
FindandDelete(StrofCols, ii);
};
dj = dj + 1
} while ( dj < maxCols );
i = i+1;
}
while (i < tb.rows.length);
ExcelSheet.ActiveSheet.Range(ExcelSheet.ActiveSheet.Cells(dy+1,1),ExcelSheet.ActiveSheet.Cells(i+dy,maxCols)).Borders.weight = 2;
ExcelSheet.ActiveSheet.Range(ExcelSheet.ActiveSheet.Cells(dy+1,1),ExcelSheet.ActiveSheet.Cells(i+dy,maxCols)).Borders.LineStyle = 1;
ExcelSheet.ActiveSheet.Range(ExcelSheet.ActiveSheet.Cells(dy+1,1),ExcelSheet.ActiveSheet.Cells(i+dy,maxCols)).WrapText = true;
ExcelSheet.ActiveSheet.Range(ExcelSheet.ActiveSheet.Cells(dy+1,1),ExcelSheet.ActiveSheet.Cells(i+dy,maxCols)).Columns.AutoFit;
dy = dy + i + 1;
};
};
ExcelSheet.Application.Visible = true;
}
-->
</SCRIPT>
Как его можно использовать? Например, создайте кнопку и "повешайте" на неё запуск функции Unload_into_Excell(). Не забудьте добавить адрес страницы со скриптом в надежные узлы в Internet Explorer. Раньше это делалось так:
Сервис -> Свойства обозревателя -> Надёжные узлы.
Хороших Вам проектов!
Comments
function generateexcel(tableid)
{
var table= document.getEle mentById(tablei d);
var html = table.outerHTML ;
window.open('da ta:application/ vnd.ms-excel,' + encodeURIComponent(html));
}
RSS feed for comments to this post