on 08-24-2016 7:30 AM
that demo gives everything you need. just check the source code
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
take a look at this thread
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks for reply rajeesh
i am using xlsx.js lib but its not working.
i am creating project using JS-XLSX Live Demo that sample.
Thanks & Regards,
Ayushi
XLSX.js file- https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.8.0/xlsx.js
jszip.js file- https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.8.0/jszip.js
hope this helps
HI Rajeesh
here is my code
view part-
<l:VerticalLayout>
<u:FileUploader id="fileUploader" name="myFileUpload"
class="sapUiSmallMarginEnd" uploadUrl="upload/" width="400px"
tooltip="Upload your file to the local server" uploadComplete="handleUploadComplete"
change="handleExcelUpload" placeholder="Please Select File" />
Controller part-
onInit: function() {
var oModel = new sap.ui.model.json.JSONModel();
this.getView().setModel(oModel);
sap.ui.getCore().setModel(oModel);
},
handleExcelUpload : function(e) {
this._import(e.getParameter("files")
&& e.getParameter("files")[0]);
},
_import : function(file) {
if (file && window.FileReader) {
var reader = new FileReader();
that = this;
//result = {};
//var data;
reader.onload = function(evt) {
var data = evt.target.result;
//var xlsx = XLSX.read(data, {type: 'binary'});
var arr = String.fromCharCode.apply(null, new Uint8Array(data));
var xlsx = XLSX.read(btoa(arr), {type: 'base64'});
result = xlsx.Strings;
result = {};
xlsx.SheetNames.forEach(function(sheetName) {
var rObjArr = XLSX.utils.sheet_to_row_object_array(xlsx.Sheets[sheetName]);
if(rObjArr.length > 0){
result[sheetName] = rObjArr;
}
});
return result;
that.b64toBlob(xlsx, "binary");
};
reader.readAsArrayBuffer(file);
};
},
b64toBlob : function(b64Data, contentType) {
contentType = contentType || '';
var sliceSize = 512;
b64Data = b64Data.replace(/^[^,]+,/, '');
b64Data = b64Data.replace(/\s/g, '');
var byteCharacters = Base64.decode(b64Data);
var byteArrays = [];
for (var offset = 0; offset < byteCharacters.length;offset += sliceSize){
var slice = byteCharacters.slice(offset, offset + sliceSize);
var byteNumbers = new Array(slice.length);
for (var i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
var byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
var blob = new Blob(byteArrays, {
type : contentType
});
}
index.htmt
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.8.0/xlsx.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.8.0/jszip.js"></script>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta http-equiv='Content-Type' content='text/html;charset=UTF-8'/>
<script src="resources/sap-ui-core.js"
id="sap-ui-bootstrap"
data-sap-ui-libs="sap.m"
data-sap-ui-theme="sap_bluecrystal">
</script>
<!-- only load the mobile lib "sap.m" and the "sap_bluecrystal" theme -->
<script>
sap.ui.localResources("excel_file2");
var app = new sap.m.App({initialPage:"idexcel_file1"});
var page = sap.ui.view({id:"idexcel_file1", viewName:"excel_file2.excel_file", type:sap.ui.core.mvc.ViewType.XML});
app.addPage(page);
app.placeAt("content");
</script>
<script src ="excel_file2/jszip.js"></script>
<script src ="excel_file2/xlsx.js"></script>
</head>
<body class="sapUiBody" role="application">
<div id="content"></div>
</body>
</html>
output screen
Note- i have added xlxs.js and jszip.js files
Regards,
Ayushi
Hi take a look at this,
I just checked the source code as jun wu said and tweaked a little
Hi Ayushi,
I shared the code in jsbin in my previous reply.try uploading .xlsx file in my code sample.
If you are not able to see this is my controller code
handleExcelUpload: function(e) {
var result = {};
rABS = false;//read as binary string
that = this;
var files = e.getParameter("files");
var f = files[0];
{
var reader = new FileReader();
var name = f.name;
reader.onload = function(e) {
if(typeof console !== 'undefined') console.log("onload", new Date(), rABS, false);
var data = e.target.result;
var wb;
if(rABS) {
wb = XLSX.read(data, {type: 'binary'});
} else {
var arr = that._fixdata(data);
wb = XLSX.read(btoa(arr), {type: 'base64'});
}
result = that._process_wb(wb);
alert(result);
};
if(rABS) reader.readAsBinaryString(f);
else reader.readAsArrayBuffer(f);
}
},
_process_wb:function(wb){
var output = "";
output = JSON.stringify(this._to_json(wb), 2, 2);
return output;
},
_to_json:function(workbook){
var result = {};
workbook.SheetNames.forEach(function(sheetName) {
var roa = XLSX.utils.sheet_to_row_object_array(workbook.Sheets[sheetName]);
if(roa.length > 0){
result[sheetName] = roa;
}
});
return result;
},
_fixdata:function(data){
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;
}
Regards
User | Count |
---|---|
84 | |
10 | |
9 | |
8 | |
6 | |
6 | |
6 | |
5 | |
3 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.