|
@@ -1,4 +1,5 @@
|
|
import FileSaver from 'file-saver'
|
|
import FileSaver from 'file-saver'
|
|
|
|
+import request from '@/utils/request';
|
|
|
|
|
|
const Excel = require('exceljs')
|
|
const Excel = require('exceljs')
|
|
|
|
|
|
@@ -41,7 +42,7 @@ var setBorder = function (luckyBorderInfo, worksheet) {
|
|
// color: '#d0d4e3'
|
|
// color: '#d0d4e3'
|
|
// style: 1
|
|
// style: 1
|
|
// }
|
|
// }
|
|
- const {col_index, row_index} = elem.value
|
|
|
|
|
|
+ const { col_index, row_index } = elem.value
|
|
const borderData = Object.assign({}, elem.value)
|
|
const borderData = Object.assign({}, elem.value)
|
|
delete borderData.col_index
|
|
delete borderData.col_index
|
|
delete borderData.row_index
|
|
delete borderData.row_index
|
|
@@ -73,7 +74,7 @@ var setStyleAndValue = function (cellArr, worksheet) {
|
|
let value = ''
|
|
let value = ''
|
|
|
|
|
|
if (cell.f) {
|
|
if (cell.f) {
|
|
- value = {formula: cell.f, result: cell.v}
|
|
|
|
|
|
+ value = { formula: cell.f, result: cell.v }
|
|
} else if (!cell.v && cell.ct && cell.ct.s) {
|
|
} else if (!cell.v && cell.ct && cell.ct.s) {
|
|
// xls转为xlsx之后,内部存在不同的格式,都会进到富文本里,即值不存在与cell.v,而是存在于cell.ct.s之后
|
|
// xls转为xlsx之后,内部存在不同的格式,都会进到富文本里,即值不存在与cell.v,而是存在于cell.ct.s之后
|
|
// value = cell.ct.s[0].v
|
|
// value = cell.ct.s[0].v
|
|
@@ -130,7 +131,7 @@ const getImagePosition = function (num, arr) {
|
|
}
|
|
}
|
|
|
|
|
|
const getCellSize = function (table, worksheet, r, c) {
|
|
const getCellSize = function (table, worksheet, r, c) {
|
|
- const {cs, rs} = table.data[r][c].mc;
|
|
|
|
|
|
+ const { cs, rs } = table.data[r][c].mc;
|
|
const rowhidden = table.config.rowhidden && Object.keys(table.config.rowhidden) || '';
|
|
const rowhidden = table.config.rowhidden && Object.keys(table.config.rowhidden) || '';
|
|
const rowArr = [];
|
|
const rowArr = [];
|
|
for (let i = 0; i < rs; i++) {
|
|
for (let i = 0; i < rs; i++) {
|
|
@@ -182,8 +183,8 @@ var setImages = function (table, worksheet, workbook) {
|
|
imgWidth = systemPlatform() === "mac" ? size.w * 6.9 : (systemPlatform() === "win" ? size.w * 7.9 : size.w * 7.4);
|
|
imgWidth = systemPlatform() === "mac" ? size.w * 6.9 : (systemPlatform() === "win" ? size.w * 7.9 : size.w * 7.4);
|
|
imgHeight = size.h / 0.75;
|
|
imgHeight = size.h / 0.75;
|
|
cellConfig = {
|
|
cellConfig = {
|
|
- tl: {col: col_st, row: row_st},
|
|
|
|
- ext: {width: imgWidth, height: imgHeight}
|
|
|
|
|
|
+ tl: { col: col_st, row: row_st },
|
|
|
|
+ ext: { width: imgWidth, height: imgHeight }
|
|
}
|
|
}
|
|
// console.log("Image:Insert Mode");
|
|
// console.log("Image:Insert Mode");
|
|
} else {
|
|
} else {
|
|
@@ -198,8 +199,8 @@ var setImages = function (table, worksheet, workbook) {
|
|
let col_ed = getImagePosition(w_ed, visibledatacolumn);
|
|
let col_ed = getImagePosition(w_ed, visibledatacolumn);
|
|
let row_ed = getImagePosition(h_ed, visibledatarow);
|
|
let row_ed = getImagePosition(h_ed, visibledatarow);
|
|
cellConfig = {
|
|
cellConfig = {
|
|
- tl: {col: col_st, row: row_st},
|
|
|
|
- br: {col: col_ed, row: row_ed},
|
|
|
|
|
|
+ tl: { col: col_st, row: row_st },
|
|
|
|
+ br: { col: col_ed, row: row_ed },
|
|
}
|
|
}
|
|
// console.log("Image:Setting Mode");
|
|
// console.log("Image:Setting Mode");
|
|
}
|
|
}
|
|
@@ -215,7 +216,7 @@ var fillConvert = function (bg) {
|
|
const fill = {
|
|
const fill = {
|
|
type: 'pattern',
|
|
type: 'pattern',
|
|
pattern: 'solid',
|
|
pattern: 'solid',
|
|
- fgColor: {argb: bg.replace('#', '')}
|
|
|
|
|
|
+ fgColor: { argb: bg.replace('#', '') }
|
|
}
|
|
}
|
|
return fill
|
|
return fill
|
|
}
|
|
}
|
|
@@ -254,7 +255,7 @@ var fontConvert = function (
|
|
name: typeof ff === 'number' ? luckyToExcel[ff] : ff,
|
|
name: typeof ff === 'number' ? luckyToExcel[ff] : ff,
|
|
family: 1,
|
|
family: 1,
|
|
size: fs,
|
|
size: fs,
|
|
- color: {argb: fc.replace('#', '')},
|
|
|
|
|
|
+ color: { argb: fc.replace('#', '') },
|
|
bold: luckyToExcel.num2bl(bl),
|
|
bold: luckyToExcel.num2bl(bl),
|
|
italic: luckyToExcel.num2bl(it),
|
|
italic: luckyToExcel.num2bl(it),
|
|
underline: luckyToExcel.num2bl(ul),
|
|
underline: luckyToExcel.num2bl(ul),
|
|
@@ -342,7 +343,7 @@ var borderConvert = function (borderType, style = 1, color = '#000') {
|
|
}
|
|
}
|
|
const template = {
|
|
const template = {
|
|
style: luckyToExcel.style[style],
|
|
style: luckyToExcel.style[style],
|
|
- color: {argb: color.replace('#', '')}
|
|
|
|
|
|
+ color: { argb: color.replace('#', '') }
|
|
}
|
|
}
|
|
const border = {}
|
|
const border = {}
|
|
if (luckyToExcel.type[borderType] === 'all') {
|
|
if (luckyToExcel.type[borderType] === 'all') {
|
|
@@ -389,12 +390,12 @@ function addborderToCell(borders, rowIndex, colIndex) {
|
|
if (borders[bor].color.indexOf('rgb') === -1) {
|
|
if (borders[bor].color.indexOf('rgb') === -1) {
|
|
border[luckyExcel.type[bor]] = {
|
|
border[luckyExcel.type[bor]] = {
|
|
style: luckyExcel.style[borders[bor].style],
|
|
style: luckyExcel.style[borders[bor].style],
|
|
- color: {argb: borders[bor].color.replace('#', '')}
|
|
|
|
|
|
+ color: { argb: borders[bor].color.replace('#', '') }
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
border[luckyExcel.type[bor]] = {
|
|
border[luckyExcel.type[bor]] = {
|
|
style: luckyExcel.style[borders[bor].style],
|
|
style: luckyExcel.style[borders[bor].style],
|
|
- color: {argb: borders[bor].color}
|
|
|
|
|
|
+ color: { argb: borders[bor].color }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -448,3 +449,52 @@ export var exportExcel = function (luckysheet, value) {
|
|
})
|
|
})
|
|
return buffer
|
|
return buffer
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+export var exportPdf = function (luckysheet, value) {
|
|
|
|
+ // 1.创建工作簿,可以为工作簿添加属性
|
|
|
|
+ const workbook = new Excel.Workbook()
|
|
|
|
+ // 2.创建表格,第二个参数可以配置创建什么样的工作表
|
|
|
|
+ luckysheet.forEach(function (table) {
|
|
|
|
+ if (table.data.length === 0) return true
|
|
|
|
+ const worksheet = workbook.addWorksheet(table.name)
|
|
|
|
+ const merge = (table.config && table.config.merge) || {}
|
|
|
|
+ const borderInfo = (table.config && table.config.borderInfo) || {}
|
|
|
|
+ // 3.设置单元格合并,设置单元格边框,设置单元格样式,设置值,导出图片
|
|
|
|
+ setStyleAndValue(table.data, worksheet)
|
|
|
|
+ setMerge(merge, worksheet)
|
|
|
|
+ try {
|
|
|
|
+ setBorder(borderInfo, worksheet)
|
|
|
|
+ } catch (e) {
|
|
|
|
+ console.log(e)
|
|
|
|
+ }
|
|
|
|
+ setImages(table, worksheet, workbook)
|
|
|
|
+ return true
|
|
|
|
+ })
|
|
|
|
+
|
|
|
|
+ // 4.写入 buffer
|
|
|
|
+ const buffer = workbook.xlsx.writeBuffer().then(data => {
|
|
|
|
+ console.log(data)
|
|
|
|
+ const blob = new Blob([data], {
|
|
|
|
+ type: 'application/vnd.ms-excel;charset=utf-8'
|
|
|
|
+ })
|
|
|
|
+ let formData = new FormData();
|
|
|
|
+ // 第一个参数是后台接收的文件参数名,第二个参数是blob数据,第三个参数是文件名
|
|
|
|
+ formData.append('file', blob, `${value}.xlsx`);
|
|
|
|
+
|
|
|
|
+ // 发送ajax请求
|
|
|
|
+ request.post('/file/fileToPdfDownload', formData, {
|
|
|
|
+ headers: {
|
|
|
|
+ 'Content-Type': 'multipart/form-data'
|
|
|
|
+ },
|
|
|
|
+ }).then(response => {
|
|
|
|
+ if (response.data) {
|
|
|
|
+ const fileUrl = localStorage.getItem('SYS_HOST') + '/file/download?outPath=' + response.data.outPath + '&pdfFileName=' + response.data.pdfFileName
|
|
|
|
+ //执行下载文件
|
|
|
|
+ window.location.href = fileUrl
|
|
|
|
+ }
|
|
|
|
+ }).catch(error => {
|
|
|
|
+ // 处理错误
|
|
|
|
+ });
|
|
|
|
+ })
|
|
|
|
+ return buffer
|
|
|
|
+}
|