以下是我用于将数据导出到Excel的代码。
适用于较小的500的数据,但是当数据大于500时需要更多时间,如果我尝试导出1000个数据则会挂起。
function ExportToCsv(fileName, tableName) {
get();
var data = GetCellValues(tableName);
var csv = ConvertToCsv(data);
var data;
var a;
var excel = new ActiveXObject ("Excel.Application");
excel.visible = true;
var book = excel.Workbooks.Add ();
var sheet = excel.Worksheets(1);
data=csv.split(";");
for (var i=0,j=1; i<data.length-1;i++,j++)
{
cellValue = data[i].split("^");
for(var k=0,l=1;k<cellValue.length;k++,l++){
if(l==1){
a="A"+j;
}else if(l==2){
a="B"+j;
}else if(l==3){
a="C"+j;
}else if(l==4){
a="D"+j;
}else if(l==5){
a="E"+j;
}else if(l==6){
a="F"+j;
}else if(l==7){
a="G"+j;
}
sheet.Range(a) = cellValue[k];
}
}
}
function GetCellValues(tableName) {
var table = document.getElementById(tableName);
var tableArray = [];
for (var r = 0, n = table.rows.length; r < n; r++) {
tableArray[r] = [];
for (var c = 0, m = table.rows[r].cells.length; c < m; c++) {
var text = table.rows[r].cells[c].textContent || table.rows[r].cells[c].innerText;
tableArray[r][c] = text.trim();
}
}
return tableArray;
}
function ConvertToCsv(objArray) {
var array = typeof objArray != "object" ? JSON.parse(objArray) : objArray;
var str = "";
var line = "";
var index;
var line_next="";
var value;
for (var i = 0; i < array.length; i++) {
line = "";
var array1 = array[i];
for (index in array1) {
if (array1.hasOwnProperty(index)) {
value = array1[index] + "";
line +=value.replace(/"/g, "\"\"") + "^";
}
}
line = line.slice(0, -1);
str += line +";"+ "\r\n";
}
return str;
}
function get(){
var table = document.getElementById('table_0');
var numRows = table.rows.length;
var category = "";
var rows = "<tr><td>Document</td><td>Number</td><td>Title</td><td>val</td><td>test To</td><td>Prefix</td><td>suffix</td></tr>";
for(var i=0;i<numRows;i++){
var values = [];
var cells1 = table.rows[i].cells;
var checkBoxes = cells1[0].getElementsByTagName('input');
for(var counter=1; counter<checkBoxes.length; counter++){
if(checkBoxes[counter].type =='checkbox' && checkBoxes[counter].checked == true){
for(var ic=1,it=(cells1.length-1);ic<it;ic++){
if(cells1[0].innerHTML != null){
divs=cells1[ic].textContent;
values.push(divs);
}
}
rows = rows + "<tr><td>" + values[0].trim() + "</td><td>" + values[1].trim() + "</td><td>" + values[2].trim() + "</td><td>" + values[3].trim() + "</td><td>" + values[4].trim() + "</td><td>" + values[5].trim() + "</td><td>" + values[6].trim() + "</td></tr>";
}
}
$("#resultTable").html(rows);
//$(rows).appendTo("#resultTable");
$("#myDiv").hide();
}
return true;
}
尝试导出数据时,超过600个IE挂起。
是因为我使用的是activex吗?
我可以修改代码以使其适用于大数据吗?