Puja Sury
Puja Sury

Reputation: 174

set cell,title export to excel in codeigniter 2++

Dear all

I'm a newbie in PHP, and bad in English.

i have a controler

function download($id)
{
    $file_name = 'report1';
    $fieldnames ='Tanggal,Bulan,Block,Afdeling,Jumlah Panen Terakhir';
    $query = $this->main_model->get_report($id);
    to_excel($query,$file_name,$fieldnames,$judul);
}

this is my link/anchor

anchor("report/report/download/$id",'download', array('class' => 'excel'))

and this is my helper

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
function to_excel($query, $filename='exceloutput', $fieldnames)
{
 $columnnames = explode(",", $fieldnames); //better looking column names instead
 $headers = ''; // just creating the var for field headers to append to below
 $data = ''; // just creating the var for field data to append to below

 $obj =& get_instance();

 $fields = $query->field_data();
 if ($query->num_rows() == 0) {
      echo '<p>The table appears to have no data.</p>';
 } else {
       $i = 0;
      foreach ($fields as $field) {

         $headers .= $columnnames[$i]."\t";
         $i++;
      }

      foreach ($query->result() as $row) {
           $line = '';
           foreach($row as $value) {                                            
                if ((!isset($value)) OR ($value == "")) {
                     $value = "\t";
                } else {
                     $value = str_replace('"', '""', $value);
                     $value = '"' . $value . '"' . "\t";
                }
                $value = utf8_decode($value);
                $line .= $value;
           }
           $data .= trim($line)."\n";
      }

      $data = str_replace("\r","",$data);

      header("Content-type: application/x-msdownload");
      header("Content-Disposition: attachment; filename=$filename.xls");
      echo "$headers\n$data";  
 }
 } ?>

and this is my output

enter image description here

and the question is

the 'tanggal' mysql type is timestamp but when i drag resize the cell the value is show like this

enter image description here

so how to set large a cell in to_excel_helper?

and how to set title and border?

so it becomes like this

enter image description here

Thank you so much for your attention

this part make me stress since 1 week ago

so i need your help

Upvotes: 2

Views: 5252

Answers (1)

Hibiscus
Hibiscus

Reputation: 592

You're just outputting a tab-delimited file which Excel is able to read. It doesn't contain all the features of a "real" Excel file. As a result you don't have control over column width.

If you want more control over the Excel format I'd suggest using a library like PHPExcel ( http://phpexcel.codeplex.com/ ) which gives you a great deal of control over the formatting, sizing, colours, etc.

PHPExcel is pretty easy to integrate into CI. There's a wiki article about it here: https://github.com/EllisLab/CodeIgniter/wiki/PHPExcel

Upvotes: 1

Related Questions