-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat 支持在 Web Worker 内使用导出方法,修改示例、utils 目录
- Loading branch information
qiang.bai
committed
Aug 13, 2024
1 parent
c2e776a
commit 1d52b25
Showing
18 changed files
with
529 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
# introduce | ||
## introduce | ||
Based on [xlsx](https://github.com/SheetJS/sheetjs) and [xlsx style vite](https://www.npmjs.com/package/xlsx-style-vite) Export to Excel | ||
|
||
## install | ||
``` bash | ||
npm install antd-xlsx | ||
``` | ||
|
||
## notice | ||
### notice | ||
1.The data format can be found in Antd [Table](https://ant.design/components/table-cn/ ) The columns and dataSource properties of the component. Or check the example |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import React, { useState, useEffect, useRef } from 'react' | ||
import { saveAs } from 'antd-xlsx' | ||
|
||
const columns = [ | ||
{ | ||
title: '序号', | ||
dataIndex: 'key', | ||
width: 50, | ||
}, | ||
{ | ||
title: 'Name', | ||
dataIndex: 'name', | ||
width: 160, | ||
|
||
// in Worker 不能传函数 | ||
render: ` | ||
(val, row, i) => { | ||
console.log(val, row, i) | ||
return 'formatted Name: ' + val | ||
} | ||
` | ||
}, | ||
{ | ||
title: 'Age', | ||
dataIndex: 'age', | ||
width: 50, | ||
|
||
// in Worker 不能传函数 | ||
onCell: ` | ||
(record, index) => { | ||
if (index === 0) { | ||
return { | ||
rowSpan: 6, | ||
} | ||
} | ||
if (index > 0 && index < 6) { | ||
return { | ||
rowSpan: 0, | ||
} | ||
} | ||
return {} | ||
} | ||
`, | ||
}, | ||
{ | ||
title: 'Company', | ||
dataIndex: 'Company', | ||
}, | ||
{ | ||
title: 'Gender', | ||
dataIndex: 'gender', | ||
width: 50, | ||
}, | ||
] | ||
|
||
const filename = 'basic.xlsx' | ||
|
||
export default function App() { | ||
const workerRef = useRef() | ||
const [dataSource] = useState(() => { | ||
const dataSource = [] | ||
for (let i = 0; i < 100; i++) { | ||
dataSource.push({ | ||
key: i + 1, | ||
name: i > 40 ? 'John' : 'Jack', | ||
age: i + 1, | ||
Company: 'SoftLake Co', | ||
gender: i == 3 ? '' : 'M', | ||
}) | ||
} | ||
return dataSource | ||
}) | ||
|
||
const onCountBtnClick = () => { | ||
const sheets = [ | ||
{ | ||
name: 'sheet', | ||
columns: columns, | ||
dataSource | ||
} | ||
] | ||
|
||
workerRef.current.postMessage({ | ||
type: 'blob', | ||
params: { | ||
sheets, | ||
filename | ||
} | ||
}) | ||
} | ||
|
||
useEffect(() => { | ||
if ('serviceWorker' in navigator) { | ||
const worker = new Worker(new URL('./worker.js', import.meta.url), { | ||
type: 'module' | ||
}) | ||
workerRef.current = worker | ||
|
||
worker.onmessage = function (event) { | ||
const { type, data } = event.data | ||
if (type === 'wbout') { | ||
saveAs( | ||
data, | ||
filename | ||
) | ||
} | ||
} | ||
} | ||
}, []) | ||
|
||
return ( | ||
<p> | ||
使用 web worker: | ||
<button onClick={onCountBtnClick}> | ||
work in Worker | ||
</button> | ||
</p> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// import style from 'antd-xlsx' | ||
import { nostyle } from 'antd-xlsx' | ||
|
||
onmessage = function (evt) { | ||
if (evt.data.type === 'blob') { | ||
const workbook = nostyle({ | ||
worker: true, | ||
...evt.data.params | ||
}) | ||
self.postMessage({ | ||
type: "wbout", | ||
data: workbook | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
const style = require('./style') | ||
const nostyle = require('./nostyle') | ||
const saveAs = require('./saveAs') | ||
|
||
module.exports = style | ||
exports.nostyle = nostyle | ||
exports.saveAs = saveAs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import style from './style.modern' | ||
import nostyle from './nostyle.modern' | ||
import saveAs from './saveAs.modern' | ||
|
||
export default style | ||
export { | ||
nostyle | ||
nostyle, | ||
saveAs | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,6 @@ | ||
import { write } from 'xlsx' | ||
import { getSheets, s2ab, saveAs } from './utils' | ||
import download from './utils' | ||
|
||
export default ({ | ||
sheets, | ||
filename = 'excel.xlsx', | ||
hiddenHeader = false, // 导出表格时,是否隐藏表头。默认显示表头 | ||
}) => { | ||
const Sheets = getSheets({ | ||
sheets, | ||
hiddenHeader, | ||
}) | ||
const workbook = { | ||
SheetNames: sheets.map(({ name }) => name), | ||
Sheets | ||
} | ||
const wbout = write(workbook, { | ||
bookType: 'xlsx', | ||
type: 'binary', | ||
}) | ||
saveAs( | ||
new Blob([s2ab(wbout)], { type: 'application/octet-stream' }), | ||
filename | ||
) | ||
} | ||
export default (args) => download(args, { | ||
write | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,8 @@ | ||
import { write } from 'xlsx-style-vite' | ||
import { getSheets, s2ab, saveAs } from './utils' | ||
import { getCellStyle } from './utils.style' | ||
import download from './utils' | ||
import getCellStyle from './utils/cellStyle' | ||
|
||
export default ({ | ||
sheets, | ||
filename = 'excel.xlsx', | ||
hiddenHeader = false, // 导出表格时,是否隐藏表头。默认显示表头 | ||
}) => { | ||
const Sheets = getSheets({ | ||
sheets, | ||
hiddenHeader, | ||
getCellStyle, | ||
}) | ||
const workbook = { | ||
SheetNames: sheets.map(({ name }) => name), | ||
Sheets | ||
} | ||
const wbout = write(workbook, { | ||
bookType: 'xlsx', | ||
type: 'binary', | ||
}) | ||
saveAs( | ||
new Blob([s2ab(wbout)], { type: 'application/octet-stream' }), | ||
filename | ||
) | ||
} | ||
export default (args) => download(args, { | ||
getCellStyle, | ||
write | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import getSheets from './sheets' | ||
import s2ab from './s2ab' | ||
import saveAs from './saveAs' | ||
|
||
export default ( | ||
{ | ||
sheets, | ||
filename = 'excel.xlsx', | ||
hiddenHeader = false, // 导出表格时,是否隐藏表头。默认显示表头 | ||
worker, | ||
}, | ||
{ | ||
getCellStyle, | ||
write, | ||
} | ||
) => { | ||
const Sheets = getSheets({ | ||
sheets, | ||
hiddenHeader, | ||
getCellStyle, | ||
}) | ||
const workbook = { | ||
SheetNames: sheets.map(({ name }) => name), | ||
Sheets | ||
} | ||
const wbout = write(workbook, { | ||
bookType: 'xlsx', | ||
type: 'binary', | ||
}) | ||
|
||
const blob = new Blob([s2ab(wbout)], { type: 'application/octet-stream' }) | ||
if (worker) { | ||
return blob | ||
} else { | ||
saveAs(blob, filename) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/** | ||
* 将 string 转成 arrayBuffer | ||
* | ||
* @param {string} s | ||
* @returns {ArrayBuffer} | ||
*/ | ||
export default function s2ab(s) { | ||
const buf = new ArrayBuffer(s.length), view = new Uint8Array(buf) | ||
for (let i = 0; i !== s.length; ++i) { | ||
view[i] = s.charCodeAt(i) & 0xFF | ||
} | ||
return buf | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** | ||
* 保存文件 | ||
* | ||
* @param {Blob} obj | ||
* @param {string} title | ||
*/ | ||
export default function saveAs(obj, title) { | ||
var link = document.createElement('a') | ||
link.download = title | ||
link.href = URL.createObjectURL(obj) | ||
link.click() | ||
link.remove() | ||
URL.revokeObjectURL(obj) | ||
} |
Oops, something went wrong.