

useExcelExporter hook function. The config object allows you to quickly configure the relevant information for Excel export, thus enabling Excel generation. This library is suitable for work order generation, batch export of year-end data, and more.npm i tango-excel-cw exceljs file-saverimport { useExcelExporter } from "tango-excel-cw";// react
import { useExcelExporter } from "tango-excel-cw";
import { MaterialButton } from "tango-ui-cw";
import Tree from "@/assets/yourimg.jpg";
import DemoExcel from "@/assets/youremptyexcel.xlsx";
function Demo() {
const { exportExcel } = useExcelExporter();
const config = {
data: {
id: 1,
treenumber: "110C1892",
treekind: "poplar",
},
fileName: "export.xlsx",
template: DemoExcel,
cellMap: {
A3: "id",
B3: "treenumber",
C3: "treekind",
},
startRow: 2,
image: {
url: Tree,
range: {
tl: { col: 6, row: 2 },
br: { col: 7, row: 3 },
},
},
onBeforeExport: () => console.log("prepare to export..."),
onSuccess: () => console.log("export successful!"),
onError: (err) => console.error("export failure", err),
};
function ExportExcelDemo() {
exportExcel(config);
}
return (
<>
<MaterialButton onClick={ExportExcelDemo}>Export to Excel</MaterialButton>
</>
);
}
export default Demo;
id | treenumber | treekind | treelevel | location | staff | action |
|---|---|---|---|---|---|---|
1 | 110C1892 | poplar | twond level | Lama Temple | ClayW |
startRow parameter to control the starting insertion position of the data cannot fill the 'table export time' data into the first row of the table. Therefore, you can combine the cellMapStatic parameter to achieve this requirement. The sample code is as follows:
// traverse the form data finalDatas requested
const dataRows = finalDatas.map(item => ({
date: item.time,
location: item.location,
treekind: item.treekind,
treenumber: item.treenumber,
abnormal: item.abnormal,
staff: item.staff,
range: rangeStr // table export time
}));
// configure parameters for exporting to Excel
const config = {
data: dataRows, // data sources
fileName: export.xlsx, // export file name
template: EmptyTable, // empty demo excel
startRow: 3, // the starting row for data insertion
cellMap: { // cell map configuration
A: "date",
B: "location",
C: "treekind",
D: "treenumber",
E: "abnormal",
F: "staff"
},
cellMapStatic: { // accurate mapping of static data (not affected by startRow)
F1: "range" // insert 'table export time' into the specified position
},
};
Function | Type | Accepted Values | Description | Default |
|---|---|---|---|---|
exportExcel | Object | { data, fileName, template, cellMap, startRow, image... } | export configuration items, including data source, column definition, file name, etc. | - |
Prop | Type | Accepted Values | Description | Default | Verson |
|---|---|---|---|---|---|
data | Object | Object | excel data source | - | - |
fileName | String | String | exported excel name | export.xlsx | - |
template | String | String | empty demo | - | - |
cellMap | Object | Object | excel table map configuration | - | - |
startRow | Number | Number | starting row for insertion | 1 | - |
image | Object | Object | configuration when inserting pictures | - | - |
onBeforeExport | () => void | () => console.log('prepare to export...') | the callback function before exporting, triggered before the export operation starts | - | - |
onSuccess | () => void | () => console.log('export successful!') | export successful callback function, triggered after the export operation is completed | - | - |
onError | () => void | () => console.log('export failure', err) | export failure callback function, triggered when the export operation fails | - | - |
cellMapStatic | Object | Object | precisely control the insertion position when inserting static data | - | 0.0.3 |
wrapText | Array<String> | ['B2','H1:H20'] | setting wrap text | - | 0.1.1 |
alignment | Array<Array> | Array<[String, Object]> | single control:['B2',{ vertical: 'middle', horizontal: 'left' }], batch control:[['B2',{ vertical: 'middle', horizontal: 'left' }], ['C2',{ vertical: 'middle', horizontal: 'left' }]] vertical values:top/middle/bottom, horizontal values:left/center/right | set cell alignment | - | 0.2.0 |
Prop | Type | Accepted Values | Description | Default | Verson |
|---|---|---|---|---|---|
url | String | String | img src | - | - |
range | Object | Object | coordinate object control | - | - |
keepOriginalSize | Boolean | Boolean | Enable the image to maintain its original proportions within the excel grid | false | 0.0.4 |
Prop | Type | Accepted Values | Description | Default | Verson |
|---|---|---|---|---|---|
tl | Object | Object | The image is located at the top-left corner coordinates, with vertex { col: 0, row: 0 } (top-left corner of A1). | - | |
br | Object | Object | The image is located at the bottom right corner, with vertex {col: 1, row: 1} (bottom right corner of A1). | - |

Prop | Type | Accepted Values | Description | Default | Verson |
|---|---|---|---|---|---|
col | Number | Number | column | 0 | |
row | Number | Number | row | 0 | |
offset | Number | Number | offset value, used to control the position of the image in the cell | 0 |