1

I'm having a situation, that i get one template in excel, where i just want to add the information of my database, but when i do that put the template blank just with the information. Please tell me what i'm doing wrong. I'm new to PHPExcel

MODEL:

public function export_excel($id)
    {
        require_once APPPATH.'Classes/PHPExcel.php';

    $queryResult = $this->get($id);

    $objPHPExcel = new PHPExcel();

    $objPHPExcel->setActiveSheetIndex(0);  


    $result = mysql_query($queryResult[0]['query_sql']) or die (mysql_error());

    //echo "SQL = ",    $queryResult[0]['query_sql'];  // pass query allright

    // Initialise the Excel row number 
    $rowCount = $queryResult[0]['start_line'];
    //start of printing column names as names of MySQL fields  
    $column = $queryResult[0]['title_cell'];

    for ($i = 1; $i < mysql_num_fields($result); $i++)  
    {
        $objPHPExcel->getActiveSheet()->setCellValue($column.$rowCount, mysql_field_name($result,$i));
        $column++;
    }
    //end of adding column names  

    //start while loop to get data  
    $rowCount = $rowCount+1;  
    while($row = mysql_fetch_row($result))  
    {  
        $column = 'B';
        for($j=1; $j<mysql_num_fields($result);$j++)  
        {  
            if(!isset($row[$j]))  
                $value = NULL;  
            elseif ($row[$j] != "")  
                $value = strip_tags($row[$j]);  
            else  
                $value = "";  
            $objPHPExcel->getActiveSheet()->getColumnDimension($column)->setAutoSize(true);
            $objPHPExcel->getActiveSheet()->setCellValue($column.$rowCount, $value);
            $column++;
        }  
        $rowCount++;
    }

    $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel); 
    //$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');


    // Write the Excel file to filename some_excel_file.xlsx in the current directory
    if($queryResult[0]['template_location'] !=null){
        $objWriter->save($queryResult[0]['template_location']); 
        //echo date('H:i:s') , " Write to Excel2007 format" , EOL;
        return 1;
    }

    else{
        $objWriter->save('php://output');
        //echo date('H:i:s') , " Write to Excel2007 format" , EOL;
        return 1;
    }

    return null;

}
Narf
  • 14,600
  • 3
  • 37
  • 66
user3617397
  • 74
  • 1
  • 6
  • Where is the template that you want to add database information to? You're not loading any template in this code – Mark Baker May 15 '15 at 07:03
  • its true, I just resolved the problem now, cause i was not reading, i was just writting, that was my problem. thx – user3617397 May 15 '15 at 10:08

1 Answers1

0

You're not reading the template, you need to read first from the template and write into:

something like this: $objReader = PHPExcel_IOFactory::createReader('Excel5'); $objPHPExcel = $objReader->load($queryResult[0]['template_location']);

i hope it helped

Ricardo Origin
  • 175
  • 3
  • 14