1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
| import { Table, Button } from "@alifd/next";
function App() {
const dataSource = [ { item: '棉鞋1', img: 'https://img.alicdn.com/imgextra/i3/O1CN01oa2oWk1THYOTjD8um_!!6000000002357-2-tps-110-110.png', sku: '34码', amount: 999, induction: { a: 10, b: 12 } }, { item: '棉鞋1', img: 'https://img.alicdn.com/imgextra/i2/O1CN010URR3q1qa5aKdpsOh_!!6000000005511-2-tps-110-110.png', sku: '35码', amount: 88, induction: { a: 23, b: 77 } }, { item: '拖鞋1', img: 'https://img.alicdn.com/imgextra/i4/O1CN01xUEUfU1TCyQuxXEps_!!6000000002347-2-tps-110-110.png', sku: '38码', amount: 0, induction: { a: 0, b: 100 } }, { item: '拖鞋2', img: 'https://img.alicdn.com/imgextra/i2/O1CN01wtSTlr28OmrVgPKQ9_!!6000000007923-2-tps-200-200.png', sku: '35码', amount: 123, induction: { a: 90, b: 8 } },
] const cellProps = (rowIndex, colIndex) => { if (rowIndex === 0 && colIndex === 0) { return { rowSpan: 2 }; }
};
const convertTableToExcel = ( id = undefined, filterHeader = false, ) => { return new Promise((resolve, reject) => { try { let htmlString = document.getElementById(id).innerHTML const filterDOM = (selector) => { var tempDiv = document.createElement('div'); tempDiv.innerHTML = htmlString; tempDiv.querySelectorAll(selector).forEach(function (element) { element.remove(); }); return tempDiv.innerHTML; } htmlString = filterDOM('.next-table-lock-left') if (filterHeader) { htmlString = filterDOM('thead') }
console.log(htmlString) const type = "text/plain"; const blob = new Blob([htmlString], { type }); const data = [new ClipboardItem({ [type]: blob })];
navigator.clipboard.write(data).then( () => resolve(), (error) => reject(error), ); } catch (error) { reject(error) } }) } return ( <div> <Button type="primary" onClick={() => convertTableToExcel('table1')} style={{ marginRight: 10 }}>复制</Button> <Button type="primary" onClick={() => convertTableToExcel('table1', true)}>复制(不含表头)</Button> <Table id="table1" dataSource={dataSource} cellProps={cellProps}> <Table.Column title="商品" width={300} dataIndex="item" lock="left" /> <Table.ColumnGroup title="信息"> <Table.Column title="规格" width={200} dataIndex="sku" /> {/* <Table.Column title="图片" width={200} dataIndex="img" cell={(val) => <img src={val} />} /> */} <Table.Column title="库存" width={200} dataIndex="amount" /> <Table.Column title="说明" width={200} dataIndex="induction" cell={(val) => { return <div> <div>全包:{val.a}%</div> <div>半包:{val.b}%</div> </div> }} />
</Table.ColumnGroup> </Table> </div> ) }
export default App
|