一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

Jsp中的table多表頭導出excel文件具體實現

首先引入兩份JS:copyhtmltoexcel.js以及 tableToExcel.js
復制代碼 代碼如下:
/*
 * 默認轉換實現函數,如果需要其他功能,需自行擴展
 * 參數:
 *      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(false);
  tb.isAutoFit(true);
  tb.getExcelFile();
}

/*
 *  功能:HTML中Table對象轉換為Excel通用對象.
 *  作者:Jeva
 *  時間:2006-08-09
 *  參數: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
 */
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控件和插件 /n/n" +
       "→ 啟用 : 對沒有標記為安全的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 = 1;
        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;
};

copyhtmltoexcel.js

復制代碼 代碼如下:
//elTalbeOut 這個為導出內容的外層表格,主要是設置border之類的樣式,elDiv則是整個導出的html部分
function onhtmlToExcel(elTableOut,elDiv){
 try{
  //設置導出前的數據,為導出后返回格式而設置
  var elDivStrBak = elDiv.innerHTML;
  //設置table的border=1,這樣到excel中就有表格線 ps:感謝雙面提醒
  elTableOut.border=1;
  //過濾elDiv內容
  var elDivStr = elDiv.innerHTML;
  elDivStr = replaceHtml(elDivStr,"<A",">");
  elDivStr = replaceHtml(elDivStr,"</A",">");
  elDiv.innerHTML=elDivStr; 

  var oRangeRef = document.body.createTextRange();
  oRangeRef.moveToElementText( elDiv );
  oRangeRef.execCommand("Copy");

  //返回格式變換以前的內容
  elDiv.innerHTML = elDivStrBak;
  //內容數據可能很大,所以賦空
  elDivStrBak = "";
  elDivStr = "";

  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
  oXL.Visible = true;  
  oSheet = null;
  oWB = null;
  appExcel = null;
 }catch(e){
  alert(e.description)
 }
}

function replaceHtml(replacedStr,repStr,endStr){  
  var replacedStrF = "";  
  var replacedStrB = "";  
  var repStrIndex = replacedStr.indexOf(repStr);  
  while(repStrIndex != -1){  
      replacedStrF = replacedStr.substring(0,repStrIndex);  
      replacedStrB = replacedStr.substring(repStrIndex,replacedStr.length);  
      replacedStrB = replacedStrB.substring(replacedStrB.indexOf(endStr)+1,replacedStrB.length);  
      replacedStr = replacedStrF + replacedStrB;  
      repStrIndex = replacedStr.indexOf(repStr);  
  }  
  return replacedStr;
}

在JSP頁面中寫JS方法
復制代碼 代碼如下:
  //生成Excel
  function onTableToExcel(){
   var elTableOut = document.getElementById("elTableOut");
   var elDiv = document.getElementById("elDiv");
   onhtmlToExcel(elTableOut,elDiv);
  }

注意事項:使用JS導出excel需要設置IE的active。注意最佳的是IE8瀏覽器。至于在Java后臺實現excel還沒去研究,希望各位大神指教。

jsp技術Jsp中的table多表頭導出excel文件具體實現,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 精品视频999 | 亚洲第一香蕉视频 | 国产一区二区三区在线观看免费 | 国产精品久久久久久一级毛片 | 国产成人亚洲精品91专区手机 | 亚洲美女一区 | 国产真实乱子伦精品 | 亚洲欧美自拍一区 | 九九久久久久午夜精选 | 婷婷色九月综合激情丁香 | 国产成人精品免费大全 | 国产福利一区二区三区在线视频 | 在线观看91精品国产剧情免费 | 99成人免费视频 | 久久免费福利视频 | 国内视频一区二区 | 国产百合一区二区三区 | 成人免费看片视频色多多 | 无码精品一区二区三区免费视频 | 91视频大全| 天天色综| 好吊妞视频在线观看 | 一区 在线播放 | 亚洲激情久久 | 日本aⅴ日本高清视频影片www | 狂野欧美性猛交xxxx免费 | 国产午夜视频在线观看 | 色婷婷亚洲综合五月 | 男女一级毛片免费播放 | 国产在线激情视频 | 激情区小说区偷拍区图片区 | 国产区精品福利在线观看精品 | 最新国产在线观看福利 | 久色伊人 | 福利毛片 | 国产91av在线| 四虎影视永久免费观看网址 | 日本一区二区三区国产 | 色伊人国产高清在线 | 国产三级全黄在线观看 | 国产精品久久久久999 |