phpexcel export to excel with image - php

I have a table in my Database containing the data of many items with the name of its image.
From that table I want to display each item data with its image. This is the code I use to display it in PHP:
<table>
<thead>
<tr>
<th style="border-width:medium;" rowspan="2">CODE</th>
<th style="border:medium;" rowspan="2">CLR</th>
<th style="border-width:medium;" rowspan="2">IMAGE</th>
<th style="border-width:medium;" rowspan="2">DESCRIPTION</th>
<th style="border-width:medium;" rowspan="2">QTY.</th>
<th style="border-width:medium;" rowspan="2">PRICE</th>
</tr>
</thead>
<tbody>
<?php foreach($list as $data){?>
<tr>
<td><?=$data->code_caption?></td>
<td style="text-align:center"><?=$data->color?></td>
<td style="width:135px; height:135px; text-align:center;">
<img src="<?=base_url().'media/img/gallery/items/'.$data->image?>TH.jpg" style="margin:5px; text-align:center; vertical-align:middle;" />
</td>
<td><?=$data->desc?></td>
<td style="text-align:center"><?=$data->qty?></td>
<td style="text-align:right"><?=$data->price?></td>
</tr>
<?php }?>
</tbody>
</table>
I want to export the data to an Excel formatted file so it looks like what I display on my PHP page.
Can it be done?
I have tried to use the Excel header but the image becomes a link, it's not the complete image being embedded.
And I also saw the PHPexcel library, but I cannot find a way to write the loop with the image.
Any solution for my problem will be very helpful.

Thanks for Helping me I have found the answer for my own question.
<?php
$rowNumber = 12;
foreach($list as $data){
$this->excel->getActiveSheet()->setCellValue('A'.$rowNumber, $data->code_caption)
->setCellValue('B'.$rowNumber, $data->color);
if(file_exists('./media/img/gallery/items/'.$data->image.'TH.jpg'))
{
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setPath('./media/img/gallery/items/'.$data->image.'TH.jpg');
$objDrawing->setCoordinates('C'.$rowNumber);
$objDrawing->setWorksheet($this->excel->getActiveSheet());
$this->excel->getActiveSheet()->getRowDimension($rowNumber)->setRowHeight(120);
}
else
{
$this->excel->getActiveSheet()->setCellValue('C'.$rowNumber, '');
}
$this->excel->getActiveSheet()->setCellValue('D'.$rowNumber, $data->desc);
$this->excel->getActiveSheet()->setCellValue('E'.$rowNumber, $data->$d_met);
$this->excel->getActiveSheet()->setCellValue('F'.$rowNumber, $data->$w_met);
$this->excel->getActiveSheet()->setCellValue('G'.$rowNumber, $data->$h_met);
$this->excel->getActiveSheet()->setCellValue('H'.$rowNumber, $data->qty);
$this->excel->getActiveSheet()->setCellValue('I'.$rowNumber,$data->$cur_dat);
$this->excel->getActiveSheet()->setCellValue('J'.$rowNumber,$data->$total_price);
$rowNumber++;
}?>

Related

Little problem with display data from local server in html table with php and sql

Today I have a little problem, with basic and maybe funny thing. Today I wanted make table with data from sql server. For that I use Xampp for that and maybe is verry common choose for that. The problem is with table because i tried to display datas with my ideas and with my knowledge and results is maybe more than what i want to do, just maybe.
Below is my scripts and I would like to mention that I make some versions of scripts and here it's just one of them.
HTML + PHP Table
<div class="centred-container table">
<table class="table-results">
<tr class="tr-table">
<th class="th-table th-univ">Product name</th>
<th class="th-table th-univ">Cod PLU</th>
<th class="th-table th-univ">Cod Intern</th>
<th class="th-table th-univ">Cod EAN/Bare</th>
<th class="th-table th-univ">Unitate</th>
<th class="th-table th-univ">Pret</th>
<th class="th-table th-univ">Stoc</th>
</tr>
<tr class="tr-table">
<?php
while ($row = mysqli_fetch_assoc($result)) {
$nameP = $row["ProductName"];
$PLUP = $row["ProductPLU"];
$ICP = $row["ProductIC"];
$EANP = $row["ProductEAN"];
$UnitP = $row["ProductUnit"];
$PriceP = $row["ProductPrice"];
$StockP = $row["ProductStock"];
echo"<td>".$nameP."</td>
<td>".$PLUP."</td>
<td>".$ICP."</td>
<td >".$EANP."</td>
<td >".$UnitP."</td>
<td >".$PriceP."</td>
<td >".$StockP."</td> <br>";
}
?>
</table>
</div>
PHP part
if (isset($_POST["submit"])) {
session_start();
include_once 'includes/dbh.inc.php';
$codePLU = $_POST["productPLU"];
$codeEAN = $_POST["productEAN"];
$codeIC = $_POST["productIC"];
$sql = "SELECT * FROM products WHERE ProductPLU ='$codePLU' OR ProductEAN = '$codeEAN' OR ProductIC = '$codeIC';";
$result = mysqli_query($conn, $sql);
}
?>
Here I want display (SELECT) data from database and and display that in html table. The problem is I tried and method with <td><?php echo $variable ?></td> where $varable = $row["column_name"] and in this situation all my data is just maybe first or the last or just one row where there should have been more because I Know my sql query have more results (rows) than one row I see.
With pasted scripts I have all my rows returned by sql query but all rows is on one line and I want to be all in a classic table.
I want just little help for my little problem. I Know is just basic problem in php site but i want some case from you, or results, where I want to try for make my table a classic table without errors same as my errors above.
Sorry for my Englesh.
The problem is with your HTML table. Need to open and close each row (<tr>) within the while loop. code below
<table class="table-results">
<tr class="tr-table">
<th class="th-table th-univ">Product name</th>
<th class="th-table th-univ">Cod PLU</th>
<th class="th-table th-univ">Cod Intern</th>
<th class="th-table th-univ">Cod EAN/Bare</th>
<th class="th-table th-univ">Unitate</th>
<th class="th-table th-univ">Pret</th>
<th class="th-table th-univ">Stoc</th>
</tr>
<?php
while ($row = mysqli_fetch_assoc($result)) {
$nameP = $row["ProductName"];
$PLUP = $row["ProductPLU"];
$ICP = $row["ProductIC"];
$EANP = $row["ProductEAN"];
$UnitP = $row["ProductUnit"];
$PriceP = $row["ProductPrice"];
$StockP = $row["ProductStock"];
echo"<tr> <td>".$nameP."</td>
<td>".$PLUP."</td>
<td>".$ICP."</td>
<td >".$EANP."</td>
<td >".$UnitP."</td>
<td >".$PriceP."</td>
<td >".$StockP."</td> </tr>";
}
?>
</table>

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.

How to Apply CSS on HTML to Excel export

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.

How to create an html link to another page from "<?=$objResult["id"];?>"

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>

How to convert Html code to template output in browser

I need to convert HTML in template.
As seen in image content should show as a template.
I am using table to show . but now i need to show it in template output
Here is my Code:
<table id="datatable" class="tablesorter">
<span id='errfrmMsg'></span>
<thead>
<tr>
<th scope="col" id="">Name</th>
<th scope="col" id="">Content</th>
</tr>
</thead>
<tbody>
<?php
$templ_id = $_REQUEST['templ_id'];
$getTemplateQuery = mysql_query("select * from templates where id = $templ_id");
$getTemplateRow = mysql_fetch_array($getTemplateQuery);
?>
<tr>
<td valign="top" width="125"><?php echo $getTemplateRow['templ_name'];?></td>
<td valign="top" width="319">
<?php
echo $templContent = $getTemplateRow['templ_content'];
?>
</tr>
</tbody>
</table>
Here is an Output:
From what I can gather from your question, try outputting with:
$html = htmlspecialchars_decode($content)
EDIT:
echo $templContent = htmlspecialchars_decode($getTemplateRow['templ_content']);
Try this this will help You
<?php
echo $htmlContent = htmlspecialchars_decode($getTemplateRow['templ_content'])
?>

Categories