mirror of
https://github.com/bcit-ci/CodeIgniter.git
synced 2025-02-20 11:13:29 +08:00
Updated PHP Excel Class (markdown)
parent
0638045694
commit
2c5eab18bd
@ -10,9 +10,9 @@ I know there's a lot of classes that do the same thing, but I had no success wit
|
||||
|
||||
If you don't want to use the CodeIgniter way (why and who doesn't want to use it?), you can code withing your Controller:
|
||||
|
||||
`
|
||||
function file_xls()
|
||||
{
|
||||
|
||||
function file_xls()
|
||||
{
|
||||
require (dirname (__FILE__) . "/class-excel-xml.inc.php");
|
||||
$myarray = array (
|
||||
1 => array ("Oliver", "Peter", "Paul"),
|
||||
@ -21,8 +21,8 @@ function file_xls()
|
||||
$xls = new Excel_XML;
|
||||
$xls->addArray ($myarray);
|
||||
$xls->generateXML ( "filename" );
|
||||
}
|
||||
`
|
||||
}
|
||||
|
||||
|
||||
The only thing you must do is to download the file and copy it where you like, but remember to indicate the right path where your application can find it.
|
||||
|
||||
@ -34,9 +34,9 @@ The only thing you must do is to download the file and copy it where you like, b
|
||||
|
||||
If you want to use **arrays**, you have to write within your **Controller** the following code:
|
||||
|
||||
`
|
||||
function file_xls()
|
||||
{
|
||||
|
||||
function file_xls()
|
||||
{
|
||||
$this->load->helper('php-excel');
|
||||
$data_array = array (
|
||||
$data_array[] = array ("Oliver", "Peter", "Paul"),
|
||||
@ -45,14 +45,14 @@ function file_xls()
|
||||
$xls = new Excel_XML;
|
||||
$xls->addArray ($data_array);
|
||||
$xls->generateXML ( "output_name" );
|
||||
}
|
||||
`
|
||||
}
|
||||
|
||||
|
||||
But if you want to use the class with a MySQL database query (or whatever you wish in CodeIgniter?), this is the right, working code:
|
||||
|
||||
`
|
||||
function file_xls()
|
||||
{
|
||||
|
||||
function file_xls()
|
||||
{
|
||||
$this->load->helper('php-excel');
|
||||
$query = $this->db->get('table_name');
|
||||
foreach ($query->result() as $row)
|
||||
@ -62,14 +62,13 @@ function file_xls()
|
||||
$xls = new Excel_XML;
|
||||
$xls->addArray ($data_array);
|
||||
$xls->generateXML ( "output_name" );
|
||||
}
|
||||
`
|
||||
}
|
||||
|
||||
Of course, you can combine the _array method_ with the _query method_, because the only issue with the _query method_ is that only converts data, instead of field names AND data. There's no problem, because you can do it so:
|
||||
|
||||
`
|
||||
function file_xls()
|
||||
{
|
||||
|
||||
function file_xls()
|
||||
{
|
||||
$this->load->helper('php-excel');
|
||||
$query = $this->db->get('table_name');
|
||||
|
||||
@ -83,8 +82,8 @@ function file_xls()
|
||||
$xls->addArray ($field_array);
|
||||
$xls->addArray ($data_array);
|
||||
$xls->generateXML ( "output_name" );
|
||||
}
|
||||
`
|
||||
}
|
||||
|
||||
|
||||
I hope you find it useful. Cheers.
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user