这只是一个SavaAs的功能要控制格式是不可能的

解决方案 »

  1.   

    <html xmlns:o="urn:schemas-microsoft-com:office:office"
    xmlns:x="urn:schemas-microsoft-com:office:excel"
    xmlns="http://www.w3.org/TR/REC-html40"><head>
    <meta http-equiv=Content-Type content="text/html; charset=GB2312">
    <script>
    function out(){ try{
    var elTable = document.getElementById("out");
    var oRangeRef = document.body.createTextRange();
    oRangeRef.moveToElementText( elTable );
    oRangeRef.execCommand( "Copy" ); var oXL = new ActiveXObject("Excel.Application")
    var oWB = oXL.Workbooks.Add ;
    var oSheet = oWB.ActiveSheet ;
    oSheet.Paste();
    oSheet.Cells.NumberFormatLocal = "@";

    oSheet.Columns("D:D").Select
    oXL.Selection.ColumnWidth = 20 //oSheet.Columns("A:A").Select
    //oSheet.Columns("A").Width = 1000; oXL.Visible = true;
    oSheet = null;
    oWB = null;
    appExcel = null; }catch(e){alert(e.description)}

    }</script>
    </head><body><table id ='out'>
     <tr>
      <td x:str bgcolor="#FF0000">00001</td>
      <td x:str>0002</td>
     </tr>
     <tr>
      <td height=18 align=right style='height:13.5pt' x:str>00001</td>
      <td align=right x:str>000002</td>
     </tr>
     <tr>
      <td x:str>0003</td>
      <td x:str>00003</td>
     </tr>
    </table>
    <INPUT type="button" value="Button" id=button1 name=button1 onclick="out()">
    </body>
    </html>
      

  2.   

    楼上的大侠,能简单解释一下吗?td x:str...是不是都设置呢?
      

  3.   

    这些不设置也可以。
    直接用
    oSheet.Cells.NumberFormatLocal = "@"; // 修改该单元格为字符串格式
    类似的vba就可以实现。
    具体的可操作对象属性等,可以去微软上面查手册。
    或者excel里面录制宏,查看源代码。
      

  4.   

    请教:fantiny(乐于助人的菜鸟回归) ,我用了一下您的代码。运行时出现“Automation服务器对象不能创建”提示,请问这应该如何解决。在TD里面的x:str是做什么的,x:str必须有吗?
      

  5.   

    fantiny(乐于助人的菜鸟回归) ,我用的是ASP语言。是不是因这样才会出现这个提示呀。若是这样,请问如何用ASP+JAVASCRIPT 实现 转化成excel的功能呢
      

  6.   

    ASP 用vba更简单,因为都是vbscript,语法都不用改。
    不能创建对象是因为你ie执行权限不够。或者你没装excel在TD里面的x:str表示字符串,这个不是必须有的。跟
    <html xmlns:o="urn:schemas-microsoft-com:office:office"
    xmlns:x="urn:schemas-microsoft-com:office:excel"
    xmlns="http://www.w3.org/TR/REC-html40">结合才有用。这些全部用代码可以实现转换,不用设置也可。
      

  7.   

    你把文件放到localhost目录下,通过localhost访问。
    把localhost设置为信任站点看会不会出错。
      

  8.   

    修改IE的安全级别。在安全级别里修改ACTIVEX的选项,不过比较危险,有可能某些页面有病毒直接侵入系统。
      

  9.   

    function exportExcel(atblData)
    { var w = window.open("about:blank", "Excel", "widht=0, height=0");
    w.document.write(atblData.outerHTML);
    w.document.execCommand('Saveas',false, 'C:\\log.xls'); }
    你去试试,应该可以!
      

  10.   

    乐于助人大哥,怎么能让生成的EXCEL文件获得焦点呢?就是EXCEL显示在最前面,我现在就遇到这个问题,郁闷死了!
      

  11.   

    <html xmlns:o="urn:schemas-microsoft-com:office:office"
    xmlns:x="urn:schemas-microsoft-com:office:excel"
    xmlns="http://www.w3.org/TR/REC-html40"><head>
    <meta http-equiv=Content-Type content="text/html; charset=GB2312">
    <script>function newOut(){ objWin = window.open('','');
    objWin.document.open();
    objWin.document.write ("<" + "script>");
    objWin.document.write (" function out(){");
    objWin.document.write (" try{");
    objWin.document.write (' var oXL = new ActiveXObject("Excel.Application");');
    objWin.document.write (" var oWB = oXL.Workbooks.Add ;");
    objWin.document.write (" var oSheet = oWB.ActiveSheet ;");
    objWin.document.write (" oSheet.Paste();");
    objWin.document.write (' oSheet.Cells.NumberFormatLocal = "@";');
    objWin.document.write (' oSheet.Columns("D:D").Select;');
    objWin.document.write (" oXL.Selection.ColumnWidth = 20;");
    objWin.document.write (" oXL.Visible = true;");
    objWin.document.write (" oSheet = null;");
    objWin.document.write (" oWB = null;");
    objWin.document.write (" appExcel = null;");
    objWin.document.write (" }catch(e){alert(e.description)}");
    objWin.document.write (" }");
    objWin.document.write ("function chk(){");
    objWin.document.write ("out();");
    objWin.document.write ("window.opener = null;");
    objWin.document.write ("window.close();");
    objWin.document.write ("}");
    objWin.document.write ("</" + "script>");
    objWin.document.write ("<body bgcolor=#cccccc onload='chk()'></body>");
    objWin.document.close;}</script>
    </head><body>
    <a href="#" onclick="newOut()">New out</a>
    </body>
    </html>
      

  12.   

    你可以看看vba相关的文章和excel对象模型
    简单的说
    excel
    有application workbook sheet range对象模型
    操作这几个对象模型的方法和属性,
    就能达到手工操作一样的效果。