乐于分享
好东西不私藏

Fiori&UI5 前端数据导出到EXCEL

Fiori&UI5 前端数据导出到EXCEL

1.前端导出数据到EXCEL

1.1效果展示

点击下载,实现将下面Table的数据导出到excel

打开

导出的字段可以在代码中自选

1.2代码实现

view.xml :

<Buttontext="下载"icon="sap-icon://download"press="handleDownload"/>

Controller.js 

handleDownload : function(){

var oController = this;

var JSONModel = this.getView().getModel();

var localData = JSONModel.getData();

oController.exportExcle(localData.result_seldata);//result_seldata为要导出的json数组,即列表显示的数据

},

exportExcle : function(h2i) {

var oController = this;

var oDialog = this.getView().byId("BusyDialog");

oDialog.open();

if (h2i===undefined){

oDialog.close();

sap.m.MessageToast.show("请先查询");

return;

}else{

if (h2i.length < 1) {

oDialog.close();

sap.m.MessageToast.show("没有数据");

return;

}else{

jQuery.sap.delayedCall(1000, thisfunction () {

oDialog.close();

});

};

};

var aExcel = [];

var sFileName = "问题清单列表" + "_"

+ js.formatters.DataFormater(new Date());

aExcel.push("\n");

aExcel.push("问题编号""项目描述""问题描述""优先级",

"处理状态""问题类型""拉线""工序""发生日期",

"提报人","联系方式""提报日期""提报时间""解决人",

"联系方式","解决方案","解决日期");

for (var i = 0; i < h2i.length; i++) {

aExcel.push("\n");

var lv_date = js.formatters.DateFormater1(h2i[i].Zsjwcdate);

if (lv_date ==='9999-12-31'){

lv_date = "";

};

var Zsolution = h2i[i].Zsolution;

Zsolution = Zsolution.replace(/[\r\n]/g,"");//去掉回车换行

aExcel.push(h2i[i].Zissueid, h2i[i].Zprojecttxt,

h2i[i].Zdespt, js.formatters.LevelText(h2i[i].Zpriorityid),

js.formatters.StatusText(h2i[i].Zstatusid), js.formatters.TypeText(h2i[i].Ztypeid), h2i[i].Zlaxiantxt,

h2i[i].Zgxtxt,js.formatters.DateFormater1(h2i[i].Zhappendate),

h2i[i].Zreportname, h2i[i].Zphone, 

js.formatters.DateFormater1(h2i[i].Erdat),

js.formatters.TimeFormater1(h2i[i].Erzet),

h2i[i].Zsolvename, h2i[i].Zsphone,Zsolution,

lv_date);

}

jQuery.sap.require("sap.ui.core.util.File");

sap.ui.core.util.File.save(aExcel, sFileName, "csv","application/vnd.ms-excel","utf-8");//设置UTF-8编码

oDialog.close();

return;

},

BusyDialog.fragment.xml :

<core:FragmentDefinition

xmlns="sap.m"

xmlns:core="sap.ui.core">

<BusyDialogid="BusyDialog"/>

1.3不足

导出excel的时候,用aExcel.push("\n");输出换行符的时候会自动带出一个空格。后续改进需要将空格去除。