php 如何加载excel模板并在PHPExcel中写入?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18555487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to load excel template and write to it in PHPExcel?
提问by MJVM
How do I load an Excel Template with PHPExcel and write to its cells and also insert images to cells dynamically?
如何使用 PHPExcel 加载 Excel 模板并写入其单元格并将图像动态插入单元格?
回答by MJVM
You can read your excel template like this with PHPExcel:
您可以使用 PHPExcel 像这样读取您的 excel 模板:
$objPHPExcel = PHPExcel_IOFactory::load("./forms/english/cash.xlsx");
and you can write to cells like this:
您可以像这样写入单元格:
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A2', "No")
->setCellValue('B2', "Name")
->setCellValue('C2', "Email")
->setCellValue('D2', "Phone")
->setCellValue('E2', "Address");
回答by Haim Evgi
see the example, 30template.php in github site
参见示例,github 站点中的 30template.php
https://github.com/PHPOffice/PHPExcel/blob/develop/Examples/30template.php
https://github.com/PHPOffice/PHPExcel/blob/develop/Examples/30template.php
load template :
加载模板:
$objReader = PHPExcel_IOFactory::createReader('Excel5');
$objPHPExcel = $objReader->load("templates/30template.xls");
see in the example write via
在示例中看到写通过
$objPHPExcel->getActiveSheet()->setCellValue()
to add image use PHPExcel_Worksheet_Drawing :
添加图像使用 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());
回答by user3551026
Now you don't need load templates. Try to use the PHP Excel templator: https://github.com/alhimik1986/php-excel-templator
现在您不需要加载模板。尝试使用 PHP Excel 模板:https: //github.com/alhimik1986/php-excel-templator