首页 > 编程 > HTML > 正文

ASP.NET的HTMLTable原样导出到Excel

2020-03-24 15:47:11
字体:
来源:转载
供稿:网友

  function PrintTableToExcelEx(objTab)

  ...{

  try

  ...{

  var xls = new ActiveXObject( "Excel.Application" );

  }

  catch(e)

  ...{

  alert( "您必须安装Excel电子表格软件,同时浏览器须使用“ActiveX 控件”,您的浏览器须允许执行控件。 请点击【帮助】了解浏览器设置方法!");

  return false;

  }

  xls.visible = true;

  var xlBook = xls.Workbooks.Add;

  var xlsheet = xlBook.Worksheets(1);

  var CurX = 1;

  var CurY = 1;

  for (var i = 0; i < objTab.rows.length; i++)

  ...{

  for (var j = 0; j < objTab.rows[i].cells.length; j++)

  ...{

  var cell = objTab.rows[i].cells[j];

  var v = cell.outerText;

  if ( cell.children != null && cell.children.length != 0 )

  ...{

  v = "";

  for ( var k = 0; k < cell.children.length; k++ )

  ...{

  var cc = cell.children[k];

  switch (cc.type)

  ...{

  case "select-one":

  for ( var l = 0; l < cc.options.length; l++ )

  if ( cc.options[l].selected )  v += cc.options[l].text;

  break;

  case "text":

  v += cc.html' target='_blank'>value;

  break;

  case "checkbox":

  v += cc.checked ? "是" : "否";

  break;

  case undefined:

  v += cell.outerText;

  break;

  case "hidden":

  break;

  default:

  //v += cc.id + "/";

  //v += cc.tagname + "/";

  //v += cc.type + "/"

  v += cc.value;

  break;

  }

  }

  }

  while ( xlsheet.Cells(CurX,CurY).MergeCells )    CurY++;

  xlsheet.Cells(CurX, CurY).HorizontalAlignment = 2;

  var align = cell.getAttribute("align");

  switch ( align )

  ...{

  case "left":

  xlsheet.Cells(CurX, CurY).HorizontalAlignment = 2;//XlHalign.xlHalignLeft;

  break;

  case "center":

  xlsheet.Cells(CurX, CurY).HorizontalAlignment = 3;;//XlHalign.xlHalignCenter;

break;

case "right":

xlsheet.Cells(CurX, CurY).HorizontalAlignment = 4;//XlHalign.xlHalignRight;

break;

}

xlsheet.Cells(CurX, CurY).WrapText = true;

xlsheet.Cells(CurX, CurY).VerticalAlignment = 2;

xlsheet.Cells(CurX, CurY).Value = v;

xlsheet.Cells(CurX, CurY).Borders.LineStyle = 1;



var rowSpan = cell.getAttribute("RowSpan");

var colSpan = cell.getAttribute("ColSpan");

if ( rowSpan >= 2 || colSpan >= 2 )

...{

var R = xls.Range(xls.Cells(CurX, CurY), xls.Cells(CurX + rowSpan - 1, CurY + colSpan - 1));

R.MergeCells = true;

R.Borders.LineStyle = 1;

}

CurY++;            

}



CurX++;

郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表