gpt4 book ai didi

php - 如何从php中的Excel文件中删除所有合并的单元格?

转载 作者:行者123 更新时间:2023-12-04 20:50:31 30 4
gpt4 key购买 nike

我有一个这样的 Excel 文件:
enter image description here
我编写了一个代码,允许我从 xlsx 文件中提取所有照片并将它们插入到一个文件夹中。
文件中有几个合并的单元格,在处理之前,我想从 Excel 文件中删除所有合并的单元格。我试过 $objPHPExcel->setActiveSheetIndex("0")->unmergeCells();但它不起作用。
我该如何解决我的问题?

<?php

//uploaded xlsx file recovery
$xlsx="C:/wamp64/www/Extract_pictures_Excel/xlsx_files/".date('Y_m_d H-i-s')."_file.xlsx";
move_uploaded_file($_FILES["mon_fichier"]["tmp_name"],$xlsx);

require_once 'PHPExcel/Classes/PHPExcel/IOFactory.php';
$objPHPExcel = PHPExcel_IOFactory::load($xlsx);

//Unique name folder for the pictures
$dirname = uniqid();
mkdir("C:/wamp64/www/Extract_pictures_Excel/pictures_folders/$dirname/");

$sheet = $objPHPExcel->getActiveSheet();

//Unmerge all cells
foreach($sheet->getMergeCells() as $cells)
{
$sheet->unmergeCells($cells);
// var_dump($cells);
}

$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save('./unmerged_files/unmerged.xlsx');

//reading the xlsx file

foreach ($sheet->getDrawingCollection() as $drawing ) {


if ($drawing instanceof PHPExcel_Worksheet_MemoryDrawing) {
ob_start();
call_user_func(
$drawing->getRenderingFunction(),
$drawing->getImageResource()
);
$imageContents = ob_get_contents();
ob_end_clean();
switch ($drawing->getMimeType()) {
case PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_PNG :
$extension = 'png'; break;
case PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_GIF:
$extension = 'gif'; break;
case PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_JPEG :
$extension = 'jpg'; break;
}
} else {
$zipReader = fopen($drawing->getPath(),'r');
$imageContents = '';
while (!feof($zipReader)) {
$imageContents .= fread($zipReader,1024);
}
fclose($zipReader);
$extension = $drawing->getExtension();
$chemin = "C:/wamp64/www/Extract_pictures_Excel/pictures_folders/$dirname/";

}

//retrieving cell values for the images name
$row = (int) substr($drawing->getCoordinates(), 1);
//Condition to read merged cell
$stylecode = $sheet->getCell('H'.$row)->getValue();
$colorcode = $sheet->getCell('E'.$row)->getValue();
$finalname = $stylecode.'_'.$colorcode;
$myFileName = $chemin.$finalname.'.'.$extension;
file_put_contents($myFileName, $imageContents);
}
?>

最佳答案

我已将库更改为 PhpOffice\PhpSpreadsheet因为 PHPExcel 已经过时了。

require_once './vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

$xlsx = './test/catalogmtnwr2021202009210323241.xlsx';
$spreadsheet = IOFactory::load($xlsx);
$sheet = $spreadsheet->getActiveSheet();
现在我正在测试合并和取消合并的内容
echo 'before unmerge: ', count($sheet->getMergeCells()), PHP_EOL;

foreach($sheet->getMergeCells() as $cells)
$sheet->unmergeCells($cells);

echo 'after unmerge: ', count($sheet->getMergeCells()), PHP_EOL;

before unmerge: 678
after unmerge: 0


最后写回文件
$writer = new Xlsx($spreadsheet);
$writer->save('./test/unmerged.xlsx');
这按预期取消了单元格
Screenshot

关于php - 如何从php中的Excel文件中删除所有合并的单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64264766/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com