日期:2014-05-17 浏览次数:20552 次
include 'Plugin/PHPExcel/Classes/PHPExcel/IOFactory.php';
class fill_template {
var $startrow = 0;
function __construct($fn) {
$this->tpl = PHPExcel_IOFactory::load($fn);
$this->target = clone $this->tpl;
}
function add_data($ar) {
$sheet = $this->tpl->getActiveSheet();
$i = 0;
$mcol = $sheet->getHighestColumn();
foreach($sheet->getRowDimensions() as $y=>$row) {
for($x='A'; $x<=$mcol; $x++) {
$txt = trim($sheet->getCell($x.$y)->getValue());
if($txt && preg_match('/{(.+)}/', $txt, $match)) {
$txt = isset($ar[$match[1]]) ? iconv('gbk', 'utf-8', $ar[$match[1]]) : '';
}
$h = $y + $this->startrow;
$this->target->getActiveSheet()->getCell("$x$h")->setValue($txt);
$this->target->getActiveSheet()->duplicateStyle($sheet->getStyle("$x$y"), "$x$h");
}
}
foreach($sheet->getMergeCells() as $merge) {
$merge = preg_replace('/\d+/e',"$0+$this->startrow", $merge);