|
@@ -3,26 +3,26 @@ import { ElMessage, ElTable } from 'element-plus'
|
|
|
import * as XLSX from 'xlsx'
|
|
|
import FileSaver from 'file-saver'
|
|
|
|
|
|
+type ElTableType = InstanceType<typeof ElTable>
|
|
|
interface ExportOptions {
|
|
|
- tableRef: Ref<InstanceType<typeof ElTable>>
|
|
|
+ table: ElTableType | Ref<ElTableType>
|
|
|
sheetName?: string
|
|
|
fileName?: string
|
|
|
headerRowNumber?: number
|
|
|
}
|
|
|
-
|
|
|
type ExportHandler = (option: ExportOptions) => void
|
|
|
|
|
|
export default function useTableExport() {
|
|
|
const exportToExcel: ExportHandler = ({
|
|
|
- tableRef,
|
|
|
+ table,
|
|
|
sheetName = 'sheet1',
|
|
|
fileName = '导出表格.xlsx',
|
|
|
headerRowNumber = 1,
|
|
|
}) => {
|
|
|
try {
|
|
|
- const tableNode = (tableRef.value.$el as HTMLElement).cloneNode(
|
|
|
- true
|
|
|
- ) as HTMLElement
|
|
|
+ const tableNode = (('$el' in table
|
|
|
+ ? table.$el
|
|
|
+ : table.value.$el) as HTMLElement).cloneNode(true) as HTMLElement
|
|
|
// 设置了列的fixed属性后会有两个table元素,导出数据会重复,需要去掉一个table
|
|
|
const fixedTable = tableNode.querySelector('.el-table__fixed')
|
|
|
fixedTable && tableNode.removeChild(fixedTable)
|