javascript 操作 excel 全攻略

來源:互聯網
上載者:User

最近做一個項目,用到了javascript操縱excel以產生報表,下面是標有詳細註解的執行個體

 

<html>

<head>
<script language="javascript" type="text/javascript">

</script><script language="javascript" type="text/javascript">
function MakeExcel(){
var i,j;
    try {
      var xls    = new ActiveXObject ( "Excel.Application" );
     }
    catch(e) {
         alert( "要列印該表,您必須安裝Excel試算表軟體,同時瀏覽器須使用“ActiveX 控制項”,您的瀏覽器須允許執行控制項。 請點擊【協助】瞭解瀏覽器設定方法!");
              return "";
     }

            xls.visible =true;  //設定excel為可見

    var xlBook = xls.Workbooks.Add;
    var xlsheet = xlBook.Worksheets(1);
    <!--合并-->
      xlsheet.Range(xlsheet.Cells(1,1),xlsheet.Cells(1,7)).mergecells=true;
      xlsheet.Range(xlsheet.Cells(1,1),xlsheet.Cells(1,7)).value="發卡記錄";
     //  xlsheet.Range(xlsheet.Cells(1,1),xlsheet.Cells(1,6)).Interior.ColorIndex=5;//設定底色為藍色 
                //   xlsheet.Range(xlsheet.Cells(1,1),xlsheet.Cells(1,6)).Font.ColorIndex=4;//設定字型色         
   // xlsheet.Rows(1). Interior .ColorIndex = 5 ;//設定底色為藍色  設定背景色 Rows(1).Font.ColorIndex=4  

    <!--設定行高-->
    xlsheet.Rows(1).RowHeight = 25;
    <!--設定字型 ws.Range(ws.Cells(i0+1,j0), ws.Cells(i0+1,j1)).Font.Size = 13 -->
    xlsheet.Rows(1).Font.Size=14;
    <!--設定字型 設定選定區的字型  xlsheet.Range(xlsheet.Cells(i0,j0), ws.Cells(i0,j0)).Font.Name = "黑體" -->
    xlsheet.Rows(1).Font.Name="黑體";
    <!--設定列寬 xlsheet.Columns(2)=14;-->

    xlsheet.Columns("A:D").ColumnWidth =18;
     <!--設定顯示字元而不是數字-->
    xlsheet.Columns(2).NumberFormatLocal="@";
    xlsheet.Columns(7).NumberFormatLocal="@";

     //設定儲存格內容自動換行 range.WrapText  =  true  ;
     //設定儲存格內容水平對齊 range.HorizontalAlignment  =  Excel.XlHAlign.xlHAlignCenter;//設定儲存格內容豎直堆砌方式
      //range.VerticalAlignment=Excel.XlVAlign.xlVAlignCenter
    //range.WrapText  =  true;  xlsheet.Rows(3).WrapText=true  自動換行
  
    //設定標題列

     xlsheet.Cells(2,1).Value="卡號";
     xlsheet.Cells(2,2).Value="密碼";
     xlsheet.Cells(2,3).Value="計費方式";
     xlsheet.Cells(2,4).Value="有效天數";
     xlsheet.Cells(2,5).Value="金額";
     xlsheet.Cells(2,6).Value="所屬服務項目";
       xlsheet.Cells(2,7).Value="發卡時間";

     var oTable=document.all['fors:data'];
     var rowNum=oTable.rows.length;
     for(i=2;i<=rowNum;i++){
     for (j=1;j<=7;j++){
//html table類容寫到excel

       xlsheet.Cells(i+1,j).Value=oTable.rows(i-1).cells(j-1).innerHTML;
            }

    }
    <!--   xlsheet.Range(xls.Cells(i+4,2),xls.Cells(rowNum,4)).Merge; -->
    // xlsheet.Range(xlsheet.Cells(i, 4), xlsheet.Cells(i-1, 6)).BorderAround , 4
     // for(mn=1,mn<=6;mn++) .     xlsheet.Range(xlsheet.Cells(1, mn), xlsheet.Cells(i1, j)).Columns.AutoFit;
      xlsheet.Columns.AutoFit;
                 xlsheet.Range( xlsheet.Cells(1,1),xlsheet.Cells(rowNum+1,7)).HorizontalAlignment =-4108;//置中
                   xlsheet.Range( xlsheet.Cells(1,1),xlsheet.Cells(1,7)).VerticalAlignment =-4108;
                 xlsheet.Range( xlsheet.Cells(2,1),xlsheet.Cells(rowNum+1,7)).Font.Size=10;

      xlsheet.Range( xlsheet.Cells(2,1),xlsheet.Cells(rowNum+1,7)).Borders(3).Weight = 2; //設定左邊距
       xlsheet.Range( xlsheet.Cells(2,1),xlsheet.Cells(rowNum+1,7)).Borders(4).Weight = 2;//設定右邊距
             xlsheet.Range( xlsheet.Cells(2,1),xlsheet.Cells(rowNum+1,7)).Borders(1).Weight = 2;//設定頂邊距
       xlsheet.Range( xlsheet.Cells(2,1),xlsheet.Cells(rowNum+1,7)).Borders(2).Weight = 2;//設定底邊距

 

       
        xls.UserControl = true;  //很重要,不能省略,不然會出問題 意思是excel交由使用者控制
       xls=null;
       xlBook=null;
       xlsheet=null;

}

 

</script>  <link href="css/styles3.css" rel="stylesheet" type="text/css"/>
<title>ziyuanweihu</title>
</head>
<body>
  <form id="fors" method="post" action="/WebModule/admins/card/showcard.faces" enctype="application/x-www-form-urlencoded">

   
     
     
     
    <table id="fors:top" border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="left"><img src="images/jiao1.gif" alt="" /></td>
<td class="topMiddle"></td>
<td class="right"><img src="images/jiao2.gif" alt="" /></td>
</tr>
</tbody>
</table>            
            
  
      
      
    <table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="middleLeft"></td>
<td class="btstyle"><table id="fors:sort" border="0" cellpadding="0" cellspacing="0" style="valign:center" width="100%">
<tbody>
<tr>
<td class="btstyle"><input type="button" name="fors:_id7" value="&#29983;&#25104;excel&#25991;&#20214;" onclick="MakeExcel()" /><input type="submit" name="fors:_id8" value="&#36820;&#22238;" /></td>
</tr>
</tbody>
</table>
<table id="fors:data" border="1" cellpadding="0" cellspacing="1" width="100%">
<thead>
<tr>
<th scope="col"><span id="fors:data:headerText1">&#21345;&#21495;</span></th>
<th scope="col"><span id="fors:data:headerText2">&#23494;&#30721;</span></th>
<th scope="col"><span id="fors:data:headerText3">&#35745;&#36153;&#26041;&#24335;</span></th>
<th scope="col"><span id="fors:data:headerText4">&#26377;&#25928;&#22825;&#25968;</span></th>
<th scope="col">&#37329;&#39069;</th>
<th scope="col"><span id="fors:data:headerText6">&#25152;&#23646;&#26381;&#21153;&#39033;&#30446;</span></th>
<th scope="col"><span id="fors:data:headerText7">&#21457;&#21345;&#26102;&#38388;</span></th>
</tr>
</thead>
<tbody>
<tr>
<td>h000010010</td>
<td>543860</td>
<td>&#35745;&#28857;</td>
<td></td>
<td>2.0</td>
<td>&#27979;&#35797;&#39033;&#30446;</td>
<td>2006-06-23 10:14:40.843</td>
</tr>
<tr>
<td>h000010011</td>
<td>683352</td>
<td>&#35745;&#28857;</td>
<td></td>
<td>2.0</td>
<td>&#27979;&#35797;&#39033;&#30446;</td>
<td>2006-06-23 10:14:40.843</td>
</tr>
<tr>
<td>h000010012</td>
<td>433215</td>
<td>&#35745;&#28857;</td>
<td></td>
<td>2.0</td>
<td>&#27979;&#35797;&#39033;&#30446;</td>
<td>2006-06-23 10:14:40.843</td>
</tr>
<tr>
<td>h000010013</td>
<td>393899</td>
<td>&#35745;&#28857;</td>
<td></td>
<td>2.0</td>
<td>&#27979;&#35797;&#39033;&#30446;</td>
<td>2006-06-23 10:14:40.843</td>
</tr>
<tr>
<td>h000010014</td>
<td>031736</td>
<td>&#35745;&#28857;</td>
<td></td>
<td>2.0</td>
<td>&#27979;&#35797;&#39033;&#30446;</td>
<td>2006-06-23 10:14:40.843</td>
</tr>
<tr>
<td>h000010015</td>
<td>188600</td>
<td>&#35745;&#28857;</td>
<td></td>
<td>2.0</td>
<td>&#27979;&#35797;&#39033;&#30446;</td>
<td>2006-06-23 10:14:40.843</td>
</tr>
<tr>
<td>h000010016</td>
<td>363407</td>
<td>&#35745;&#28857;</td>
<td></td>
<td>2.0</td>
<td>&#27979;&#35797;&#39033;&#30446;</td>
<td>2006-06-23 10:14:40.843</td>
</tr>
<tr>
<td>h000010017</td>
<td>175315</td>
<td>&#35745;&#28857;</td>
<td></td>
<td>2.0</td>
<td>&#27979;&#35797;&#39033;&#30446;</td>
<td>2006-06-23 10:14:40.843</td>
</tr>
<tr>
<td>h000010018</td>
<td>354437</td>
<td>&#35745;&#28857;</td>
<td></td>
<td>2.0</td>
<td>&#27979;&#35797;&#39033;&#30446;</td>
<td>2006-06-23 10:14:40.843</td>
</tr>
<tr>
<td>h000010019</td>
<td>234750</td>
<td>&#35745;&#28857;</td>
<td></td>
<td>2.0</td>
<td>&#27979;&#35797;&#39033;&#30446;</td>
<td>2006-06-23 10:14:40.843</td>
</tr>
</tbody>
</table>
</td>
<td class="middleRight"></td>
</tr>
</tbody>
</table>

    <table id="fors:bottom" border="0" cellpadding="0" cellspacing="0" width="100%">
      <tbody>
        <tr>
          <td class="left">
            <img src="images/jiao3.gif" alt=""/>
          </td>
          <td class="bottomMiddle">          </td>
          <td class="right">
            <img src="images/jiao4.gif" alt=""/>
          </td>
        </tr>
      </tbody>
    </table>
 <input type="hidden" name="fors" value="fors" /></form>
</body>

</html>
歡迎加入QQ群:30406099 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.