java导出复杂excel表格_使用JavaScript将复杂表格导出为Excel

/**

* 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);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值