先端のjs-xlsxプラグインはexcelファイルのデータを導入してjsonフォーマットです.

2149 ワード




    
    
    <!--<script src="http://oss.sheetjs.com/js-xlsx/xlsx.full.min.js"></script>-->



<input type="file" onchange="importf(this)"/>
<div id="demo"/>
<!--<script src="xlsx.full.min.js"></script>-->
<script src="xlsx.min.js"/>
<script>
    /*
    FileReader  4     :
    1.readAsArrayBuffer(file):      ArrayBuffer。
    2.readAsBinaryString(file):            
    3.readAsDataURL(file):      Data URL
    4.readAsText(file, [encoding]):        ,encoding    'UTF-8'
    */
    var wb;//       
    var rABS = true; //              

    function importf(obj) {//  
        if (!obj.files) {
            return;
        }
        var f = obj.files[0];
        var reader = new FileReader();
        reader.onload = function (e) {
            var data = e.target.result;
            if (rABS) {
                wb = XLSX.read(data, {
                    type: 'binary'
                });
            } else {
                wb = XLSX.read(btoa(fixdata(data)), {//    
                    type: 'base64'
                });
            }
            //wb.SheetNames[0]   Sheets    Sheet   
            //wb.Sheets[Sheet ]     Sheet   
            document.getElementById("demo").innerHTML = JSON.stringify(XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]));
        };
        if (rABS) {
            reader.readAsBinaryString(f);
        } else {
            reader.readAsArrayBuffer(f);
        }
    }

    function fixdata(data) { //    BinaryString
        var o = "",
            l = 0,
            w = 10240;
        for (; l < data.byteLength / w; ++l) o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w, l * w + w)));
        o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w)));
        return o;
    }
</script>

</code></pre> 
  <p> </p> 
 </div> 
</div>
                            </div>
                        </div>