导出到excel多个工作表/选项卡

时间:2014-11-07 13:16:35

标签: c# asp.net

我目前正在使用以下代码导出到excel,它运行正常:

Response.ClearContent()
Response.AppendHeader("content-disposition", "attachment; filename=Responses.xls");
Response.ContentType = "application/excel";

StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);

GridView1.RenderControl(htmlTextWriter);
Response.Write(stringWriter.ToString());
Response.End();

但是,我希望能够在同一个Excel文件中添加新的标签/表格。这该怎么做?它甚至可能吗?

1 个答案:

答案 0 :(得分:1)

是的,这是可能的。但你必须做一些逆向工程。首先,让我们创建一个演示文档。打开Excel,给自己一些表。在每张纸上添加一些文字。然后执行另存为并将其另存为单个文件网页(.mhtml)文件。然后关闭Excel,将扩展名从.MHTML重命名为.XLS。然后打开文件。你会收到一条警告信息。告诉它是的,可以打开。然后你会看到两张纸。从这一点开始,使用文本编辑器(如Notepad或Notepad ++)检查.XLS文件的内容。您可以看到它们如何为文件中的每个工作表嵌入数据。在服务器上生成HTML时,请使用与检查HTML文件时学到的格式相同的格式。以下是我的内容。

MIME-Version: 1.0
X-Document-Type: Workbook
Content-Type: multipart/related; boundary="----=_NextPart_01CFFA76.49BAAF50"

This document is a Single File Web Page, also known as a Web Archive file.  If you are seeing this message, your browser or editor doesn't support Web Archive files.  Please download a browser that supports Web Archive, such as Windows® Internet Explorer®.

------=_NextPart_01CFFA76.49BAAF50
Content-Location: file:///C:/D1745AB1/test1.htm
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html; charset="us-ascii"

<html xmlns:v=3D"urn:schemas-microsoft-com:vml"
xmlns:o=3D"urn:schemas-microsoft-com:office:office"
xmlns:x=3D"urn:schemas-microsoft-com:office:excel"
xmlns=3D"http://www.w3.org/TR/REC-html40">

<head>
<meta name=3D"Excel Workbook Frameset">
<meta http-equiv=3DContent-Type content=3D"text/html; charset=3Dus-ascii">
<meta name=3DProgId content=3DExcel.Sheet>
<meta name=3DGenerator content=3D"Microsoft Excel 14">
<link rel=3DFile-List href=3D"test1_files/filelist.xml">
<![if !supportTabStrip]>
<link id=3D"shLink" href=3D"test1_files/sheet001.htm">
<link id=3D"shLink" href=3D"test1_files/sheet002.htm">

<link id=3D"shLink">

<script language=3D"JavaScript">
<!--
 var c_lTabs=3D2;

 var c_rgszSh=3Dnew Array(c_lTabs);
 c_rgszSh[0] =3D "Book1";
 c_rgszSh[1] =3D "Sheet1";



 var c_rgszClr=3Dnew Array(8);
 c_rgszClr[0]=3D"window";
 c_rgszClr[1]=3D"buttonface";
 c_rgszClr[2]=3D"windowframe";
 c_rgszClr[3]=3D"windowtext";
 c_rgszClr[4]=3D"threedlightshadow";
 c_rgszClr[5]=3D"threedhighlight";
 c_rgszClr[6]=3D"threeddarkshadow";
 c_rgszClr[7]=3D"threedshadow";

 var g_iShCur;
 var g_rglTabX=3Dnew Array(c_lTabs);

function fnGetIEVer()
{
 var ua=3Dwindow.navigator.userAgent
 var msie=3Dua.indexOf("MSIE")
 if (msie>0 && window.navigator.platform=3D=3D"Win32")
  return parseInt(ua.substring(msie+5,ua.indexOf(".", msie)));
 else
  return 0;
}

function fnBuildFrameset()
{
 var szHTML=3D"<frameset rows=3D\"*,18\" border=3D0 width=3D0 frameborder=
=3Dno framespacing=3D0>"+
  "<frame src=3D\""+document.all.item("shLink")[1].href+"\" name=3D\"frShee=
t\" noresize>"+
  "<frameset cols=3D\"54,*\" border=3D0 width=3D0 frameborder=3Dno framespa=
cing=3D0>"+
  "<frame src=3D\"\" name=3D\"frScroll\" marginwidth=3D0 marginheight=3D0 s=
crolling=3Dno>"+
  "<frame src=3D\"\" name=3D\"frTabs\" marginwidth=3D0 marginheight=3D0 scr=
olling=3Dno>"+
  "</frameset></frameset><plaintext>";

 with (document) {
  open("text/html","replace");
  write(szHTML);
  close();
 }

 fnBuildTabStrip();
}

function fnBuildTabStrip()
{
 var szHTML=3D
  "<html><head><style>.clScroll {font:8pt Courier New;color:"+c_rgszClr[6]+=
";cursor:default;line-height:10pt;}"+
  ".clScroll2 {font:10pt Arial;color:"+c_rgszClr[6]+";cursor:default;line-h=
eight:11pt;}</style></head>"+
  "<body onclick=3D\"event.returnValue=3Dfalse;\" ondragstart=3D\"event.ret=
urnValue=3Dfalse;\" onselectstart=3D\"event.returnValue=3Dfalse;\" bgcolor=
=3D"+c_rgszClr[4]+" topmargin=3D0 leftmargin=3D0><table cellpadding=3D0 cel=
lspacing=3D0 width=3D100%>"+
  "<tr><td colspan=3D6 height=3D1 bgcolor=3D"+c_rgszClr[2]+"></td></tr>"+
  "<tr><td style=3D\"font:1pt\">&nbsp;<td>"+
  "<td valign=3Dtop id=3DtdScroll class=3D\"clScroll\" onclick=3D\"parent.f=
nFastScrollTabs(0);\" onmouseover=3D\"parent.fnMouseOverScroll(0);\" onmous=
eout=3D\"parent.fnMouseOutScroll(0);\"><a>&#171;</a></td>"+
  "<td valign=3Dtop id=3DtdScroll class=3D\"clScroll2\" onclick=3D\"parent.=
fnScrollTabs(0);\" ondblclick=3D\"parent.fnScrollTabs(0);\" onmouseover=3D\=
"parent.fnMouseOverScroll(1);\" onmouseout=3D\"parent.fnMouseOutScroll(1);\=
"><a>&lt</a></td>"+
  "<td valign=3Dtop id=3DtdScroll class=3D\"clScroll2\" onclick=3D\"parent.=
fnScrollTabs(1);\" ondblclick=3D\"parent.fnScrollTabs(1);\" onmouseover=3D\=
"parent.fnMouseOverScroll(2);\" onmouseout=3D\"parent.fnMouseOutScroll(2);\=
"><a>&gt</a></td>"+
  "<td valign=3Dtop id=3DtdScroll class=3D\"clScroll\" onclick=3D\"parent.f=
nFastScrollTabs(1);\" onmouseover=3D\"parent.fnMouseOverScroll(3);\" onmous=
eout=3D\"parent.fnMouseOutScroll(3);\"><a>&#187;</a></td>"+
  "<td style=3D\"font:1pt\">&nbsp;<td></tr></table></body></html>";

 with (frames['frScroll'].document) {
  open("text/html","replace");
  write(szHTML);
  close();
 }

 szHTML =3D
  "<html><head>"+
  "<style>A:link,A:visited,A:active {text-decoration:none;"+"color:"+c_rgsz=
Clr[3]+";}"+
  ".clTab {cursor:hand;background:"+c_rgszClr[1]+";font:9pt Arial;padding-l=
eft:3px;padding-right:3px;text-align:center;}"+
  ".clBorder {background:"+c_rgszClr[2]+";font:1pt;}"+
  "</style></head><body onload=3D\"parent.fnInit();\" onselectstart=3D\"eve=
nt.returnValue=3Dfalse;\" ondragstart=3D\"event.returnValue=3Dfalse;\" bgco=
lor=3D"+c_rgszClr[4]+
  " topmargin=3D0 leftmargin=3D0><table id=3DtbTabs cellpadding=3D0 cellspa=
cing=3D0>";

 var iCellCount=3D(c_lTabs+1)*2;

 var i;
 for (i=3D0;i<iCellCount;i+=3D2)
  szHTML+=3D"<col width=3D1><col>";

 var iRow;
 for (iRow=3D0;iRow<6;iRow++) {

  szHTML+=3D"<tr>";

  if (iRow=3D=3D5)
   szHTML+=3D"<td colspan=3D"+iCellCount+"></td>";
  else {
   if (iRow=3D=3D0) {
    for(i=3D0;i<iCellCount;i++)
     szHTML+=3D"<td height=3D1 class=3D\"clBorder\"></td>";
   } else if (iRow=3D=3D1) {
    for(i=3D0;i<c_lTabs;i++) {
     szHTML+=3D"<td height=3D1 nowrap class=3D\"clBorder\">&nbsp;</td>";
     szHTML+=3D
      "<td id=3DtdTab height=3D1 nowrap class=3D\"clTab\" onmouseover=3D\"p=
arent.fnMouseOverTab("+i+");\" onmouseout=3D\"parent.fnMouseOutTab("+i+");\=
">"+
      "<a href=3D\""+document.all.item("shLink")[i].href+"\" target=3D\"frS=
heet\" id=3DaTab>&nbsp;"+c_rgszSh[i]+"&nbsp;</a></td>";
    }
    szHTML+=3D"<td id=3DtdTab height=3D1 nowrap class=3D\"clBorder\"><a id=
=3DaTab>&nbsp;</a></td><td width=3D100%></td>";
   } else if (iRow=3D=3D2) {
    for (i=3D0;i<c_lTabs;i++)
     szHTML+=3D"<td height=3D1></td><td height=3D1 class=3D\"clBorder\"></t=
d>";
    szHTML+=3D"<td height=3D1></td><td height=3D1></td>";
   } else if (iRow=3D=3D3) {
    for (i=3D0;i<iCellCount;i++)
     szHTML+=3D"<td height=3D1></td>";
   } else if (iRow=3D=3D4) {
    for (i=3D0;i<c_lTabs;i++)
     szHTML+=3D"<td height=3D1 width=3D1></td><td height=3D1></td>";
    szHTML+=3D"<td height=3D1 width=3D1></td><td></td>";
   }
  }
  szHTML+=3D"</tr>";
 }

 szHTML+=3D"</table></body></html>";
 with (frames['frTabs'].document) {
  open("text/html","replace");
  charset=3Ddocument.charset;
  write(szHTML);
  close();
 }
}

function fnInit()
{
 g_rglTabX[0]=3D0;
 var i;
 for (i=3D1;i<=3Dc_lTabs;i++)
  with (frames['frTabs'].document.all.tbTabs.rows[1].cells[fnTabToCol(i-1)])
   g_rglTabX[i]=3DoffsetLeft+offsetWidth-6;
}

function fnTabToCol(iTab)
{
 return 2*iTab+1;
}

function fnNextTab(fDir)
{
 var iNextTab=3D-1;
 var i;

 with (frames['frTabs'].document.body) {
  if (fDir=3D=3D0) {
   if (scrollLeft>0) {
    for (i=3D0;i<c_lTabs&&g_rglTabX[i]<scrollLeft;i++);
    if (i<c_lTabs)
     iNextTab=3Di-1;
   }
  } else {
   if (g_rglTabX[c_lTabs]+6>offsetWidth+scrollLeft) {
    for (i=3D0;i<c_lTabs&&g_rglTabX[i]<=3DscrollLeft;i++);
    if (i<c_lTabs)
     iNextTab=3Di;
   }
  }
 }
 return iNextTab;
}

function fnScrollTabs(fDir)
{
 var iNextTab=3DfnNextTab(fDir);

 if (iNextTab>=3D0) {
  frames['frTabs'].scroll(g_rglTabX[iNextTab],0);
  return true;
 } else
  return false;
}

function fnFastScrollTabs(fDir)
{
 if (c_lTabs>16)
  frames['frTabs'].scroll(g_rglTabX[fDir?c_lTabs-1:0],0);
 else
  if (fnScrollTabs(fDir)>0) window.setTimeout("fnFastScrollTabs("+fDir+");"=
,5);
}

function fnSetTabProps(iTab,fActive)
{
 var iCol=3DfnTabToCol(iTab);
 var i;

 if (iTab>=3D0) {
  with (frames['frTabs'].document.all) {
   with (tbTabs) {
    for (i=3D0;i<=3D4;i++) {
     with (rows[i]) {
      if (i=3D=3D0)
       cells[iCol].style.background=3Dc_rgszClr[fActive?0:2];
      else if (i>0 && i<4) {
       if (fActive) {
        cells[iCol-1].style.background=3Dc_rgszClr[2];
        cells[iCol].style.background=3Dc_rgszClr[0];
        cells[iCol+1].style.background=3Dc_rgszClr[2];
       } else {
        if (i=3D=3D1) {
         cells[iCol-1].style.background=3Dc_rgszClr[2];
         cells[iCol].style.background=3Dc_rgszClr[1];
         cells[iCol+1].style.background=3Dc_rgszClr[2];
        } else {
         cells[iCol-1].style.background=3Dc_rgszClr[4];
         cells[iCol].style.background=3Dc_rgszClr[(i=3D=3D2)?2:4];
         cells[iCol+1].style.background=3Dc_rgszClr[4];
        }
       }
      } else
       cells[iCol].style.background=3Dc_rgszClr[fActive?2:4];
     }
    }
   }
   with (aTab[iTab].style) {
    cursor=3D(fActive?"default":"hand");
    color=3Dc_rgszClr[3];
   }
  }
 }
}

function fnMouseOverScroll(iCtl)
{
 frames['frScroll'].document.all.tdScroll[iCtl].style.color=3Dc_rgszClr[7];
}

function fnMouseOutScroll(iCtl)
{
 frames['frScroll'].document.all.tdScroll[iCtl].style.color=3Dc_rgszClr[6];
}

function fnMouseOverTab(iTab)
{
 if (iTab!=3Dg_iShCur) {
  var iCol=3DfnTabToCol(iTab);
  with (frames['frTabs'].document.all) {
   tdTab[iTab].style.background=3Dc_rgszClr[5];
  }
 }
}

function fnMouseOutTab(iTab)
{
 if (iTab>=3D0) {
  var elFrom=3Dframes['frTabs'].event.srcElement;
  var elTo=3Dframes['frTabs'].event.toElement;

  if ((!elTo) ||
   (elFrom.tagName=3D=3DelTo.tagName) ||
   (elTo.tagName=3D=3D"A" && elTo.parentElement!=3DelFrom) ||
   (elFrom.tagName=3D=3D"A" && elFrom.parentElement!=3DelTo)) {

   if (iTab!=3Dg_iShCur) {
    with (frames['frTabs'].document.all) {
     tdTab[iTab].style.background=3Dc_rgszClr[1];
    }
   }
  }
 }
}

function fnSetActiveSheet(iSh)
{
 if (iSh!=3Dg_iShCur) {
  fnSetTabProps(g_iShCur,false);
  fnSetTabProps(iSh,true);
  g_iShCur=3DiSh;
 }
}

 window.g_iIEVer=3DfnGetIEVer();
 if (window.g_iIEVer>=3D4)
  fnBuildFrameset();
//-->
</script>
<![endif]><!--[if gte mso 9]><xml>
 <x:ExcelWorkbook>
  <x:ExcelWorksheets>
   <x:ExcelWorksheet>
    <x:Name>Book1</x:Name>
    <x:WorksheetSource HRef=3D"test1_files/sheet001.htm"/>
   </x:ExcelWorksheet>
   <x:ExcelWorksheet>
    <x:Name>Sheet1</x:Name>
    <x:WorksheetSource HRef=3D"test1_files/sheet002.htm"/>
   </x:ExcelWorksheet>
  </x:ExcelWorksheets>
  <x:Stylesheet HRef=3D"test1_files/stylesheet.css"/>
  <x:WindowHeight>12585</x:WindowHeight>
  <x:WindowWidth>27795</x:WindowWidth>
  <x:WindowTopX>480</x:WindowTopX>
  <x:WindowTopY>120</x:WindowTopY>
  <x:ActiveSheet>1</x:ActiveSheet>
  <x:ProtectStructure>False</x:ProtectStructure>
  <x:ProtectWindows>False</x:ProtectWindows>
 </x:ExcelWorkbook>
</xml><![endif]-->
</head>

<frameset rows=3D"*,39" border=3D0 width=3D0 frameborder=3Dno framespacing=
=3D0>
 <frame src=3D"test1_files/sheet002.htm" name=3D"frSheet">
 <frame src=3D"test1_files/tabstrip.htm" name=3D"frTabs" marginwidth=3D0 ma=
rginheight=3D0>
 <noframes>
  <body>
   <p>This page uses frames, but your browser doesn't support them.</p>
  </body>
 </noframes>
</frameset>
</html>

------=_NextPart_01CFFA76.49BAAF50
Content-Location: file:///C:/D1745AB1/test1_files/stylesheet.css
Content-Transfer-Encoding: quoted-printable
Content-Type: text/css; charset="us-ascii"

tr
    {mso-height-source:auto;}
col
    {mso-width-source:auto;}
br
    {mso-data-placement:same-cell;}
.style0
    {mso-number-format:General;
    text-align:general;
    vertical-align:bottom;
    white-space:nowrap;
    mso-rotate:0;
    mso-background-source:auto;
    mso-pattern:auto;
    color:black;
    font-size:11.0pt;
    font-weight:400;
    font-style:normal;
    text-decoration:none;
    font-family:Calibri, sans-serif;
    mso-font-charset:0;
    border:none;
    mso-protection:locked visible;
    mso-style-name:Normal;
    mso-style-id:0;}
td
    {mso-style-parent:style0;
    padding-top:1px;
    padding-right:1px;
    padding-left:1px;
    mso-ignore:padding;
    color:black;
    font-size:11.0pt;
    font-weight:400;
    font-style:normal;
    text-decoration:none;
    font-family:Calibri, sans-serif;
    mso-font-charset:0;
    mso-number-format:General;
    text-align:general;
    vertical-align:bottom;
    border:none;
    mso-background-source:auto;
    mso-pattern:auto;
    mso-protection:locked visible;
    white-space:nowrap;
    mso-rotate:0;}
.xl65
    {mso-style-parent:style0;
    color:black;}

------=_NextPart_01CFFA76.49BAAF50
Content-Location: file:///C:/D1745AB1/test1_files/tabstrip.htm
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html; charset="us-ascii"

<html>
<head>
<meta http-equiv=3DContent-Type content=3D"text/html; charset=3Dus-ascii">
<meta name=3DProgId content=3DExcel.Sheet>
<meta name=3DGenerator content=3D"Microsoft Excel 14">
<link id=3DMain-File rel=3DMain-File href=3D"../test1.htm">
<![if IE]>
<base href=3D"file:///C:\D1745AB1\test1_files\tabstrip.htm"
id=3D"webarch_temp_base_tag">
<![endif]>
<script language=3D"JavaScript">
<!--
if (window.name!=3D"frTabs")
 window.location.replace(document.all.item("Main-File").href);
//-->
</script>
<style>
<!--
A {
    text-decoration:none;
    color:#000000;
    font-size:9pt;
}
-->
</style>
</head>
<body topmargin=3D0 leftmargin=3D0 bgcolor=3D"#808080">
<table border=3D0 cellspacing=3D1>
 <tr>
 <td bgcolor=3D"#FFFFFF" nowrap><b><small><small>&nbsp;<a href=3D"sheet001.=
htm" target=3D"frSheet"><font face=3D"Arial" color=3D"#000000">Book1</font>=
</a>&nbsp;</small></small></b></td>
 <td bgcolor=3D"#FFFFFF" nowrap><b><small><small>&nbsp;<a href=3D"sheet002.=
htm" target=3D"frSheet"><font face=3D"Arial" color=3D"#000000">Sheet1</font=
></a>&nbsp;</small></small></b></td>

 </tr>
</table>
</body>
</html>

------=_NextPart_01CFFA76.49BAAF50
Content-Location: file:///C:/D1745AB1/test1_files/sheet001.htm
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html; charset="us-ascii"

<html xmlns:v=3D"urn:schemas-microsoft-com:vml"
xmlns:o=3D"urn:schemas-microsoft-com:office:office"
xmlns:x=3D"urn:schemas-microsoft-com:office:excel"
xmlns=3D"http://www.w3.org/TR/REC-html40">

<head>
<meta http-equiv=3DContent-Type content=3D"text/html; charset=3Dus-ascii">
<meta name=3DProgId content=3DExcel.Sheet>
<meta name=3DGenerator content=3D"Microsoft Excel 14">
<link id=3DMain-File rel=3DMain-File href=3D"../test1.htm">
<link rel=3DFile-List href=3Dfilelist.xml>
<![if IE]>
<base href=3D"file:///C:\D1745AB1\test1_files\sheet001.htm"
id=3D"webarch_temp_base_tag">
<![endif]>
<link rel=3DStylesheet href=3Dstylesheet.css>
<style>
<!--table
    {mso-displayed-decimal-separator:"\.";
    mso-displayed-thousand-separator:"\,";}
@page
    {margin:1.0in .75in 1.0in .75in;
    mso-header-margin:.5in;
    mso-footer-margin:.5in;}
-->
</style>
<![if !supportTabStrip]><script language=3D"JavaScript">
<!--
function fnUpdateTabs()
 {
  if (parent.window.g_iIEVer>=3D4) {
   if (parent.document.readyState=3D=3D"complete"
    && parent.frames['frTabs'].document.readyState=3D=3D"complete")
   parent.fnSetActiveSheet(0);
  else
   window.setTimeout("fnUpdateTabs();",150);
 }
}

if (window.name!=3D"frSheet")
 window.location.replace("../test1.htm");
else
 fnUpdateTabs();
//-->
</script>
<![endif]>
</head>

<body link=3Dblue vlink=3Dpurple>
<!--The following information was generated by Microsoft Excel's Publish as=
 Web
Page wizard.--><!--If the same item is republished from Excel, all informat=
ion between the DIV
tags will be replaced.--><!-----------------------------><!--START OF OUTPU=
T FROM EXCEL PUBLISH AS WEB PAGE WIZARD --><!----------------------------->

<table border=3D0 cellpadding=3D0 cellspacing=3D0 width=3D31 style=3D'borde=
r-collapse:
 collapse;table-layout:fixed;width:23pt'>
 <col width=3D31 style=3D'mso-width-source:userset;mso-width-alt:1133;width=
:23pt'>
 <tr height=3D20 style=3D'mso-height-source:userset;height:15.0pt'>
  <td height=3D20 class=3Dxl65 width=3D31 style=3D'height:15.0pt;width:23pt=
'>test</td>
 </tr>
 <tr height=3D20 style=3D'mso-height-source:userset;height:15.0pt'>
  <td height=3D20 class=3Dxl65 style=3D'height:15.0pt'></td>
 </tr>
 <![if supportMisalignedColumns]>
 <tr height=3D0 style=3D'display:none'>
  <td width=3D31 style=3D'width:23pt'></td>
 </tr>
 <![endif]>
</table>

</body>

</html>

------=_NextPart_01CFFA76.49BAAF50
Content-Location: file:///C:/D1745AB1/test1_files/sheet002.htm
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html; charset="us-ascii"

<html xmlns:v=3D"urn:schemas-microsoft-com:vml"
xmlns:o=3D"urn:schemas-microsoft-com:office:office"
xmlns:x=3D"urn:schemas-microsoft-com:office:excel"
xmlns=3D"http://www.w3.org/TR/REC-html40">

<head>
<meta http-equiv=3DContent-Type content=3D"text/html; charset=3Dus-ascii">
<meta name=3DProgId content=3DExcel.Sheet>
<meta name=3DGenerator content=3D"Microsoft Excel 14">
<link id=3DMain-File rel=3DMain-File href=3D"../test1.htm">
<link rel=3DFile-List href=3Dfilelist.xml>
<![if IE]>
<base href=3D"file:///C:\D1745AB1\test1_files\sheet002.htm"
id=3D"webarch_temp_base_tag">
<![endif]>
<link rel=3DStylesheet href=3Dstylesheet.css>
<style>
<!--table
    {mso-displayed-decimal-separator:"\.";
    mso-displayed-thousand-separator:"\,";}
@page
    {margin:.75in .7in .75in .7in;
    mso-header-margin:.3in;
    mso-footer-margin:.3in;}
-->
</style>
<![if !supportTabStrip]><script language=3D"JavaScript">
<!--
function fnUpdateTabs()
 {
  if (parent.window.g_iIEVer>=3D4) {
   if (parent.document.readyState=3D=3D"complete"
    && parent.frames['frTabs'].document.readyState=3D=3D"complete")
   parent.fnSetActiveSheet(1);
  else
   window.setTimeout("fnUpdateTabs();",150);
 }
}

if (window.name!=3D"frSheet")
 window.location.replace("../test1.htm");
else
 fnUpdateTabs();
//-->
</script>
<![endif]>
</head>

<body link=3Dblue vlink=3Dpurple>

<table border=3D0 cellpadding=3D0 cellspacing=3D0 width=3D64 style=3D'borde=
r-collapse:
 collapse;table-layout:fixed;width:48pt'>
 <col width=3D64 style=3D'width:48pt'>
 <tr height=3D20 style=3D'height:15.0pt'>
  <td height=3D20 width=3D64 style=3D'height:15.0pt;width:48pt'>test2</td>
 </tr>
 <![if supportMisalignedColumns]>
 <tr height=3D0 style=3D'display:none'>
  <td width=3D64 style=3D'width:48pt'></td>
 </tr>
 <![endif]>
</table>

</body>

</html>

------=_NextPart_01CFFA76.49BAAF50
Content-Location: file:///C:/D1745AB1/test1_files/filelist.xml
Content-Transfer-Encoding: quoted-printable
Content-Type: text/xml; charset="utf-8"

<xml xmlns:o=3D"urn:schemas-microsoft-com:office:office">
 <o:MainFile HRef=3D"../test1.htm"/>
 <o:File HRef=3D"stylesheet.css"/>
 <o:File HRef=3D"tabstrip.htm"/>
 <o:File HRef=3D"sheet001.htm"/>
 <o:File HRef=3D"sheet002.htm"/>
 <o:File HRef=3D"filelist.xml"/>
</xml>
------=_NextPart_01CFFA76.49BAAF50--

但这种方法很糟糕。对不起,直言不讳。这种方法生成HTML,但强制Excel打开它。出于以下原因,这是一个坏主意:

  • 您希望用户使用Excel打开文件,因此您可以使用.XLS扩展名创建该文件。 Excel会抱怨它不是一个合适的Excel格式,并询问你是否真的要打开它。它让用户感到困惑。
  • 如果您使用.HTML扩展程序保存它,Excel不会抱怨,但它也不可能是打开HTML文件的默认程序(他们的浏览器是)。
  • 很难控制生成的HTML。 GridViews的标记不像你想要的那样干净。直接在Excel中使用HTML肯定会变坏。
  • 即使你使用了一个不错的库来生成HTML,它也是一种黑魔法,可以让它正确格式化,以便在Excel中看起来如此。

您需要使用实际能够生成适当的Excel兼容文件的库。由于您处于ASP.NET环境中,因此排除了Office Interop库。但您可以使用EPPlus库或Open Office XML SDK。它们能够实际生成真正的.XLSX(Open Office XML电子表格)文件,这些文件与Excel和Open Office / Libre Office等直接兼容.Apple的Spreadsheets也应该能够打开它们。