<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當前位置: 首頁 - 科技 - 知識百科 - 正文

        js導出到excel實例教程

        來源:懂視網 責編:小采 時間:2020-11-27 20:20:26
        文檔

        js導出到excel實例教程

        js導出到excel實例教程:轉自:<!DOCTYPE html><html><head> <meta charset="utf-8"> <title>保存到Excel</title></head><body> <input type="button&
        推薦度:
        導讀js導出到excel實例教程:轉自:<!DOCTYPE html><html><head> <meta charset="utf-8"> <title>保存到Excel</title></head><body> <input type="button&

        轉自:


        <!DOCTYPE html>
        <html>
        <head>
        <meta charset="utf-8">
        <title>保存到Excel</title>
        </head>
        <body>
        <input type="button" value="保存到Excel" onclick="JavaScript:saveAsExcel('tableId')" />
        <table id="tableId">
        <thead>
        <tr>
        <th>序列</th>
        <th>名字</th>
        <th>年齡</th>
        <th>性別</th>
        </tr>
        </thead>
        <tbody>
        <tr>
        <td>01</td>
        <td>張三</td>
        <td>18</td>
        <td>女</td>
        </tr>
        <tr>
        <td>02</td>
        <td>李四</td>
        <td>20</td>
        <td>女</td>
        </tr>
        <tr>
        <td>03</td>
        <td>王五</td>
        <td>22</td>
        <td>男</td>
        </tr>
        <tr>
        <td>04</td>
        <td>張飛</td>
        <td>100</td>
        <td>男</td>
        </tr>


        </tbody>
        </table>
        </body>
        <script type="text/javascript">
        /*
        * 默認轉換實現函數,如果需要其他功能,需自行擴展
        * 參數:
        * tableID : HTML中Table對象id屬性值
        * 詳細用法參見以下 TableToExcel 對象定義
        */
        function saveAsExcel(tableID) {
        var tb = new TableToExcel(tableID);
        tb.setFontStyle("Courier New");
        tb.setFontSize(10);
        tb.setTableBorder(2);
        tb.setColumnWidth(7);
        tb.isLineWrap(true);
        tb.getExcelFile();
        }


        /*
        * 功能:HTML中Table對象轉換為Excel通用對象.
        * 參數:tableID HTML中Table對象的ID屬性值
        * 說明:
        * 能適應復雜的HTML中Table對象的自動轉換,能夠自動根據行列擴展信息
        * 合并Excel中的單元格,客戶端需要安裝有Excel
        * 詳細的屬性、方法引用說明參見:Excel的Microsoft Excel Visual Basic參考
        * 示范:
        * var tb = new TableToExcel('demoTable');
        * tb.setFontStyle("Courier New");
        * tb.setFontSize(10); //推薦取值10
        * tb.setFontColor(6); //一般情況不用設置
        * tb.setBackGround(4); //一般情況不用設置
        * tb.setTableBorder(2); //推薦取值2
        * tb.setColumnWidth(10); //推薦取值10
        * tb.isLineWrap(false);
        * tb.isAutoFit(true);
        *
        * tb.getExcelFile();
        * 如果設置了單元格自適應,則設置單元格寬度無效
        * 版本:1.0
        * BUG提交:QQ:18234348 或者
        */
        function TableToExcel(tableID) {
        this.tableBorder = -1; //邊框類型,-1沒有邊框 可取1/2/3/4
        this.backGround = 0; //背景顏色:白色 可取調色板中的顏色編號 1/2/3/4....
        this.fontColor = 1; //字體顏色:黑色
        this.fontSize = 10; //字體大小
        this.fontStyle = "宋體"; //字體類型
        this.rowHeight = -1; //行高
        this.columnWidth = -1; //列寬
        this.lineWrap = true; //是否自動換行
        this.textAlign = -4108; //內容對齊方式 默認為居中
        this.autoFit = false; //是否自適應寬度
        this.tableID = tableID;
        }


        TableToExcel.prototype.setTableBorder = function (excelBorder) {
        this.tableBorder = excelBorder;
        };


        TableToExcel.prototype.setBackGround = function (excelColor) {
        this.backGround = excelColor;
        };


        TableToExcel.prototype.setFontColor = function (excelColor) {
        this.fontColor = excelColor;
        };


        TableToExcel.prototype.setFontSize = function (excelFontSize) {
        this.fontSize = excelFontSize;
        };


        TableToExcel.prototype.setFontStyle = function (excelFont) {
        this.fontStyle = excelFont;
        };


        TableToExcel.prototype.setRowHeight = function (excelRowHeight) {
        this.rowHeight = excelRowHeight;
        };


        TableToExcel.prototype.setColumnWidth = function (excelColumnWidth) {
        this.columnWidth = excelColumnWidth;
        };


        TableToExcel.prototype.isLineWrap = function (lineWrap) {
        if (lineWrap == false || lineWrap == true) {
        this.lineWrap = lineWrap;
        }
        };


        TableToExcel.prototype.setTextAlign = function (textAlign) {
        this.textAlign = textAlign;
        };


        TableToExcel.prototype.isAutoFit = function (autoFit) {
        if (autoFit == true || autoFit == false)
        this.autoFit = autoFit;
        }
        //文件轉換主函數
        TableToExcel.prototype.getExcelFile = function () {
        var jXls, myWorkbook, myWorksheet, myHTMLTableCell, myExcelCell, myExcelCell2;
        var myCellColSpan, myCellRowSpan;


        try {
        jXls = new ActiveXObject('Excel.Application');
        }
        catch (e) {
        alert("無法啟動Excel!\n\n" + e.message +
        "\n\n如果您確信您的電腦中已經安裝了Excel," +
        "那么請調整IE的安全級別。\n\n具體操作:\n\n" +
        "工具 → Internet選項 → 安全 → 自定義級別 → 對沒有標記為安全的ActiveX進行初始化和腳本運行 → 啟用");
        return false;
        }


        jXls.Visible = true;
        myWorkbook = jXls.Workbooks.Add();
        jXls.DisplayAlerts = false;
        myWorkbook.Worksheets(3).Delete();
        myWorkbook.Worksheets(2).Delete();
        jXls.DisplayAlerts = true;
        myWorksheet = myWorkbook.ActiveSheet;


        var readRow = 0, readCol = 0;
        var totalRow = 0, totalCol = 0;
        var tabNum = 0;


        //設置行高、列寬
        if (this.columnWidth != -1)
        myWorksheet.Columns.ColumnWidth = this.columnWidth;
        else
        myWorksheet.Columns.ColumnWidth = 7;
        if (this.rowHeight != -1)
        myWorksheet.Rows.RowHeight = this.rowHeight;


        //搜索需要轉換的Table對象,獲取對應行、列數
        var obj = document.all.tags("table");
        for (x = 0; x < obj.length; x++) {
        if (obj[x].id == this.tableID) {
        tabNum = x;
        totalRow = obj[x].rows.length;
        for (i = 0; i < obj[x].rows[0].cells.length; i++) {
        myHTMLTableCell = obj[x].rows(0).cells(i);
        myCellColSpan = myHTMLTableCell.colSpan;
        totalCol = totalCol + myCellColSpan;
        }
        }
        }


        //開始構件模擬表格
        var excelTable = new Array();
        for (i = 0; i <= totalRow; i++) {
        excelTable[i] = new Array();
        for (t = 0; t <= totalCol; t++) {
        excelTable[i][t] = false;
        }
        }


        //開始轉換表格
        for (z = 0; z < obj[tabNum].rows.length; z++) {
        readRow = z + 1;
        readCol = 0;
        for (c = 0; c < obj[tabNum].rows(z).cells.length; c++) {
        myHTMLTableCell = obj[tabNum].rows(z).cells(c);
        myCellColSpan = myHTMLTableCell.colSpan;
        myCellRowSpan = myHTMLTableCell.rowSpan;
        for (y = 1; y <= totalCol; y++) {
        if (excelTable[readRow][y] == false) {
        readCol = y;
        break;
        }
        }
        if (myCellColSpan * myCellRowSpan > 1) {
        myExcelCell = myWorksheet.Cells(readRow, readCol);
        myExcelCell2 = myWorksheet.Cells(readRow + myCellRowSpan - 1, readCol + myCellColSpan - 1);
        myWorksheet.Range(myExcelCell, myExcelCell2).Merge();
        myExcelCell.HorizontalAlignment = this.textAlign;
        myExcelCell.Font.Size = this.fontSize;
        myExcelCell.Font.Name = this.fontStyle;
        myExcelCell.wrapText = this.lineWrap;
        myExcelCell.Interior.ColorIndex = this.backGround;
        myExcelCell.Font.ColorIndex = this.fontColor;
        if (this.tableBorder != -1) {
        myWorksheet.Range(myExcelCell, myExcelCell2).Borders(1).Weight = this.tableBorder;
        myWorksheet.Range(myExcelCell, myExcelCell2).Borders(2).Weight = this.tableBorder;
        myWorksheet.Range(myExcelCell, myExcelCell2).Borders(3).Weight = this.tableBorder;
        myWorksheet.Range(myExcelCell, myExcelCell2).Borders(4).Weight = this.tableBorder;
        }


        myExcelCell.Value = myHTMLTableCell.innerText;
        for (row = readRow; row <= myCellRowSpan + readRow - 1; row++) {
        for (col = readCol; col <= myCellColSpan + readCol - 1; col++) {
        excelTable[row][col] = true;
        }
        }


        readCol = readCol + myCellColSpan;
        } else {
        myExcelCell = myWorksheet.Cells(readRow, readCol);
        myExcelCell.Value = myHTMLTableCell.innerText;
        myExcelCell.HorizontalAlignment = this.textAlign;
        myExcelCell.Font.Size = this.fontSize;
        myExcelCell.Font.Name = this.fontStyle;
        myExcelCell.wrapText = this.lineWrap;
        myExcelCell.Interior.ColorIndex = this.backGround;
        myExcelCell.Font.ColorIndex = this.fontColor;
        if (this.tableBorder != -1) {
        myExcelCell.Borders(1).Weight = this.tableBorder;
        myExcelCell.Borders(2).Weight = this.tableBorder;
        myExcelCell.Borders(3).Weight = this.tableBorder;
        myExcelCell.Borders(4).Weight = this.tableBorder;
        }
        excelTable[readRow][readCol] = true;
        readCol = readCol + 1;
        }
        }
        }
        if (this.autoFit == true)
        myWorksheet.Columns.AutoFit;


        jXls.UserControl = true;
        jXls = null;
        myWorkbook = null;
        myWorksheet = null;
        };
        </script>
        </html>

        聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        js導出到excel實例教程

        js導出到excel實例教程:轉自:<!DOCTYPE html><html><head> <meta charset="utf-8"> <title>保存到Excel</title></head><body> <input type="button&
        推薦度:
        標簽: 導出 excel 導入
        • 熱門焦點

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 亚洲免费在线视频观看| 亚洲欧洲一区二区| 亚洲精品乱码久久久久久V| 亚洲AV人无码激艳猛片| 三年在线观看免费观看完整版中文| 免费jlzzjlzz在线播放视频| 看全免费的一级毛片| 亚洲成a人片在线观看日本麻豆| 亚洲国产精品ⅴa在线观看| 日本a级片免费看| 国产精品亚洲va在线观看| 免费大黄网站在线观| 久久久久久av无码免费看大片| 国产亚洲人成无码网在线观看| 亚洲欧洲另类春色校园网站| aaa毛片免费观看| 亚洲国产精华液网站w| 91av免费观看| 亚洲人成综合在线播放| 毛色毛片免费观看| 久久久久亚洲精品无码系列| 日本免费人成网ww555在线| 亚洲日本在线播放| 免费的全黄一级录像带| 亚洲 国产 图片| 亚洲色成人网站WWW永久四虎| 亚洲一区二区在线免费观看| 亚洲精品免费在线| 成人av免费电影| 一级做受视频免费是看美女| 国产男女猛烈无遮挡免费视频网站 | 亚洲日产2021三区| 韩国日本好看电影免费看| 国产亚洲综合久久| 亚洲AV无码久久精品蜜桃| 精品熟女少妇aⅴ免费久久| 亚洲国产老鸭窝一区二区三区| 国产成人免费爽爽爽视频 | 日韩一级免费视频| 国产一区二区三区免费观看在线| 又粗又硬免费毛片|