熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> JSP教程 >> 正文

js導出格式化的excel 實例方法

2022-06-13   來源: JSP教程 
最常見的方式是使用js函數操作excle文件這種方式可能因浏覽器不同存在兼容性問題另一種簡單方法就是導出一個excel能識別格式的xml文件用excel打開   復制代碼 代碼如下:
function getTableDataByXML(inTable inWindow) {
    var rows = ;
    //alert("getTblData is " + inWindow);
    var tblDocument = document;
    if (!!inWindow && inWindow != "") {
        if (!documentall(inWindow)) {
            return null;
        }
        else {
            tblDocument = eval(inWindow)document;
        }
    }
    var inTbl = tblDocumentgetElementById(inTable);
    var outStr = "<?xml version=""?>n";
    outStr = outStr + "<?msoapplication progid="ExcelSheet"?>n";
    outStr = outStr + "<Workbook xmlns="urn:schemasmicrosoftcom:office:spreadsheet"";
    outStr = outStr + " xmlns:o="urn:schemasmicrosoftcom:office:office"";
    outStr = outStr + " xmlns:x="urn:schemasmicrosoftcom:office:excel"";
    outStr = outStr + " xmlns:ss="urn:schemasmicrosoftcom:office:spreadsheet">n";
    outStr = outStr + "<Worksheet ss:Name="Sheet">n";
    outStr = outStr + "<Table ss:ExpandedColumnCount="">n";
    var re = /^[]+?[]*$/; //是否為數字
    if (inTbl != null) {
        for (var j = ; j < inTblrowslength; j++) {
            outStr += "<Row ss:AutoFitHeight="">n";
            for (var i = ; i < inTblrows[j]cellslength; i++) {
                if (i == && rows > ) {
                    outStr += "<Cell><Data ss:Type="String"></Data></Cell>n";
                    rows = ;
                }
                var cellValue = inTblrows[j]cells[i]innerText;
                //小於位數字用Number
                if(retest(cellValue) && (new String(cellValue))length < ){
                    outStr = outStr + "<Cell><Data ss:Type="Number">" + cellValue + "</Data></Cell>n";
                }else{
                    outStr = outStr + "<Cell><Data ss:Type="String">" + cellValue + "</Data></Cell>n";
                }
                if (inTblrows[j]cells[i]colSpan > ) {
                    for (var k = ; k < inTblrows[j]cells[i]colSpan ; k++) {
                        outStr += " <Cell><Data ss:Type="String"></Data></Cell>n";
                    }
                }
                if (i == ) {
                    if (rows == && inTblrows[j]cells[i]rowSpan > ) {
                        rows = inTblrows[j]cells[i]rowSpan ;
                    }
                }
            }
            outStr += "</Row>n";
        }
    }
    else {
        outStr = null;
        alert("你要導出的表不存在!!");
        return;
    }
    outStr = outStr + "</Table>n</Worksheet>n</Workbook>";
    return outStr;
}

  
上述函數原本是導出txt文件的函數把excel文件另存為一個xml文件就可得到excel能識別什麼內容格式的xml文件


From:http://tw.wingwit.com/Article/program/Java/JSP/201311/20186.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.