<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    java隨記

    堅持就是勝利!

     

    javascript 操作 excel 全攻略

    最近做一個項目,用到了javascript操縱excel以生成報表,下面是標(biāo)有詳細(xì)注解的實例

    ?


    <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 控件”,您的瀏覽器須允許執(zhí)行控件。 請點擊【幫助】了解瀏覽器設(shè)置方法!");
    ????????????? return "";
    ???? }

    ??????????? xls.visible =true;? //設(shè)置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="發(fā)卡記錄";
    ???? //? xlsheet.Range(xlsheet.Cells(1,1),xlsheet.Cells(1,6)).Interior.ColorIndex=5;//設(shè)置底色為藍(lán)色?
    ??????????????? //?? xlsheet.Range(xlsheet.Cells(1,1),xlsheet.Cells(1,6)).Font.ColorIndex=4;//設(shè)置字體色?????????
    ?? // xlsheet.Rows(1). Interior .ColorIndex = 5 ;//設(shè)置底色為藍(lán)色? 設(shè)置背景色 Rows(1).Font.ColorIndex=4??

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

    ??? xlsheet.Columns("A:D").ColumnWidth =18;
    ???? <!--設(shè)置顯示字符而不是數(shù)字-->
    ??? xlsheet.Columns(2).NumberFormatLocal="@";
    ??? xlsheet.Columns(7).NumberFormatLocal="@";


    ???? //設(shè)置單元格內(nèi)容自動換行 range.WrapText? =? true? ;
    ???? //設(shè)置單元格內(nèi)容水平對齊方式 range.HorizontalAlignment? =? Excel.XlHAlign.xlHAlignCenter;//設(shè)置單元格內(nèi)容豎直堆砌方式
    ????? //range.VerticalAlignment=Excel.XlVAlign.xlVAlignCenter
    ??? //range.WrapText? =? true;? xlsheet.Rows(3).WrapText=true? 自動換行
    ??
    ??? //設(shè)置標(biāo)題欄

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

    ??? ?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; //設(shè)置左邊距
    ?????? xlsheet.Range( xlsheet.Cells(2,1),xlsheet.Cells(rowNum+1,7)).Borders(4).Weight = 2;//設(shè)置右邊距
    ???????????? xlsheet.Range( xlsheet.Cells(2,1),xlsheet.Cells(rowNum+1,7)).Borders(1).Weight = 2;//設(shè)置頂邊距
    ?????? xlsheet.Range( xlsheet.Cells(2,1),xlsheet.Cells(rowNum+1,7)).Borders(2).Weight = 2;//設(shè)置底邊距

    ?

    ???????
    ??????? 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?

    posted on 2006-06-27 11:12 傻 瓜 閱讀(30348) 評論(18)  編輯  收藏 所屬分類: javascript

    評論

    # re: javascript 操作 excel 全攻略 2006-06-27 12:42 我心飛翔

    不錯  回復(fù)  更多評論   

    # re: javascript 操作 excel 全攻略 2006-08-23 10:23 2

    2323  回復(fù)  更多評論   

    # re: javascript 操作 excel 全攻略 2007-01-28 16:41 mstu

    尋找中的,謝謝?。?nbsp; 回復(fù)  更多評論   

    # re: javascript 操作 excel 全攻略[未登錄] 2007-03-16 14:45 vivi

    感謝提供^_^  回復(fù)  更多評論   

    # re: javascript 操作 excel 全攻略 2007-08-01 10:46 xin

    bucuo  回復(fù)  更多評論   

    # re: javascript 操作 excel 全攻略 2007-08-16 14:00 菜鳥先飛

    不錯,非常感謝

    請再寫一篇同樣的生成word的.  回復(fù)  更多評論   

    # re: javascript 操作 excel 全攻略 2008-08-28 11:02 predancompany@gmail.com

    直接將上面代碼放在一個html文件運行是可以的,但是我放在j2ee的應(yīng)用中,并具改為.jsp文件,就不行了,不知道是什么原因,help me...........  回復(fù)  更多評論   

    # re: javascript 操作 excel 全攻略 2008-08-28 11:03 predancompany@gmail.com

    異常提示為下面的代碼:
    alert( "要打印該表,您必須安裝Excel電子表格軟件,同時瀏覽器須使用“ActiveX 控件”,您的瀏覽器須允許執(zhí)行控件。 請點擊【幫助】了解瀏覽器設(shè)置方法!");  回復(fù)  更多評論   

    # re: javascript 操作 excel 全攻略 2008-10-16 12:27 akchen

    學(xué)習(xí),IE沒問題,不過Firefox 不支持,一定要用ActiveX方式嗎?討論下,呵呵(E-Mai:akchen@126.coml)  回復(fù)  更多評論   

    # re: javascript 操作 excel 全攻略 2008-12-18 11:13 流浪的貓

    這里有一個問題,每導(dǎo)出一次,就打開一個EXCELE進程!請問這個問題如何解決?  回復(fù)  更多評論   

    # re: javascript 操作 excel 全攻略 2008-12-23 16:53 hillhua

    不錯,拿用了!  回復(fù)  更多評論   

    # re: javascript 操作 excel 全攻略 2008-12-26 10:48 as

    那是因為IE跟firefox調(diào)用控件函數(shù)不一樣,如果搞過ajax就應(yīng)該知道怎樣辦了  回復(fù)  更多評論   

    # re: javascript 操作 excel 全攻略 2008-12-26 10:49 as

    去掉xls.UserControl = true; 這一句就會在同一個excel進程里打開  回復(fù)  更多評論   

    # re: javascript 操作 excel 全攻略 2009-07-29 15:44 lasuk

    看了幾篇文章,發(fā)現(xiàn)一個字都不差

    唉 ~

    在哪抄的都?  回復(fù)  更多評論   

    # re: javascript 操作 excel 全攻略 2009-08-03 12:36 fool

    發(fā)布了rss,會被其它的網(wǎng)站抓取的老大.  回復(fù)  更多評論   

    # re: javascript 操作 excel 全攻略 2009-08-03 12:40 fool

    放心,轉(zhuǎn)抄的我會注明出處的  回復(fù)  更多評論   

    # re: javascript 操作 excel 全攻略 2011-10-18 08:21 怒云

    關(guān)于事件的呢?怎么定義事件

    下面是我的代碼,本地腳本:

    var ExcelApp=new ActiveXObject("Excel.Application");
    var wb=ExcelApp.Workbooks.Add();
    ExcelApp.Visible=true;
    wb.BeforeClose=function (Cancel){
    WScript.Echo("Workbook was closing");
    }
    wb.Close();
    WScript.Sleep(3000)
    ExcelApp.Quit();
    可惜出錯了。。。求正解  回復(fù)  更多評論   

    # re: javascript 操作 excel 全攻略 2015-03-18 10:06 416119174

    這個只能導(dǎo)出靜態(tài)的,我是動態(tài)的,有記錄集,查詢記錄是分頁顯示的  回復(fù)  更多評論   


    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導(dǎo)航:
     

    導(dǎo)航

    統(tǒng)計

    常用鏈接

    留言簿(7)

    我參與的團隊

    隨筆分類

    隨筆檔案

    文章分類

    友情鏈接

    搜索

    積分與排名

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲中文字幕视频国产| 亚洲人成黄网在线观看| 免费无码中文字幕A级毛片| 亚洲成A∨人片在线观看无码| 成人五级毛片免费播放| 特级无码毛片免费视频| 亚洲激情视频在线观看| 女人18毛片水真多免费看| 9久久免费国产精品特黄| 亚洲免费闲人蜜桃| 亚洲精品成a人在线观看| 亚洲免费黄色网址| 免费一级做a爰片久久毛片潮| 亚洲日韩图片专区第1页| 国产大片91精品免费看3| 性xxxx视频免费播放直播| 亚洲AV无码AV吞精久久| 久久精品亚洲一区二区三区浴池 | 国产高清对白在线观看免费91| 78成人精品电影在线播放日韩精品电影一区亚洲 | 免费播放一区二区三区| 亚洲AV永久无码精品一福利| 国产精品久久久亚洲| 国内一级一级毛片a免费| 日本免费在线观看| 羞羞漫画小舞被黄漫免费| 亚洲冬月枫中文字幕在线看 | 精品国产亚洲一区二区三区| 在线观看成人免费视频| 97在线视频免费公开观看| 成年大片免费视频播放一级| 亚洲伊人久久大香线蕉啊| 亚洲人成伊人成综合网久久久| 成人永久免费高清| 免费看成人AA片无码视频羞羞网| 中文字幕av无码不卡免费| 久久精品国产亚洲AV| 亚洲国产一区在线观看| 亚洲成AV人片在线播放无码| 亚洲国产中文v高清在线观看| 好大好硬好爽免费视频|