admin管理员组文章数量:1391929
I am trying to read a .xlsx file in my SAPUI5 application using FileReader
api. The UI5 control that I am using to upload the file is sap.ui.unified.FileUploader
My requirement is to read the content of the Excel sheet and display its data in a table in my UI5 application.
For this, I create a FileReader
instance and call its .readAsBinaryString
or .readAsText
method and pass my file to it.
When the content is read, inside the onload event, the content of the file is displayed in an unreadable format (refer below)
Am I missing something? Or is there a different way of reading Excel data?
I am trying to read a .xlsx file in my SAPUI5 application using FileReader
api. The UI5 control that I am using to upload the file is sap.ui.unified.FileUploader
My requirement is to read the content of the Excel sheet and display its data in a table in my UI5 application.
For this, I create a FileReader
instance and call its .readAsBinaryString
or .readAsText
method and pass my file to it.
When the content is read, inside the onload event, the content of the file is displayed in an unreadable format (refer below)
Am I missing something? Or is there a different way of reading Excel data?
Share Improve this question asked Dec 13, 2019 at 12:08 d33ad33a 7501 gold badge17 silver badges42 bronze badges 1- 1 check this, you may get some idea youtube reference link – santhosh Commented Dec 13, 2019 at 12:38
2 Answers
Reset to default 3Thanks to mkysoft's and this answer. I was able to read the .xlsx file content.
For this, I had to create two files jszip.js
and xlsx.js
in my project. I created a separate folder for them and mentioned the two files' locations in sap.ui.define
section in my controller.
The two libraries are available at CDNJS - JSZip3 and CDNJS - XLSX4 locations.
sap.ui.define([
"sap/ui/core/mvc/Controller",
.... //other libraries
"projectnamespace/foldername/jszip",
"projectnamespace/foldername/xlsx"
], function (Controller,...., jszip, xlsx)
And then, to read the .xlsx
file I added the following code:
var oFileUploader = sap.ui.getCore().byId("fileUploader"); // get the sap.ui.unified.FileUploader
var file = oFileUploader.getFocusDomRef().files[0]; // get the file from the FileUploader control
if (file && window.FileReader) {
var reader = new FileReader();
reader.onload = function (e) {
var data = e.target.result;
var excelsheet = XLSX.read(data, {
type: "binary"
});
excelsheet.SheetNames.forEach(function (sheetName) {
var oExcelRow = XLSX.utils.sheet_to_row_object_array(excelsheet.Sheets[sheetName]); // this is the required data in Object format
var sJSONData = JSON.stringify(oExcelRow); // this is the required data in String format
});
};
reader.readAsBinaryString(file);
}
You need to parse Excel file in client side or server side. Excel files are not plain text. Old excel files (xls) can not parse without Excel, it has own binary format. But you can parse xlsx files, these are zipped multiple file which contains raw data, image and style files.
- You can use exsting js library for client parse, example post How to parse Excel file in Javascript/HTML5.
- You can use abap2xlsx library for ABAP backend.
本文标签: Issue in Reading Excel (xlsx) File Data in JavaScriptSAPUI5Stack Overflow
版权声明:本文标题:Issue in Reading Excel (xlsx) File Data in JavaScriptSAPUI5 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744686320a2619723.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论