Backend API
import React, { useCallback, useRef } from 'react';
import { saveAs } from 'file-saver-es';
import Button from 'devextreme-react/button';
import TabPanel, { Item } from 'devextreme-react/tab-panel';
import DataGrid, { Column } from 'devextreme-react/data-grid';
import type { DataGridRef } from 'devextreme-react/data-grid';
import { ArrayStore } from 'devextreme-react/common/data';
import type { DataSourceOptions } from 'devextreme-react/common/data';
import { Workbook } from 'devextreme-exceljs-fork';
import { exportDataGrid } from 'devextreme-react/common/export/excel';
import type { DataGridCell } from 'devextreme-react/common/export/excel';
import { products } from './data.ts';
import type { ExcelCell, CustomizeCellOptions } from './data.ts';
const priceDataSource: DataSourceOptions = {
store: new ArrayStore({
data: products,
key: 'Product_ID',
}),
select: ['Product_ID', 'Product_Name', 'Product_Sale_Price', 'Product_Retail_Price'],
filter: ['Product_ID', '<', 10],
};
const ratingDataSource: DataSourceOptions = {
store: new ArrayStore({
data: products,
key: 'Product_ID',
}),
select: ['Product_ID', 'Product_Name', 'Product_Consumer_Rating', 'Product_Category'],
filter: ['Product_ID', '<', 10],
};
const setAlternatingRowsBackground = (gridCell?: DataGridCell, excelCell?: ExcelCell) => {
if (gridCell?.rowType === 'header' || gridCell?.rowType === 'data') {
if (excelCell && excelCell.fullAddress.row % 2 === 0) {
excelCell.fill = {
type: 'pattern', pattern: 'solid', fgColor: { argb: 'D3D3D3' }, bgColor: { argb: 'D3D3D3' },
};
}
}
};
const App = () => {
const priceGridRef = useRef<DataGridRef>(null);
const ratingGridRef = useRef<DataGridRef>(null);
const exportGrids = useCallback(() => {
const workbook = new Workbook();
const priceSheet = workbook.addWorksheet('Price');
const ratingSheet = workbook.addWorksheet('Rating');
priceSheet.getRow(2).getCell(2).value = 'Price';
priceSheet.getRow(2).getCell(2).font = { bold: true, size: 16, underline: 'double' };
ratingSheet.getRow(2).getCell(2).value = 'Rating';
ratingSheet.getRow(2).getCell(2).font = { bold: true, size: 16, underline: 'double' };
exportDataGrid({
worksheet: priceSheet,
component: priceGridRef.current?.instance(),
topLeftCell: { row: 4, column: 2 },
customizeCell: ({ gridCell, excelCell }: CustomizeCellOptions) => {
setAlternatingRowsBackground(gridCell, excelCell);
},
}).then(() => exportDataGrid({
worksheet: ratingSheet,
component: ratingGridRef.current?.instance(),
topLeftCell: { row: 4, column: 2 },
customizeCell: ({ gridCell, excelCell }) => {
setAlternatingRowsBackground(gridCell, excelCell);
},
})).then(() => {
workbook.xlsx.writeBuffer().then((buffer) => {
saveAs(new Blob([buffer], { type: 'application/octet-stream' }), 'MultipleGrids.xlsx');
});
});
}, []);
return (
<div>
<div id="exportContainer">
<Button
text="Export multiple grids"
icon="xlsxfile"
onClick={exportGrids}
/>
</div>
<TabPanel id="tabPanel" deferRendering={false}>
<Item title="Price">
<DataGrid
id="priceDataGrid"
ref={priceGridRef}
dataSource={priceDataSource}
showBorders={true}
rowAlternationEnabled={true}
>
<Column dataField="Product_ID" caption="ID" width={50} />
<Column dataField="Product_Name" caption="Name" />
<Column dataField="Product_Sale_Price" caption="Sale Price" dataType="number" format="currency" />
<Column dataField="Product_Retail_Price" caption="Retail Price" dataType="number" format="currency" />
</DataGrid>
</Item>
<Item title="Rating">
<DataGrid
id="ratingDataGrid"
ref={ratingGridRef}
dataSource={ratingDataSource}
showBorders={true}
rowAlternationEnabled={true}
>
<Column dataField="Product_ID" caption="ID" width={50} />
<Column dataField="Product_Name" caption="Name" />
<Column dataField="Product_Consumer_Rating" caption="Rating" />
<Column dataField="Product_Category" caption="Category" />
</DataGrid>
</Item>
</TabPanel>
</div>
);
};
export default App;
import React, { useCallback, useRef } from 'react';
import { saveAs } from 'file-saver-es';
import Button from 'devextreme-react/button';
import TabPanel, { Item } from 'devextreme-react/tab-panel';
import DataGrid, { Column } from 'devextreme-react/data-grid';
import { ArrayStore } from 'devextreme-react/common/data';
import { Workbook } from 'devextreme-exceljs-fork';
import { exportDataGrid } from 'devextreme-react/common/export/excel';
import { products } from './data.js';
const priceDataSource = {
store: new ArrayStore({
data: products,
key: 'Product_ID',
}),
select: ['Product_ID', 'Product_Name', 'Product_Sale_Price', 'Product_Retail_Price'],
filter: ['Product_ID', '<', 10],
};
const ratingDataSource = {
store: new ArrayStore({
data: products,
key: 'Product_ID',
}),
select: ['Product_ID', 'Product_Name', 'Product_Consumer_Rating', 'Product_Category'],
filter: ['Product_ID', '<', 10],
};
const setAlternatingRowsBackground = (gridCell, excelCell) => {
if (gridCell?.rowType === 'header' || gridCell?.rowType === 'data') {
if (excelCell && excelCell.fullAddress.row % 2 === 0) {
excelCell.fill = {
type: 'pattern',
pattern: 'solid',
fgColor: { argb: 'D3D3D3' },
bgColor: { argb: 'D3D3D3' },
};
}
}
};
const App = () => {
const priceGridRef = useRef(null);
const ratingGridRef = useRef(null);
const exportGrids = useCallback(() => {
const workbook = new Workbook();
const priceSheet = workbook.addWorksheet('Price');
const ratingSheet = workbook.addWorksheet('Rating');
priceSheet.getRow(2).getCell(2).value = 'Price';
priceSheet.getRow(2).getCell(2).font = { bold: true, size: 16, underline: 'double' };
ratingSheet.getRow(2).getCell(2).value = 'Rating';
ratingSheet.getRow(2).getCell(2).font = { bold: true, size: 16, underline: 'double' };
exportDataGrid({
worksheet: priceSheet,
component: priceGridRef.current?.instance(),
topLeftCell: { row: 4, column: 2 },
customizeCell: ({ gridCell, excelCell }) => {
setAlternatingRowsBackground(gridCell, excelCell);
},
})
.then(() =>
exportDataGrid({
worksheet: ratingSheet,
component: ratingGridRef.current?.instance(),
topLeftCell: { row: 4, column: 2 },
customizeCell: ({ gridCell, excelCell }) => {
setAlternatingRowsBackground(gridCell, excelCell);
},
}),
)
.then(() => {
workbook.xlsx.writeBuffer().then((buffer) => {
saveAs(new Blob([buffer], { type: 'application/octet-stream' }), 'MultipleGrids.xlsx');
});
});
}, []);
return (
<div>
<div id="exportContainer">
<Button
text="Export multiple grids"
icon="xlsxfile"
onClick={exportGrids}
/>
</div>
<TabPanel
id="tabPanel"
deferRendering={false}
>
<Item title="Price">
<DataGrid
id="priceDataGrid"
ref={priceGridRef}
dataSource={priceDataSource}
showBorders={true}
rowAlternationEnabled={true}
>
<Column
dataField="Product_ID"
caption="ID"
width={50}
/>
<Column
dataField="Product_Name"
caption="Name"
/>
<Column
dataField="Product_Sale_Price"
caption="Sale Price"
dataType="number"
format="currency"
/>
<Column
dataField="Product_Retail_Price"
caption="Retail Price"
dataType="number"
format="currency"
/>
</DataGrid>
</Item>
<Item title="Rating">
<DataGrid
id="ratingDataGrid"
ref={ratingGridRef}
dataSource={ratingDataSource}
showBorders={true}
rowAlternationEnabled={true}
>
<Column
dataField="Product_ID"
caption="ID"
width={50}
/>
<Column
dataField="Product_Name"
caption="Name"
/>
<Column
dataField="Product_Consumer_Rating"
caption="Rating"
/>
<Column
dataField="Product_Category"
caption="Category"
/>
</DataGrid>
</Item>
</TabPanel>
</div>
);
};
export default App;
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App.tsx';
ReactDOM.render(
<App />,
document.getElementById('app'),
);
import type { DataGridCell } from 'devextreme-react/common/export/excel';
export interface Product {
Product_ID: number;
Product_Name: string;
Product_Sale_Price: number;
Product_Retail_Price: number;
Product_Consumer_Rating: number;
Product_Category: string;
}
export interface ExcelCell {
fullAddress: { row: number };
fill: any;
}
export interface CustomizeCellOptions {
gridCell?: DataGridCell;
excelCell?: ExcelCell;
}
export const products: Product[] = [
{
Product_ID: 1,
Product_Name: "HD Video Player",
Product_Sale_Price: 220.00,
Product_Retail_Price: 330.00,
Product_Consumer_Rating: 4,
Product_Category: "Video Players",
},
{
Product_ID: 2,
Product_Name: "SuperHD Video Player",
Product_Sale_Price: 275.00,
Product_Retail_Price: 400.00,
Product_Consumer_Rating: 4,
Product_Category: "Video Players",
},
{
Product_ID: 3,
Product_Name: "SuperPlasma 50",
Product_Sale_Price: 1800.00,
Product_Retail_Price: 2400.00,
Product_Consumer_Rating: 4.5,
Product_Category: "Televisions",
},
{
Product_ID: 4,
Product_Name: "SuperLED 50",
Product_Sale_Price: 1100.00,
Product_Retail_Price: 1600.00,
Product_Consumer_Rating: 5,
Product_Category: "Televisions",
},
{
Product_ID: 5,
Product_Name: "SuperLED 42",
Product_Sale_Price: 1050.00,
Product_Retail_Price: 1450.00,
Product_Consumer_Rating: 5,
Product_Category: "Televisions",
},
{
Product_ID: 6,
Product_Name: "SuperLCD 55",
Product_Sale_Price: 1045.00,
Product_Retail_Price: 1350.00,
Product_Consumer_Rating: 4.5,
Product_Category: "Televisions",
},
{
Product_ID: 7,
Product_Name: "SuperLCD 42",
Product_Sale_Price: 999.00,
Product_Retail_Price: 1200.00,
Product_Consumer_Rating: 4,
Product_Category: "Televisions",
},
{
Product_ID: 8,
Product_Name: "SuperPlasma 65",
Product_Sale_Price: 2900.00,
Product_Retail_Price: 3500.00,
Product_Consumer_Rating: 3,
Product_Category: "Televisions",
},
{
Product_ID: 9,
Product_Name: "SuperLCD 70",
Product_Sale_Price: 3200.00,
Product_Retail_Price: 4000.00,
Product_Consumer_Rating: 4,
Product_Category: "Televisions",
},
];
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App.js';
ReactDOM.render(<App />, document.getElementById('app'));
export const products = [
{
Product_ID: 1,
Product_Name: 'HD Video Player',
Product_Sale_Price: 220.0,
Product_Retail_Price: 330.0,
Product_Consumer_Rating: 4,
Product_Category: 'Video Players',
},
{
Product_ID: 2,
Product_Name: 'SuperHD Video Player',
Product_Sale_Price: 275.0,
Product_Retail_Price: 400.0,
Product_Consumer_Rating: 4,
Product_Category: 'Video Players',
},
{
Product_ID: 3,
Product_Name: 'SuperPlasma 50',
Product_Sale_Price: 1800.0,
Product_Retail_Price: 2400.0,
Product_Consumer_Rating: 4.5,
Product_Category: 'Televisions',
},
{
Product_ID: 4,
Product_Name: 'SuperLED 50',
Product_Sale_Price: 1100.0,
Product_Retail_Price: 1600.0,
Product_Consumer_Rating: 5,
Product_Category: 'Televisions',
},
{
Product_ID: 5,
Product_Name: 'SuperLED 42',
Product_Sale_Price: 1050.0,
Product_Retail_Price: 1450.0,
Product_Consumer_Rating: 5,
Product_Category: 'Televisions',
},
{
Product_ID: 6,
Product_Name: 'SuperLCD 55',
Product_Sale_Price: 1045.0,
Product_Retail_Price: 1350.0,
Product_Consumer_Rating: 4.5,
Product_Category: 'Televisions',
},
{
Product_ID: 7,
Product_Name: 'SuperLCD 42',
Product_Sale_Price: 999.0,
Product_Retail_Price: 1200.0,
Product_Consumer_Rating: 4,
Product_Category: 'Televisions',
},
{
Product_ID: 8,
Product_Name: 'SuperPlasma 65',
Product_Sale_Price: 2900.0,
Product_Retail_Price: 3500.0,
Product_Consumer_Rating: 3,
Product_Category: 'Televisions',
},
{
Product_ID: 9,
Product_Name: 'SuperLCD 70',
Product_Sale_Price: 3200.0,
Product_Retail_Price: 4000.0,
Product_Consumer_Rating: 4,
Product_Category: 'Televisions',
},
];
<!DOCTYPE html>
<html lang="en">
<head>
<title>DevExtreme Demo</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0" />
<link rel="stylesheet" type="text/css" href="https://cdn3.devexpress.com/jslib/26.1.5/css/dx.light.css" />
<link rel="stylesheet" type="text/css" href="./bundle.css" />
</head>
<body class="dx-viewport">
<div class="demo-container">
<div id="app"></div>
</div>
<script src="bundles/vendor/react.vendor.js"></script>
<script src="./bundle.js"></script>
</body>
</html>
#priceDataGrid,
#ratingDataGrid {
padding: 10px;
}
#exportContainer {
text-align: right;
}
#tabPanel {
padding-top: 10px;
}