怎样将从数据库中提取的内容插入到 Excel模板中?

解决方案 »

  1.   

    用 PHPExcel 很容易的
    但是你要清晰的知道哪个字段的内容放到哪个单元格中
      

  2.   

    不好意思,因为是新手,对PHPExcel的用法还不是很了解,由于时间比较紧迫,
    麻烦能详细说明一下吗?可以给个简单的示例吗?
      

  3.   

    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) {
        $ar = array_values($ar);
    //    if(!isset($this->target)) $this->target = clone $this->tpl;
        $sheet = $this->tpl->getActiveSheet();
        $i = 0;
        foreach($sheet->getRowDimensions() as $y=>$row) {
          foreach($sheet->getColumnDimensions($row) as $x=>$col) {
            $txt = trim($sheet->getCell($x.$y)->getValue());
            if($txt && $txt{0} == '#') $txt = isset($ar[$i]) ? $ar[$i++] : '';
            $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);
          $this->target->getActiveSheet()->mergeCells($merge);
        }
        $this->startrow += $sheet->getHighestRow() + 3;//多加3行便于裁剪
      }
      function output($fn) {
        $t = PHPExcel_IOFactory::createWriter($this->target, 'Excel5');
        $t->save($fn);
      }
    }$p = new fill_template('tpl.xls');
    $p->add_data(array(1,2,3,4,5,6,7,8,9));
    $p->add_data(array(1,2,3,4,5,6,7,8,9, 'a汉字'));
    $p->add_data(array(1,2,3,4,5,6,7,8,9));
    $p->output('xxx.xls');1、下载 PHPExcel 并解包到你需要的地方,假定解包到 Plugin (插件)下
    2、将以上代码保存为 php 程序,就可测试了
    3、模板文件假定为 tpl.xls,其中在需要填写内容的单元格中都写有以“#”开头的识别串
    4、xxx.xls 为填写后的文件,自己规定
    5、add_data 方法是添加数据,你遍历查询结果时调用它就可以了。
      

  4.   


    大哥,我按你给的示例试了一下,期间修改了几个配置上的问题,但是到最后一步$p->output('xxx.xls');    出不来,错误提示:
    Fatal error: Uncaught exception 'PHPExcel_Exception' with message 'Test!U25 -> Test!U687 -> Test!U687 -> Cyclic Reference in Formula' in D:\www\a_com\test\testExcel\PHPExcel\Classes\PHPExcel\Cell.php on line 307 
    ( ! ) PHPExcel_Exception: Test!U25 -> Test!U687 -> Test!U687 -> Cyclic Reference in Formula in D:\www\a_com\test\testExcel\PHPExcel\Classes\PHPExcel\Cell.php on line 307 
    Call Stack 
    # Time      Memory   Function                                   Location 
    1 0.0006   361576    {main}( )                                  ..\testExcel.php:0 
    2 165.7018 114799344 fill_template->output( )                   ..\testExcel.php:58 
    3 165.7082 115688536 PHPExcel_Writer_Excel5->save( )            ..\testExcel.php:44 
    4 169.1487 120241712 PHPExcel_Writer_Excel5_Worksheet->close( ) ..\Excel5.php:194 
    5 172.0875 120936224 PHPExcel_Cell->getCalculatedValue( )      ..\Worksheet.php:460 
      

  5.   

    对不起,我只是前几日为解决这里的朋友的问题,才接触的 PHPExcel
    只会写,不会排错