我想从 excel 工作表中提取图像。请注意,图像不在任何单元格内,而是漂浮在我们的单元格顶部。 XLSX
xlsx-extract
或任何其他替代方案。在服务器端可能是可能的,但我的问题仍然是“可以在客户端完成吗?”
xlsx
直接拿不起来。我尝试使用
xlsx-extract
,它可以用于嵌入图像,但不能用于单元格外部的浮动图像。一种方法是
aspose.cells
,但它适用于服务器端
import fs from 'fs';
import path from 'path';
import ExcelJS from 'exceljs';
// Function to read and extract images from the XLSX file
async function readExcelFile(filePath) {
const workbook = new ExcelJS.Workbook();
await workbook.xlsx.readFile(filePath);
workbook.eachSheet((sheet, sheetId) => {
console.log(`Reading Sheet: ${sheet.name}`);
sheet.getImages().forEach((image) => {
const imageData = workbook.model.media[image.imageId];
const imageExtension = imageData.extension;
const imageBuffer = imageData.buffer;
console.log(imageBuffer);
// Extract row and column information
const topLeftCell = image.range.tl;
const bottomRightCell = image.range.br;
const topLeft = `Row: ${topLeftCell.row}, Column: ${topLeftCell.col}`;
const bottomRight = `Row: ${bottomRightCell.row}, Column: ${bottomRightCell.col}`;
console.log(`Image found at: Top-Left (${topLeft}), Bottom-Right (${bottomRight})`);
// Save image to file
const outputImagePath = path.join(
`output_image_${sheetId}_${topLeftCell.row}_${topLeftCell.col}.${imageExtension}`,
);
fs.writeFileSync(outputImagePath, imageBuffer);
console.log(`Saved image to ${outputImagePath}`);
});
});
}
// Path to the XLSX file
const filePath = './test.xlsx';
// Call the function to read the file
readExcelFile(filePath).catch(console.error);