如果要在导出的 Excel 中包含图片,可以使用 "xlsx-populate" 库和 "jszip" 库。以下是一个示例:
1. 首先,安装 "xlsx-populate" 和 "jszip" 库:
```
npm install xlsx-populate jszip --save
```
2. 然后,在组件中引入库:
```
import XlsxPopulate from "xlsx-populate";
import JSZip from "jszip";
```
3. 在组件中定义数据和图片:
```
const data = [
["Name", "Age", "Gender", "Image"],
["John Doe", 30, "Male", "image1"],
["Jane Smith", 25, "Female", "image2"],
["Bob Johnson", 50, "Male", "image3"]
];
const images = {
image1: "https://example.com/image1.jpg",
image2: "https://example.com/image2.jpg",
image3: "https://example.com/image3.jpg"
};
```
4. 在组件中添加导出按钮,并定义导出函数:
```
async function exportToExcel() {
const workbook = await XlsxPopulate.fromBlankAsync();
// 添加工作表
const sheet = workbook.addSheet("Sheet1");
// 添加表头
sheet.cell("A1").value("Name");
sheet.cell("B1").value("Age");
sheet.cell("C1").value("Gender");
sheet.cell("D1").value("Image");
// 添加数据和图片
data.forEach((row, rowIndex) => {
row.forEach((cell, cellIndex) => {
if (cellIndex === 3) {
// 添加图片
const img = new Image();
img.crossOrigin = "Anonymous";
img.src = images[cell];
const promise = new Promise((resolve, reject) => {
img.onload = () => {
resolve(img);
};
img.onerror = reject;
});
promise.then(img => {
const canvas = document.createElement("canvas");
canvas.width = img.width;
canvas.height = img.height;
const context = canvas.getContext("2d");
context.drawImage(img, 0, 0);
const base64 = canvas.toDataURL();
const imgData = base64.replace(/^data:image\/\w+;base64,/, "");
const imgBuffer = Buffer.from(imgData, "base64");
const zip = new JSZip();
const imgFile = zip.folder("images").file("image.png", imgBuffer, {
binary: true
});
const imgPath = `images/image.png`;
sheet.row(rowIndex + 1).cell(cellIndex + 1).value(imgPath);
resolve();
});
} else {
// 添加数据
sheet.row(rowIndex + 1).cell(cellIndex + 1).value(cell);
}
});
});
// 生成 Excel 文件并下载
const buffer = await workbook.outputAsync();
const blob = new Blob([buffer], { type: "application/octet-stream" });
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.download = "data.xlsx";
link.click();
URL.revokeObjectURL(url);
}
```
5. 最终的组件代码如下:
```
import React from "react";
import XlsxPopulate from "xlsx-populate";
import JSZip from "jszip";
const data = [
["Name", "Age", "Gender", "Image"],
["John Doe", 30, "Male", "image1"],
["Jane Smith", 25, "Female", "image2"],
["Bob Johnson", 50, "Male", "image3"]
];
const images = {
image1: "https://example.com/image1.jpg",
image2: "https://example.com/image2.jpg",
image3: "https://example.com/image3.jpg"
};
async function exportToExcel() {
const workbook = await XlsxPopulate.fromBlankAsync();
// 添加工作表
const sheet = workbook.addSheet("Sheet1");
// 添加表头
sheet.cell("A1").value("Name");
sheet.cell("B1").value("Age");
sheet.cell("C1").value("Gender");
sheet.cell("D1").value("Image");
// 添加数据和图片
data.forEach((row, rowIndex) => {
row.forEach((cell, cellIndex) => {
if (cellIndex === 3) {
// 添加图片
const img = new Image();
img.crossOrigin = "Anonymous";
img.src = images[cell];
const promise = new Promise((resolve, reject) => {
img.onload = () => {
resolve(img);
};
img.onerror = reject;
});
promise.then(img => {
const canvas = document.createElement("canvas");
canvas.width = img.width;
canvas.height = img.height;
const context = canvas.getContext("2d");
context.drawImage(img, 0, 0);
const base64 = canvas.toDataURL();
const imgData = base64.replace(/^data:image\/\w+;base64,/, "");
const imgBuffer = Buffer.from(imgData, "base64");
const zip = new JSZip();
const imgFile = zip.folder("images").file("image.png", imgBuffer, {
binary: true
});
const imgPath = `images/image.png`;
sheet.row(rowIndex + 1).cell(cellIndex + 1).value(imgPath);
resolve();
});
} else {
// 添加数据
sheet.row(rowIndex + 1).cell(cellIndex + 1).value(cell);
}
});
});
// 生成 Excel 文件并下载
const buffer = await workbook.outputAsync();
const blob = new Blob([buffer], { type: "application/octet-stream" });
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.download = "data.xlsx";
link.click();
URL.revokeObjectURL(url);
}
function ExportExcelWithImages() {
return <button onClick={exportToExcel}>Export to Excel</button>;
}
export default ExportExcelWithImages;
```
当用户点击 "Export to Excel" 按钮时,将会下载一个名为 "data.xlsx" 的文件,其中包含数据和图片。