i am exporting data from database (MySQL) in excel sheet. i also want to export images in Excel using PHP.if any demo link is available, please share it here
You have to use one of the followig library to do that.
URLS:
https://github.com/PHPOffice/PHPExcel
https://phpexcel.codeplex.com/
https://code.google.com/p/php-excel/
What you're looking for is PHPExcel.
Here are some examples.
This is how I solved mine. Hope this helps!
public function export_items_to_excel(){
$items = $this->transaction->view_all_items();
$output = '';
$output .= "<table class='table' border='1'>
<thead>
<th style='background-color:#c7c7c7;'>NAME</th>
<th style='background-color:#c7c7c7;'>DESCRIPTION</th>
<th style='background-color:#c7c7c7;'>QUANTITY</th>
<th style='background-color:#c7c7c7;'>WEIGHT (KG)</th>
<th style='background-color:#c7c7c7;'>HS CODE</th>
<th style='background-color:#c7c7c7;'>SERIAL NO.</th>
<th style='background-color:#c7c7c7;'>UNIT VALUE</th>
<th style='background-color:#c7c7c7;'>CURRENCY</th>
<th style='width:220px !important;background-color:#c7c7c7;'>PICTURE</th>
</thead>
<tbody>
";
foreach($items as $item){
$output .= "
<tr>
<td style='text-align:center;'>".$item->item_name."</td>
<td style='text-align:center;'>".$item->item_description."</td>
<td style='text-align:center;'>".$item->item_quantity."</td>
<td style='text-align:center;'>".number_format($item->item_weight, 2)."</td>
<td style='text-align:center;'>".$item->item_hs_code."</td>
<td style='text-align:center;'>".$item->item_serial_number."</td>
<td style='text-align:center;'>".number_format($item->item_unit_value, 2)."</td>
<td style='text-align:center;'>".$item->item_currency."</td>
<td style='text-align:center;width:220px !important;height:220px !important;'><img src='".base_url()."assets/uploads/".$item->item_picture."' style='width:200px !important;height:152px !important;'> </td>
</tr>
";
}
$output .= "</tbody>
</table>
";
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header("Content-Disposition: attachment; filename=items.xls");
header("Cache-Control: max-age=0");
echo $output;
}
Related
I am using php and mysql to generate excel file. Every thing works fine. I am using html table code and exporting data to excel file. But the downloaded file is having bigger size for 20k records it is showing nearly 35 MB. Below is my code .
<?php
$generated_date = date("Y-m-d-H-i-s");
$filename = 'order_detail_report'.$generated_date;
header("Content-Type: application/xls");
header("Content-Disposition: attachment; filename=$filename.xls");
header("Pragma: no-cache");
header("Expires: 0");
?>
Html table code
<table width="100%" border="0" cellspacing="0" cellpadding="0" >
<tr>
<th class="excel_style">Order Number</th>
<th class="excel_style">EAN</th>
<th class="excel_style">Article #</th>
<th class="excel_style">Amazon Price</th>
<th class="excel_style">ERP Price</th>
<th class="excel_style">Requested Quantity</th>
<th class="excel_style">Dispatch Quantity</th>
<th class="excel_style">Rejected Quantity</th>
<th class="excel_style">Cancelled Quantity</th>
</tr>
While loop
<?php
$query = "my select query";
$res = mysqli_query($mysqliConn,$query);
while($result= mysqli_fetch_assoc($res)){
?>
<tr>
<td class="tdstyle"><?php echo $result['order_id'];?></td>
<td class="tdstyle"><?php echo $result['ean'];?></td>
<td class="tdstyle"><?php echo $result['article_no'];?></td>
<td class="tdstyle"><?php echo $result['net_price'];?></td>
<td class="tdstyle"><?php echo $result['erp_price'];?></td>
<td class="tdstyle"><?php echo $result['requested_quantity'];?></td>
<td class="tdstyle"><?php echo $result['dispatch_quantity']?></td>
<td class="tdstyle"><?php echo $result['rejected_quanity'];?></td>
<td class="tdstyle"><?php echo $result['cancelled_quantity']?></td>
</tr>
<?php } ?>
</table>
How to solve this size of the file problem? Any help would be greatly appreciated. If I use phpexcel library I am getting some out of memory exception. So client asked me don't use phpexcel library.
It is not clear what is exact problem - the size of result generated file or the memory getting exhausting?
But, lets assume that the problem is memory getting exhausting. Do you really need to generate excel(.xslx) files or CSV format is acceptable? You can generate CSV file with a really low memory usage like this
<?php
$fp = fopen('file.csv', 'w');
$query = "my select query";
$res = mysqli_query($mysqliConn,$query);
while($result= mysqli_fetch_assoc($res)) {
$fields = [
$result['order_id'],
$result['ean'],
$result['article_no'],
$result['net_price'],
$result['erp_price'],
$result['requested_quantity'],
$result['dispatch_quantity'],
$result['rejected_quantity'],
$result['cancelled_quantity']
]
fputcsv($fp, $fields);
}
fclose($fp);
?>
I have this build up HTML table that I want to convert into Excel spreadsheet but it happens to show the amount thousands separator and the 2 decimal as the PHP function number_format specifies. in short like for 100.00 in Excel I am having 100
$OutputExcel = "
<h3 align='center'>$COMPANYNAME</h3>
<table class='tblNemoList' border='0' cellpadding='2' cellspacing='0' width='100%' style='-fs-table-paginate: paginate;'>
<caption align='left'><b>Detailed Horse Charges</b></caption>
<thead>
<tr>
<th align='left'>#</th>
<th align='left'>No</th>
<th align='left'>Horse Name</th>
<th align='left'>Description</th>
<th align='left'>Date</th>
<th align='left'>Amount</th>
</tr>
</thead>
";
$i = 0;
while($row = $xdb->fetch_object($rowHorseCharges))
{
//ini
$i++;
$OutputPDF .= "
<tr >
<td>$i</td>
<td>".$row->No."</td>
<td>".$row->strHorse."</td>
<td>".$row->Description."</td>
<td>".$row->Date."</td>
<td align='right'>".number_format($row->Amount,2,'.',',')."</td>
</tr>
";
$OutputExcel .= "
<tr >
<td align='left' >$i</td>
<td align='left' >".$row->No."</td>
<td align='left' >".$row->strHorse."</td>
<td align='left' >".$row->Description."</td>
<td align='left' >".$row->Date."</td>
<td align='right' >".number_format($row->Amount,2,'.',',')."</td>
</tr>
";
}
$OutputPDF .= "
<tr><td colspan='6' style='border-bottom:1px solid black;'></td></tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td align='right' ><b>Tota: ".number_format($rowTotal->Total,2,'.',',')."</b></td>
</tr>
</table>";
$OutputExcel .= "
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td align='right' ><b>Total: ".number_format($rowTotal->Total,2,'.',',')."</b></td>
</tr>
</table>
";
I am converting it using this
switch($Action){
case "Clear":
break;
case "Export Excel":
include_once("reports/". $_REQUEST[radReport] .".php");
header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=". $strFilename .".xls"); //gets set inside the report.rpt.php
header("Pragma: no-cache");
header("Expires: 0");
echo $OutputExcel;
die;
break;
case "ExportPDF":
include_once("reports/". $_REQUEST[radReport] .".php");
ini_set('display_errors', '0');
header("Content-type: application/pdf");
require_once('includes/mpdf/mpdf.php');
$mpdf = new mPDF('utf-8', $format ,9,'Arial');
$mpdf->setHTMLHeader(getPDFHeader($COMPANYNAME));
$mpdf->setHTMLFooter(getPDFFooter());
if($orientation == 'P'){$mpdf->AddPage($orientation,'', '' ,'','',15,15,20,16,7,9);}else{$mpdf->AddPage($orientation,'', '' ,'','',30,30,30,30,18,12);}
$mpdf->WriteHTML($OutputPDF);
$mpdf->Output($strFilename."pdf", "I");
die;
break;
case "OpenPDF":
default:
$page->Content .= $Output
. js("d('". $_POST[radReport] ."').click(function())");
break;
}
You need to add CSS to your table page. Like so:
<style type='text/css'>
.num2d {
mso-number-format:"0\.00";
}
</style>
and then use that as a class to the <td> like so:
<td class="num2d">34</td>
I didn't make this up myself, but found it here:
http://cosicimiento.blogspot.nl/2008/11/styling-excel-cells-with-mso-number.html
I am trying to export HTML to Excel format using php headers, but CSS styling is not applying on elements (while export to excel file), I also try to implement Bootstrap Classes, but no luck
Note: while applying bootstrap classes, I included the bootstrap.css in my html
Is there any way available to apply CSS?
Headers using
header('Content-type: application/excel');
header("Content-Disposition: attachment; filename=\"$filename\"");
Sample HTML elements
<table width="100%" border="1" class="table table-striped table-bordered">
<thead>
<tr>
<td colspan="9" style="font-weight: bold; font-size: 16px; text-align: center;"><h2>Push Notification </h2></td>
</tr>
<tr><td colspan="9"></td></tr>
</thead>
<tbody>
<tr class="heading">
<th colspan="2" ></th>
<th style="width:2% !important">S.no</th>
<th style="width:10% !important">Date</th>
<th style="width:10% !important">App</th>
<th style="width:20%">Category</th>
<th colspan="2" style="width:50%">Message</th>
</tr>
#if($push_notifications->count() > 0)
<?php $counter = 1; ?>
#foreach($push_notifications as $notification)
<tr #if(($counter %2) ==1) bcolor="#c9c9c9" #endif>
<td colspan="2"></td>
<td style="width:2% !important">{!!$counter!!}</td>
<td style="width:10% !important">{!!$notification->date!!}</td>
<td style="width:10% !important">{!!$notification->notificationAppType->name!!}</td>
<td style="width:20%">{!!$notification->notificationCategory->name!!}</td>
<td colspan="2" style="width:50%">{!!htmlentities($notification->message, ENT_QUOTES)!!}</td>
</tr>
<?php $counter++; ?>
#endforeach
#else
<tr>
<td colspan="9">No record available</td>
</tr>
#endif
</tbody>
</table>
Short answer is no, you can't apply css to excel.
You can, however, apply some formatting to the text of excel files, but the amount of formatting is limited by the library you use to generate the excel file and to what formatting the excel itself permits.
I'm working on a php/mysql application and need to make the output of one column, one row an html link to another html page. Have not been able to find any relavant information. The "" needs to be the link. Thanks for any help.
<table id="display" style="width:800px;">
<tr>
<th width="40">ID</th>
<th width="70">Last</th>
<th width="70">First</th>
<th width="10">Middle</th>
<th width="70">Birth</th>
<th width="70">Death</th>
<th width="170">Notes</th>
<th width="100">Cemetery</th>
</tr>
<?
while($objResult = mysql_fetch_array($objQuery))
{
?>
<tr>
<td style='text-align:center;'><?=$objResult["id"];?></td>
<td style='text-align:center;'><?=$objResult["last"];?></td>
<td style='text-align:center;'><?=$objResult["first"];?></td>
<td style='text-align:center;'><?=$objResult["middle"];?></td>
<td style='text-align:center;'><?=$objResult["birth"];?></td>
<td style='text-align:center;'><?=$objResult["death"];?></td>
<td><?=$objResult["notes"];?></td>
<td style='text-align:center;'><?=$objResult["cemetery"];?></td>
</tr>
<?
}
?>
</table>
I have a spinner and what happens is that whatever number is in the spinner, when the form is submitted, it should display the word "quest" as many times as the number in the spinner.. E.g if number in spinner is 3, then it will display "quest" 3 times in the table.
The problem is displaying it in the table.
At the moment with my current code it is displaying it like this:
quest
quest
quest
Question Id, Option Type, Duration .... These are table headings
It is displaying the words quest outside the table
Instead I want the word "quest" to be displayed in the Question Id column like this:
Question Id, Option Type, Duration...
quest
quest
quest
How can I get it to display it like the example above?
Below is code
<table border=1 id="qandatbl" align="center">
<tr>
<th class="col1">Question No</th>
<th class="col2">Option Type</th>
<th class="col1">Duration</th>
<th class="col2">Weight(%)</th>
<th class="col1">Answer</th>
<th class="col2">Video</th>
<th class="col1">Audio</th>
<th class="col2">Image</th>
</tr>
<?php
$spinnerCount = $_POST['txtQuestion'];
if($spinnerCount > 0) {
for($i = 1; $i <= $spinnerCount; $i++) {
echo "<tr>quest";
}
}
?>
<td class='qid'></td>
<td class="options"></td>
<td class="duration"></td>
<td class="weight"></td>
<td class="answer"></td>
<td class="video"></td>
<td class="audio"></td>
<td class="image"></td>
</tr>
</table>
I did try echo "<td class='qid'></td>"; but this completely failed as well
Try this:
<table border=1 id="qandatbl" align="center">
<tr>
<th class="col1">Question No</th>
<th class="col2">Option Type</th>
<th class="col1">Duration</th>
<th class="col2">Weight(%)</th>
<th class="col1">Answer</th>
<th class="col2">Video</th>
<th class="col1">Audio</th>
<th class="col2">Image</th>
</tr>
<?php
$spinnerCount = $_POST['txtQuestion'];
if($spinnerCount > 0) {
for($i = 1; $i <= $spinnerCount; $i++) {
?>
<tr>
<td class='qid'><?php echo $quest; ?></td>
<td class="options"></td>
<td class="duration"></td>
<td class="weight"></td>
<td class="answer"></td>
<td class="video"></td>
<td class="audio"></td>
<td class="image"></td>
</tr>
<?php
} // For
} // If
?>
</table>
Is this what you want to do? Display "quest" in the first column?
<table border=1 id="qandatbl" align="center">
<tr>
<th class="col1">Question No</th>
<th class="col2">Option Type</th>
<th class="col1">Duration</th>
<th class="col2">Weight(%)</th>
<th class="col1">Answer</th>
<th class="col2">Video</th>
<th class="col1">Audio</th>
<th class="col2">Image</th>
</tr>
<?php
$spinnerCount = $_POST['txtQuestion'];
if($spinnerCount > 0) {
for($i = 1; $i <= $spinnerCount; $i++) { ?>
<tr>
<td class='qid'>quest</td>
<td class="options"></td>
<td class="duration"></td>
<td class="weight"></td>
<td class="answer"></td>
<td class="video"></td>
<td class="audio"></td>
<td class="image"></td>
</tr>
<?php
}
}
?></table>
?>