PhpSpreadsheet 清除现有单元格或范围内容

时间:2021-07-09 10:05:15

标签: phpspreadsheet phpoffice-phpspreadsheet

我已成功从 mysql 表中以所需格式提取数据。但是在使用mysql数据之前,我想清除上传格式excel表中的现有内容。我试过搜索/阅读很多文章/官方文档,但没有找到合适的。

请求帮助。

Excel table

<?php
require ROOT.'vendor/autoload.php';

//specify upload file name
$inputFileName = ROOT.'upload_format.xlsx';

// Set sheetname
$sheetname = 'Instructions';

// Load $inputFileName to a Spreadsheet object
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($inputFileName);

//Set Active sheet by sheet Name under $sheet object
$sheet = $spreadsheet->getSheetByName($sheetname);

$q1="select 
    id,
    status as value
from
    xyz
where
    id = '51'";
$q1r = $dbc->get_results($q1);

//merge header row
$sheet->mergeCells('D15:E15');

// Set data table header
$sheet->getCellByColumnAndRow(4, 15)->setValue('Your Status');

// create heading
$sheet->getCellByColumnAndRow(4, 16)->setValue('Description');
$sheet->getCellByColumnAndRow(5, 16)->setValue('Mention');

// loop through actual data
if(count($q1r)>0){
    $i = 17;
    foreach($q1r as $row) {
        $sheet->getCellByColumnAndRow(4, $i)->setValue($row['value']);
        $sheet->getCellByColumnAndRow(5, $i)->setValue($row['id']);
        $i = $i + 1;
    }
}else{
    //merge cell to display error message
    $sheet->mergeCells('D17:E17');
    //set error message
    $sheet->getCellByColumnAndRow(4, 17)->setValue('No records available');
}

//write it again to Filesystem with the same name
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, "Xlsx");
$writer->save($inputFileName);

0 个答案:

没有答案
相关问题