Transfer product is slightly net: http://www.pinlue.com/article/2020/03/0418/339980811018.html
Javascript implementation of the contents of page table into several methods excel in
A direct copy of the entire form to EXCEL
Second, by traversing the table, to the corresponding cell EXCEL assignment.
Third, the contents of the table is extracted, using the Save As .csv IE format.
The benefits of each method:
1. direct copy form, to retain the original format of the table, for example, columns, rows combined, alignment, background and the like,
2. By traversing table, more flexible, can traverse the table of contents part of certain needs.
3. Use the Save As IE, do not create ActiveXObject object, you can deal with mergers of form.
Disadvantages of each method:
1. script error may pop up: Automation can not create object.
Solution: Enable IE security settings: not marked as safe ActiveX controls Initialize and script.
Since copying the entire table to EXCEL, the Giga titles to the table, and added to the EXCEL you may run into trouble.
Solution: First, join the first row in the table
<tr><td colspan="x" align="center">&</td></tr>
X, number of columns to the entire table, after copying complete a form, add the following code, oSheet is currently active sheet.
oSheet.Range (oSheet.Cells (1, 1), oSheet.Cells (1, x)) value = "table heading"; // Set Title
oSheet.Rows (1) .Font.Size = 16; // the text size,
oSheet.Rows (1) .Font.Name = "Times New Roman"; // set text font
Note: I did not use the following properties may be useful, it may be error
. OSheet.Range (oSheet.Cells (1,1), oSheet.Cells (1,14)) mergecells = true; // merge cells
. OSheet.Range (oSheet.Cells (1,1), oSheet.Cells (1,14)) Interior.ColorIndex = 6; // set the background?
. OSheet.Range (oSheet.Cells (1,1), oSheet.Cells (1,14)) Font.ColorIndex = 5; // set the font color?
oSheet.Rows (1) .RowHeight = 20; // set the row height
oSheet.Cells (iRow, iCol) .Halignment = '2' // centrally disposed Font
2. The error may pop up script: Automation can not create object (above solution).
Table of contents written to EXCEL no form lines (unresolved)
And there will be problems when merged cell unit, the solution: merge cells and then write data.
. OSheet.Range (oSheet.Cells (1,1), oSheet.Cells (1,14)) mergecells = true; // merge cells
3. The table of contents is written into the free form lines EXCEL (unresolved)
When the form of complex format, there will be problems, (rowspan> 1 or colspan> 1), solution: header format are generally more complex, you can write to die first header, and then recycled to write other data.
Web content can be copied directly to the following :( Notepad, and then save the html format, with open IEs, please share a problem: [email protected])
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<Title> WEB page Export to EXCEL documents Methods </ title>
</head>
<body>
<table id="tableExcel" width="100%" border="1" cellspacing="0" cellpadding="0">
<tr>
<Td colspan = "5" align = "center"> WEB page exported to EXCEL documents Methods </ td>
</tr>
<tr>
<Td> column headings 1 </ td>
<Td> column header 2 </ td>
<Td> column header 3 </ td>
<Td> column header 4 </ td>
<Td> column heading 5 </ td>
</tr>
<tr>
<td>aaa</td>
<td>bbb</td>
<td>ccc</td>
<td>ddd</td>
<td>eee</td>
</tr>
<tr>
<td>AAA</td>
<td>BBB</td>
<td>CCC</td>
<td>DDD</td>
<td>EEE</td>
</tr>
<tr>
<td>FFF</td>
<td>GGG</td>
<td>HHH</td>
<td>III</td>
<td>JJJ</td>
</tr>
</table>
<Input type = "button" οnclick = "javascript: method1 ( 'tableExcel');" value = "A first method is introduced into EXCEL">
<Input type = "button" οnclick = "javascript: method2 ( 'tableExcel');" value = "The second method is introduced into EXCEL">
<Input type = "button" οnclick = "javascript: getXlsFromTbl ( 'tableExcel', null);" value = "A third method of introducing into EXCEL">
<SCRIPT LANGUAGE="javascript">
function method1 (tableid) {// the entire table copied into EXCEL
var curTbl = document.getElementById (tableID);
var oXL = new ActiveXObject("Excel.Application");
// Create AX Object excel
var oWB = oXL.Workbooks.Add();
// Get workbook objects
var oSheet = oWB.ActiveSheet;
// Activates the current sheet
var sel = document.body.createTextRange();
sel.moveToElementText(curTbl);
// move the contents of the table in TextRange
sel.select();
// Select the content TextRange
sel.execCommand("Copy");
// copy the contents of TextRange
oSheet.Paste ();
// pasted into activities in EXCEL
oXL.Visible = true;
// set the Visible property excel
}
function method2 (tableid) // read table for each cell in the EXCEL
{
var curTbl = document.getElementById (tableID);
var oXL = new ActiveXObject("Excel.Application");
// Create AX Object excel
var oWB = oXL.Workbooks.Add();
// Get workbook objects
var oSheet = oWB.ActiveSheet;
// Activates the current sheet
var Lenr = curTbl.rows.length;
// Get the number of rows in the table
for (i = 0; i < Lenr; i++)
{
var Lenc = curTbl.rows(i).cells.length;
// Get the number of columns per row
for (j = 0; j < Lenc; j++)
{
oSheet.Cells(i + 1, j + 1).value = curTbl.rows(i).cells(j).innerText;
// assignment
}
}
oXL.Visible = true;
// set the Visible property excel
}
function getXlsFromTbl(inTblId, inWindow) {
try {
var allStr = "";
var curStr = "";
//alert("getXlsFromTbl");
if (inTblId != null && inTblId != "" && inTblId != "null") {
curStr = getTblData(inTblId, inWindow);
}
if (curStr != null) {
allStr += curStr;
}
else {
alert ( "You want to export table does not exist!");
return;
}
var fileName = getExcelFileName();
doFileExport(fileName, allStr);
}
catch(e) {
alert ( "Export abnormal occurrence:" + e.name + "->" + e.description + "!");
}
}
function getTblData(inTbl, inWindow) {
There are rows = 0;
//alert("getTblData is " + inWindow);
var tblDocument = document;
if (!!inWindow && inWindow != "") {
if (!document.all(inWindow)) {
return null;
}
else {
tblDocument = eval(inWindow).document;
}
}
var curTbl = tblDocument.getElementById(inTbl);
var outStr = "";
if (curTbl != null) {
for (var j = 0; j < curTbl.rows.length; j++) {
//alert("j is " + j);
for (var i = 0; i < curTbl.rows[j].cells.length; i++) {
//alert("i is " + i);
if (i == 0 && rows > 0) {
outStr += " \t";
rows -= 1;
}
outStr += curTbl.rows[j].cells[i].innerText + "\t";
if (curTbl.rows[j].cells[i].colSpan > 1) {
for (var k = 0; k < curTbl.rows[j].cells[i].colSpan - 1; k++) {
outStr += " \t";
}
}
if (i == 0) {
if (rows == 0 && curTbl.rows[j].cells[i].rowSpan > 1) {
rows = curTbl.rows[j].cells[i].rowSpan - 1;
}
}
}
outStr += "\r\n";
}
}
else {
outStr = null;
alert (inTbl + "does not exist!");
}
return outStr;
}
function getExcelFileName() {
var d = new Date();
var curYear = d.getYear();
var curMonth = "" + (d.getMonth() + 1);
var curDate = "" + d.getDate();
var curHour = "" + d.getHours();
var curMinute = "" + d.getMinutes();
var curSecond = "" + d.getSeconds();
if (curMonth.length == 1) {
curMonth = "0" + curMonth;
}
if (curDate.length == 1) {
curDate = "0" + curDate;
}
if (curHour.length == 1) {
curHour = "0" + curHour;
}
if (curMinute.length == 1) {
curMinute = "0" + curMinute;
}
if (curSecond.length == 1) {
curSecond = "0" + curSecond;
}
var fileName = "leo_zhang" + "_" + curYear + curMonth + curDate + "_"
+ curHour + curMinute + curSecond + ".csv";
//alert(fileName);
return fileName;
}
function doFileExport(inName, inStr) {
was xlsWin = null;
if (!!document.all("glbHideFrm")) {
xlsWin = glbHideFrm;
}
else {
var width = 6;
var height = 4;
var openPara = "left=" + (window.screen.width / 2 - width / 2)
+ ",top=" + (window.screen.height / 2 - height / 2)
+ ",scrollbars=no,width=" + width + ",height=" + height;
xlsWin = window.open("", "_blank", openPara);
}
xlsWin.document.write(inStr);
xlsWin.document.close();
xlsWin.document.execCommand(‘Saveas‘, true, inName);
xlsWin.close();
}
</SCRIPT>
</body>
</html>