/**
* Utilities for exporting a table as an excel file
* @author Daniel.Sun(山风小子)
* @version 0.6*/varidTmr="";functionCleanup() {
window.clearInterval(idTmr);
CollectGarbage();
}functionexportAsXls(table) {functionImpactedCell(row, col, offset) {this.row=row;this.col=col;this.offset=offset;
}functionCurrentCell(row, col, text, colspan, rowspan) {this.row=row;this.col=col;this.text=text;this.colspan=colspan;this.rowspan=rowspan;this.getRow=functiongetRow() {returnthis.row;
}this.setRow=functionsetRow(row) {this.row=row;
}this.getCol=functiongetCol() {returnthis.col;
}this.setCol=functionsetCol(col) {this.col=col;
}this.setColspan=functionsetColspan(colspan) {this.colspan=colspan;
}this.getColspan=functiongetColspan() {returnthis.colspan;
}this.setRowspan=functionsetRowspan(rowspan) {this.rowspan=rowspan;
}this.getRowspan=functiongetRowspan() {returnthis.rowspan;
}
}functionCellManager(originalRow, colOffset, impactedCells, currentCell) {this.originalRow=originalRow;this.colOffset=colOffset;this.impactedCells=impactedCells;this.currentCell=currentCell;this.setCurrentCell=functionsetCurrentCell(currentCell) {this.currentCell=currentCell;
}this.setOriginalRow=functionsetOriginalRow(originalRow) {this.originalRow=originalRow;
}this.getCorrectedCol=functiongetCorrectedCol() {returnthis.currentCell.getCol()+this.colOffset;
}this.setColOffset=functionsetColOffset(colOffset) {this.colOffset=colOffset;
}this.getColOffset=functiongetColOffset() {returnthis.colOffset;
}this.initColOffset=functioninitColOffset() {if(this.currentCell.getRow()!=this.originalRow) {this.colOffset=0;
}
}this.getImpactedCells=functiongetImpactedCells() {returnthis.impactedCells;
}this.addImpactedCell=functionaddImpactedCell(impactedCell) {this.impactedCells.push(impactedCell);
}this.addImpactedCells=functionaddImpactedCells() {varcurrentCell=this.currentCell;for(vari=1; i
offset=currentCell.getColspan();
}else{
offset=1;
}this.addImpactedCell(newImpactedCell(impactedRow, impactedCol, offset))
}
}this.calcOffset=functioncalcOffset(row) {varcolOffset=this.colOffset;varresult=colOffset;for(vari=0; i
colOffset+=impactedCell.offset;
result=colOffset;break;
}
}this.colOffset=result;returnresult;
}this.correctColOffset=functioncorrectColOffset() {varcurrentCell=this.currentCell;vartmpColOffset;while(true) {this.calcOffset(currentCell.getRow());
tmpColOffset=this.getColOffset();this.calcOffset(currentCell.getRow());if(this.getColOffset()==tmpColOffset) {break;
}
}
}this.mergeCells=functionmergeCells(oSheet, row1, col1, row2, col2) {
oSheet.Range(oSheet.Cells(row1, col1), oSheet.Cells(row2, col2)).MergeCells=true;
}this.mergeCellsConditionally=functionmergeCellsConditionally(oSheet) {varcurrentCell=this.currentCell;varcolsShouldMerge=currentCell.getColspan()>1;varrowsShouldMerge=currentCell.getRowspan()>1;if(colsShouldMerge&&!rowsShouldMerge) {this.mergeCells(
oSheet,
currentCell.getRow(),this.getCorrectedCol(),
currentCell.getRow(),this.getCorrectedCol()+currentCell.getColspan()-1);
}elseif(!colsShouldMerge&&rowsShouldMerge) {this.mergeCells(
oSheet,
currentCell.getRow(),this.getCorrectedCol(),
currentCell.getRow()+currentCell.getRowspan()-1,this.getCorrectedCol()
);
}elseif(colsShouldMerge&&rowsShouldMerge) {this.mergeCells(
oSheet,
currentCell.getRow(),this.getCorrectedCol(),
currentCell.getRow()+currentCell.getRowspan()-1,this.getCorrectedCol()+currentCell.getColspan()-1);
}
}
}varoXL=newActiveXObject("Excel.Application");varoWB=oXL.Workbooks.Add();varoSheet=oWB.ActiveSheet;varcellManager=newCellManager(0,0,newArray());
traverseTable(
table,function(i, j, cell) {vartext=cell.innerText;if(null!=text) {varrow=i+1;varcol=j+1;varcurrentCell=newCurrentCell(row, col, text);
cellManager.setCurrentCell(currentCell);
cellManager.initColOffset();
cellManager.setOriginalRow(row);varcolspan=cell.getAttribute("colspan");varrowspan=cell.getAttribute("rowspan");
currentCell.setColspan(colspan);
currentCell.setRowspan(rowspan);varcolsShouldMerge=currentCell.getColspan()>1;varrowsShouldMerge=currentCell.getRowspan()>1;
cellManager.correctColOffset();if(rowsShouldMerge) {
cellManager.addImpactedCells();
}
cellManager.mergeCellsConditionally(oSheet);varcellInSheet=oSheet.Cells(currentCell.getRow(), cellManager.getCorrectedCol());if(colsShouldMerge) {varalign=cell.getAttribute("align");if("center"==align) {
cellInSheet.HorizontalAlignment=3;
}
cellManager.setColOffset(cellManager.getColOffset()+(colspan-1));
}
cellInSheet.NumberFormatLocal="@";
cellInSheet.value=text;
}
}
)
oXL.Visible=true;
oXL.UserControl=true;
oXL=null;
idTmr=window.setInterval("Cleanup();",1);
}functiontraverseTable(table, action) {for(vari=0; i
action(i, j, cell);
}
}
}