如何加载excel模板并在PHPExcel中写入?

时间:2013-09-01 05:16:18

标签: php templates phpexcel

如何使用PHPExcel加载Excel模板并写入其单元格并将图像动态插入单元格?

3 个答案:

答案 0 :(得分:15)

您可以使用PHPExcel读取您的Excel模板:

$objPHPExcel = PHPExcel_IOFactory::load("./forms/english/cash.xlsx");

你可以写这样的单元格:

  $objPHPExcel->setActiveSheetIndex(0)
                            ->setCellValue('A2', "No")
                            ->setCellValue('B2', "Name")
                            ->setCellValue('C2', "Email")
                            ->setCellValue('D2', "Phone")
                            ->setCellValue('E2', "Address");

答案 1 :(得分:7)

参见github网站上的示例,30template.php

https://github.com/PHPOffice/PHPExcel/blob/develop/Examples/30template.php

加载模板:

$objReader = PHPExcel_IOFactory::createReader('Excel5');

$objPHPExcel = $objReader->load("templates/30template.xls");

在示例中看到通过

$objPHPExcel->getActiveSheet()->setCellValue() 

添加图片使用PHPExcel_Worksheet_Drawing:

// 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());

答案 2 :(得分:0)

现在,您不需要加载模板。尝试使用PHP Excel模板: https://github.com/alhimik1986/php-excel-templator