Export to excel with multiple sheet using codeigniter? - php

I have a table which contain employee details.I have to export these employee details in excel sheet.In excel each sheet contain each employee details and sheet name should be employee name. In models I have writen a query to fetch the details from database the following is the sample view code. This following code will give only one sheet of result How I implement multiple sheet excel
<?PHP
header ("Cache-Control: no-cache, must-revalidate");
header ("Pragma: no-cache");
header ('Content-type: application/x-msexcel');
header ("Content-Disposition: attachment; filename=employee_workbook.xls" );
$this->load->view('reader/reader');
?>
<?PHP
if($result->num_rows()>0)
{
?>
<?PHP foreach($result->result_array() as $entry):?>
<table width="100%" border="1" cellpadding="1" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td width="200" bgcolor="#FFFFFF"><strong>Employee ID</strong></td>
<td width="150" bgcolor="#FFFFFF"><strong>Employee Name</strong></td>
<td width="150" bgcolor="#FFFFFF"><strong>Check in Time</strong></td>
<td width="100" bgcolor="#FFFFFF"><strong>Project Name</strong></td>
<td width="100" bgcolor="#FFFFFF"><strong>Check Out Time</strong></td>
</tr>
<tr>
<td align="left" bgcolor="#FFFFFF"><?php echo $entry['emp_id']; ?></td>
<td align="left" bgcolor="#FFFFFF"><?PHP echo $entry['emp_name']; ?></td>
<td align="left" bgcolor="#FFFFFF"><?PHP echo $entry['in_time']; ?></td>
<td align="left" bgcolor="#FFFFFF"><?PHP echo $entry['prj_name']; ?></td>
<td align="left" bgcolor="#FFFFFF"><?PHP echo $entry['out_time']; ?></td>
</tr>
</table>
<?PHP endforeach;?>
<?PHP
}
?>

You might want to look into PHPExcel, I have tried almost every script out there to export data to excel, nothing does it better than PHPExcel, you can even export to multiple sheets.

You can also use EasyXLS. Download the Excel library for PHP.
You can find a lot a code samples starting from here. You can save multiple sheets using this library.

Related

Reformatting a result from a json file

I have a json file I am getting returned from an API. I have learnt how to format it and lay it out in PHP but one bit I can't work out is how to reformat the date that is returned.
This is the page that displays the results:
<body>
<h1 align="center"><font face ="Arial"><u><br>Results<br><br></u>Postcode - <?php echo $resultData['postcode']; ?></font></h1>
<br>
<br>
<h2 align="center">Known Floor Sizes Listed By Report Date</h2>
<br>
<table class= "general" align= "center" bgcolor="#ffffff">
<tr>
<td align="center" rowspan="2"><b>Inspection Date</b></td>
<td align="center" rowspan="2"><b>Address</b></td>
<td align="center" colspan="2"><b>Size in</b></td>
</tr>
<tr>
<td align="center"><b>SqM</b></td>
<td align="center"><b>SqFt</b></td>
</tr>
<?php foreach ($resultData['known_floor_areas'] as $property) { ?>
<tr>
<td align="center"><?php echo $property['inspection_date']; ?></td>
<td align= "left" ><?php echo $property['address']; ?></td>
<td align="center"><?php echo (round($property['square_feet'] * 0.092903,0));?></td>
<td align="center"><?php echo $property['square_feet']; ?></td>
</tr>
<?php } ?>
</table>
</body>
This displays the date in the format: 2015-06-22T23:00:00.000000Z
I want to change the format to a basic: 2015-06-22
I'm sure it's simple but after a lot of searching I'm going round in circles.
Use DateTime() then format() it:
<td align="center"><?php echo (new DateTime($property['inspection_date']))->format('Y-m-d'); ?></td>

How can I set name to excel sheet in PHP?

Am generating .xls file with <table> tags. Export is working fine. Now I have to set the name for the sheet.
Code is as follows:
<h2>==>> Export Table into Excel file <<==</h2>
<?php
echo $excel_data = '<table border="1">
<thead>
<th width="1"></th>
<th align="left">S.No.</th>
<th align="left">Name</th>
<th align="left">DOJ</th>
</thead>
<tbody>
<tr>
<td width="1"></td>
<td align="left">1</td>
<td align="left">Sreekanth Kuriyala</td>
<td align="left">04-06-2015</td>
</tr>
<tr>
<td width="1"></td>
<td align="left">2</td>
<td>SK1</td>
<td align="left">26-07-2015</td>
</tr>
<tr>
<td width="1"></td>
<td align="left">3</td>
<td> SK2</td>
<td align="left">26-07-2015</td>
</tr>
</tbody>
</table>';
$excel_file = 'report.xls';
file_put_contents ($excel_file, $excel_data);
?>
</br>
</br>
<a href="<?php echo $excel_file; ?>" download>Export to Excel</a>
Can anyone help me on this. Thanks in advance.
For that you have to follow/generate Excel file content structure instead of table structure.
You can use PHP library for Excel file generator. There are many PHP library available you can googling it. Some of are
PHPExcel
SimpleExcel PHP
Pear Excel Writer
Excel Writer(XML) for PHP
Thanks.

PHP using mysql_fetch_array to fill a table

Ok Before you tell me to use mysqli I am using the depreciated methods on purpose for a webapp lesson. I am not a student, this is not homework, it is to help me teach an understanding for web application security.
I cannot figure out why this wont work. Basically, All I want to do is create a page that takes the data from the mysql database and places it into a table as shown. At this point I am open to anything.
Thank you in advance.
<html>
<head><title>Untitled</title></head>
<body>
<h1>Weblog Example</h1>
<dl>
<?php
mysql_connect("localhost","root","");
mysql_select_db("blog1");
$query ="SELECT entrytitle, entrytext,";
$query.=" DATE_FORMAT(entrydate, '%M %d, %Y') AS entrydate";
$query.=" FROM weblog ORDER BY entrydate DESC LIMIT 10";
$result=mysql_query($query);
?>
<table width="90%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td width="15%" align="center" bgcolor="#E6E6E6"><strong>Title</strong></td>
<td width="75%" align="center" bgcolor="#E6E6E6"><strong>Entry</strong></td>
<td width="15" align="center" bgcolor="#E6E6E6"><strong>Date/Time</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['entrytitle']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['entrytext']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['entrydate']; ?></td>
</tr>
<?php
}
?>
</dl>
</body>
</html>
Ok- made some minor edits- it now gives me 3 rows in the table but doesn't populate the data...
Do a var_dump on $entrytitle and $entrytext and you'll understand you error.
Data is temporary stored into $rows when you do a mysql_fetch_array.
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><? echo $rows["entrytitle"]; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows["entrytext"]; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows["entrytext"]; ?></td>
</tr>
<?php
}
?>
You're using the wrong variable in your while loop, you're also not referencing the correct date column from your query result.
This should give you what you're looking for:
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><?php echo $rows['entrytitle']; ?></td>
<td align="center" bgcolor="#FFFFFF"><?php echo $rows['entrytext']; ?></td>
<td align="center" bgcolor="#FFFFFF"><?php echo $rows['entrydate']; ?></td>
</tr>
<?php
}
?>
Both your $query.= should read as $query .= that alone will be an issue and will not concatenate properly because of the missing space before the dots.
You're also missing a </table> tag.
Plus, make sure that short tags are set/on, otherwise do <?php echo $rows...
instead of <? echo $rows....
You should also check for possible query errors using:
$result = mysql_query($query) or die(mysql_error());
and using error reporting.
Add error reporting to the top of your file(s) which will help find errors.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
Sidenote: Error reporting should only be done in staging, and never production.
I also suggest you switch to mysqli with prepared statements, or PDO with prepared statements, they're much safer.

Can't view the content of my main forum table in PHP

I am trying to view forum topics in this table. However the content of the table will not display. I don't know what I am missing from my code. It shows the table and displays the correct amount of rows but no text.
<?php
include "db.php" ?>
$sql="SELECT * FROM $tbl_name ORDER BY id DESC";
// OREDER BY id DESC is order result by descending
$result=mysql_query($sql);
?>
<table width="90%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td width="6%" align="center" bgcolor="#E6E6E6"><strong>#</strong></td>
<td width="53%" align="center" bgcolor="#E6E6E6"><strong>Topic</strong></td>
<td width="15%" align="center" bgcolor="#E6E6E6"><strong>Views</strong></td>
<td width="13%" align="center" bgcolor="#E6E6E6"><strong>Replies</strong></td>
<td width="13%" align="center" bgcolor="#E6E6E6"><strong>Date/Time</strong></td>
</tr>
<?php
// Start looping table row
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['topic']; ?><BR></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['view']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['reply']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['datetime']; ?></td>
</tr>
<?php
// Exit looping and close connection
}
mysql_close();
?>
<tr>
<td colspan="5" align="right" bgcolor="#E6E6E6"><strong>Create New Topic</strong> </td>
</tr>
</table>
Remove ?> after include "db.php"
<?php
include "db.php"
$sql="SELECT * FROM $tbl_name ORDER BY id DESC";
// OREDER BY id DESC is order result by descending
$result=mysql_query($sql);
?>
<table width="90%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td width="6%" align="center" bgcolor="#E6E6E6"><strong>#</strong></td>
<td width="53%" align="center" bgcolor="#E6E6E6"><strong>Topic</strong></td>
<td width="15%" align="center" bgcolor="#E6E6E6"><strong>Views</strong></td>
<td width="13%" align="center" bgcolor="#E6E6E6"><strong>Replies</strong></td>
<td width="13%" align="center" bgcolor="#E6E6E6"><strong>Date/Time</strong></td>
</tr>
<?php
// Start looping table row
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['topic']; ?><BR></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['view']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['reply']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['datetime']; ?></td>
</tr>
<?php
// Exit looping and close connection
}
mysql_close();
?>
<tr>
<td colspan="5" align="right" bgcolor="#E6E6E6"><strong>Create New Topic</strong> </td>
</tr>
</table>
It's probably because your server does not have short_open_tag configuration enabled.
Because of that, the following line is not parsed as PHP code, but rather it's considered as plain text.
<? echo $rows['view']; ?>
Use this instead:
<?php echo $rows['view']; ?>
Short tags are considered a PITA in the coding world, because if you ever have to migrate your code to a server that doesn't support it, or you can't enable it, than you have to edit all your code just for that environment when you could have just gone with normal tags. However, as of 5.4.x, short hand tags are enabled by default and parsed in any PHP document. But it's good to keep in mind different environments.
Your code:
<?php
include "db.php" ?>
//--------------------------^ remove this.
So final code should look like:
<?php
include "db.php"
$sql="SELECT * FROM $tbl_name ORDER BY id DESC";
// OREDER BY id DESC is order result by descending
$result=mysql_query($sql);
?>

convert Data from Database to excel

i have grid that gets data from data base like this :-
<table width="100%" cellspacing="0" cellpadding="0" border="0" class="gwlines">
<tbody><tr>
<th width="15">#</th>
<th width="150">Name</th>
<th width="100">Type</th>
<th width="50">Date</th>
<th width="50">From</th>
<th width="50">To</th>
<th width="30">Price</th>
<th width="30">Paid</th>
<th width="30">Remaind</th>
<th colspan="4">Actions</th>
</tr>
<?
$i=1;
while($objResult=mysql_fetch_array($objQuery)){
?>
<tr id="vip_row<?=$objResult["vip_id"];?>" <? echo ($i%2==0?'bgcolor="#edf0f6"':''); ?>>
<td><input name="cbSelect[]" type="checkbox" id="cbSelect[]" value="<?=$objResult["vip_id"]; ?>" /></td>
<td><?=$objResult["customer_name"]; ?></td>
<td><?=$objResult["vip_type"]; ?></td>
<td><?=$objResult["vip_date"]; ?></td>
<td><?=$objResult["vip_from"]; ?></td>
<td><?=$objResult["vip_to"]; ?></td>
<td><?=$objResult["vip_price"]; ?></td>
<td><?=$objResult["vip_paid"]; ?></td>
<td><?=$objResult["vip_remind"]; ?></td>
<td width="16"><img width="15" height="15" class="tabpimpa" alt="picture" src="images/edit.png"></td>
<td width="16"><img width="15" height="15" class="tabpimpa" alt="picture" src="images/delete.png"></td>
<td width="16"><img width="15" height="15" class="tabpimpa" alt="picture" src="images/print.png"></td>
<td width="16"><img width="15" height="15" class="tabpimpa" alt="picture" src="images/sms.png"></td>
</tr>
<? ++$i; } ?>
</tbody></table>
this grid will get data from data base, and i want to export this result data to excel.
how can i do this?
Following from http://support.microsoft.com/kb/165499 you should be able to just save the HTML table and open it in excel!
It will of course drop most of the formatting and the functions, but most of the time that is not a real concern when exporting data to excel.
try the solution to this link. But if I where you, don't just convert it to excel if you want to print it. You can use pdf like this and this.
try using phpexcel
happy coding :)
EDIT: "phpexcel" is a project providing a set of classes for the PHP programming language, which allow you to write to and read from different spreadsheet file formats, like Excel (BIFF) .xls, Excel 2007 (OfficeOpenXML) .xlsx, CSV, Libre/OpenOffice Calc .ods, Gnumeric, PDF, HTML, ... This project is built around Microsoft's OpenXML standard and PHP.
-by phpexcel site

Categories