PHP code can insert image to excel file and open it correctly in MS Excel?

前端 未结 1 516
予麋鹿
予麋鹿 2020-12-06 03:57

I already have a document of excel and I want to use php to insert an image to that excel.

Is it possible to do that? How to implement it (code)?

Thanks,

相关标签:
1条回答
  • 2020-12-06 04:22
    $fileType = 'Excel2007';
    $fileName = 'test.xlsx';
    
    // Load the workbook
    $objPHPExcelReader = PHPExcel_IOFactory::createReader($fileType);
    $objPHPExcel = $objPHPExcelReader->load($fileName);
    
    // Add an image to the worksheet
    $objDrawing = new PHPExcel_Worksheet_Drawing();
    $objDrawing->setName('My Image');
    $objDrawing->setDescription('The Image that I am inserting');
    $objDrawing->setPath('./images/myImage.png');
    $objDrawing->setCoordinates('B2');
    $objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
    
    // Save the workbook
    $objPHPExcelWriter = PHPExcel_IOFactory::createWriter($objPHPExcel,$fileType);
    $objPHPExcelWriter->save($fileName);
    
    0 讨论(0)
提交回复
热议问题