vue之导出excel表格
<el-button type="primary" @click="exportBtn">{{$t('导出')}}</el-button>
import XLSX from 'xlsx'
exportBtn() {
let url = service.API_EXPORT_DATA.format(
this.keywordFormObjSuc.keyword
)
this.$http.get(url).then(({ data }) => {
this.loading = true;
let fileName = '基础信息维护表' + ".xlsx";
let wb = XLSX.utils.book_new();
let ws = XLSX.utils.json_to_sheet(
[
{
SPAREPART_CODE: this.$t("物料编码"),
SPAREPART_NAME: this.$t("物料名称"),
MODEL_CODE: this.$t("规格型号"),
QTY: this.$t("数量"),
SAFETY_STOCK_QTY: this.$t("安全库存"),
WORKCENTER_NAME: this.$t("仓库编码"),
WAREHOUSE_NAME: this.$t("仓库名称"),
UOM: this.$t("单位编码"),
},
],
{
skipHeader: true,
}
);
let temp = [];
data.forEach((element) => {
let p = {};
p.SPAREPART_CODE = element.SPAREPART_CODE;
p.SPAREPART_NAME = element.SPAREPART_NAME;
p.MODEL_CODE = element.MODEL_CODE;
p.QTY = element.QTY;
p.SAFETY_STOCK_QTY = element.SAFETY_STOCK_QTY;
p.WORKCENTER_NAME = element.WORKCENTER_NAME;
p.WAREHOUSE_NAME = element.WAREHOUSE_NAME
p.UOM = element.UOM;
temp.push(p);
});
XLSX.utils.sheet_add_json(ws, temp, {
header: [
"SPAREPART_CODE",
"SPAREPART_NAME",
"MODEL_CODE",
"QTY",
"SAFETY_STOCK_QTY",
"WORKCENTER_NAME",
"WAREHOUSE_NAME",
"UOM"
],
skipHeader: true,
origin: "A2",
});
ws["!cols"] = [];
for (var i = 0; i < 10; i++) {
ws["!cols"].push({
wpx: 120,
});
}
XLSX.utils.book_append_sheet(
wb,
ws,
this.$t("基础信息维护表")
);
XLSX.writeFile(wb, fileName);
this.loading = false;
this.$message.success(this.$t('导出成功'));
});
},
|