I have a HTML table similar to this ( but it gets more complicated at the end ):
<table class="planning-table">
<thead>
<tr>
<th colspan="6">3 month ( June, 2015 )</th>
</tr>
</thead>
<tbody>
<tr style="display: none;" class="spacer-row"></tr>
<tr>
<td>
<span class="check" style="padding-right: 10px;">
<input class="checkboxes" id="status[49]" value="1" name="status[49]" type="checkbox">
<span class="state"></span>
</span>
</td>
<td>
<div>New weeks meeting!</div>
</td>
<td>
Decors
</td>
<td>
</td>
<td style="width: 120px;">
18. march
</td>
<td>
<div style="display: none;">
<div id="editTask49" class="editTask">
......
</div>
</div>
</td>
</tr>
</tbody>
</table>
Now I need to export this to XLS. I have checked similar examples, but most of them don't create correct XLS cells, meaning, they get messed up.
How can I acomplish this ( probably using PHPExcel ) ?
You can use an external library, as PHPExcel, but if you want something simple you can try put this at the begin of your PHP:
header("Content-Type: application/vnd.ms-excel");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("content-disposition: attachment;filename=excel.xls");
This code will force your browser to "send" the content to a file your excel is goint to interpret
Try!
Related
Hi I am going to Export my html form to excel
Here is my code:
<script type="text/javascript">
function callexports()
{
var tabdata=document.getElementById("tabledata").innerHTML;
document.getElementById("tableval").value=tabdata;
document.getElementById("sendtable").submit();
}
</script>
<form id="sendtable" action="exportexcel.php" method="post">
<input type="button" name="Export Excel"
value="Export Excel" onclick="callexports();"/>
<div id="tabledata">
<table border='1'>
<tr >
<th style="background-color:yellow;">Sno</th>
<th style="background-color:yellow;">Name</th>
<th style="background-color:yellow;">Marks</th>
<th style="background-color:yellow;">Age : Age Testing </th>
</tr>
<tr><td>1</td><td>Anvesh</td><td>90</td></tr>
<tr><td>2</td><td>Kapil</td><td>80</td></tr>
<tr><td>3</td><td>Mahesh</td><td>70</td></tr>
<tr><td>4</td><td>Sravan</td><td>90</td></tr>
<tr><td colspan='2'>Total</td><td>330</td></tr>
</table>
</div>
<input type="hidden" name="tableval" id="tableval" />
</form>
**exportexcel.php**
<?php
$filename = "Student_Info".time(); //File Name
$file_ending = "xls";
//header info for browser
header("Content-Type: application/xls");
header("Content-Disposition: attachment; filename=$filename.xls");
header("Pragma: no-cache");
header("Expires: 0");
Echo $_REQUEST['tableval'];
?>
Now it's working fine.But i have a doubt
<th style="background-color:yellow;">Age : Age new Testing </th>
Here Age is My heading and Age new Testing is my description. Is there any way to remove description from <th>. I mean i need only Heading and i want to remove description? Please help me?
I generating a QR CODE with url as data content, but when i try to print a page where the qr is generated, the qr does not seem to appear yet other content displays. Here is my snippet
<?php
require_once("qrcode.php");
//---------------------------------------------------------
$qr = new QRCode();
// ƒGƒ‰[’ù³ƒŒƒxƒ‹‚ðÝ’è
// QR_ERROR_CORRECT_LEVEL_L : 7%
// QR_ERROR_CORRECT_LEVEL_M : 15%
// QR_ERROR_CORRECT_LEVEL_Q : 25%
// QR_ERROR_CORRECT_LEVEL_H : 30%
$qr->setErrorCorrectLevel(QR_ERROR_CORRECT_LEVEL_L);
$qr->setTypeNumber(4);
$qr->addData("http:/".$_SERVER['REQUEST_URI']."");
$qr->make();
//---------------------------------------------------------
?>
and below is the other content on the page
<div class="invoice-box" id="invoice-box">
<table cellpadding="0" cellspacing="0">
<tr class="top">
<td colspan="2">
<table>
<tr>
<td class="title">
<img src="images/logo.png" style="width:100%; max-width:200px; height:95px;">
</td>
<td>
Invoice #: <?php print $invoice_details->sub_code ?><br>
Created: <?php print date('Y/M/d', strtotime($invoice_details->paid_date)) ?><br>
Due: February 1, 2015
</td>
</tr>
</table>
</td>
</tr>
<tr class="information">
<td colspan="2">
<table>
<tr>
<td>
<br>
<br>
</td>
<td>
<?php print $invoice_details->org ?><br>
<?php print $invoice_details->lname ?> <?php print $invoice_details->fname ?><br>
<?php print $invoice_details->email ?>
</td>
<td>
<?php $qr->printHTML(); ?>
</td>
</tr>
</table>
</td>
</tr>
<tr class="heading">
<td>
Payment Method
</td>
<td>
</td>
</tr>
<tr class="details">
<td>
<?php print $invoice_details->method ?>
</td>
<td>
</td>
</tr>
<tr class="heading">
<td>
Item
</td>
<td>
Price(UGX)
</td>
</tr>
<tr class="item">
<td>
<?php print ucfirst($invoice_details->event) ?> - Summit
</td>
<td>
<?php print number_format($invoice_details->amount) ?>
</td>
</tr>
<tr class="total">
<td></td>
<td>
Total: UGX <?php print number_format($invoice_details->amount) ?>
</td>
</tr>
</table>
</div>
<input type="button" value="Print Div" onclick="PrintElem('#invoice-box')" />
and my printing script
<script type="text/javascript">
function PrintElem(elem)
{
Popup($(elem).html());
}
function Popup(data)
{
var mywindow = window.open('', 'invoice-box', 'height=400,width=600');
mywindow.document.write('<html><head><title>my div</title>');
/*optional stylesheet*/ //mywindow.document.write('<link rel="stylesheet" href="main.css" type="text/css" />');
mywindow.document.write('</head><body >');
mywindow.document.write(data);
mywindow.document.write('</body></html>');
mywindow.document.close(); // necessary for IE >= 10
mywindow.focus(); // necessary for IE >= 10
mywindow.print();
mywindow.close();
return true;
}
</script>
Assuming I found the correct library, it has an image creator. What you need to do is use an output buffer around the imagegif (or imagepng) call to capture the image's binary data into a string, then base64 encode the string, and echo that as part of a data URI directly in the src attribute of an img tag.
$img = $qr->createImage(4, 2);
ob_start();
imagegif($img);
imagedestroy($img);
$img = ob_get_clean();
In your HTML
<img src="data:image/gif;base64,<?php echo base64_encode($img);?>">
If you're concerned about support for data URLs, leave out the output buffering, add a unique, random filename as the second parameter to the imagegif call to save it, then echo that filename in the src.
It seems that you're trying to print a black background color which is removed by default from printing. Unfortunately there's no way to force it back programatically in browsers other than Safari and Chrome.
You can force this back for Safari and Chrome by doing:
<div class="invoice-box" id="invoice-box">
<style>
.invoice-box table td: {
-webkit-print-color-adjust: exact !important;
}
</style>
<table cellpadding="0" cellspacing="0">
<tr class="top">
<td colspan="2">
<table>
<tr>
<td class="title">
<img src="images/logo.png" style="width:100%; max-width:200px; height:95px;">
</td>
<td>
Invoice #: <?php print $invoice_details->sub_code ?><br>
Created: <?php print date('Y/M/d', strtotime($invoice_details->paid_date)) ?><br>
Due: February 1, 2015
</td>
</tr>
</table>
</td>
</tr>
<tr class="information">
<td colspan="2">
<table>
<tr>
<td>
<br>
<br>
</td>
<td>
<?php print $invoice_details->org ?><br>
<?php print $invoice_details->lname ?> <?php print $invoice_details->fname ?><br>
<?php print $invoice_details->email ?>
</td>
<td>
<?php $qr->printHTML(); ?>
</td>
</tr>
</table>
</td>
</tr>
<tr class="heading">
<td>
Payment Method
</td>
<td>
</td>
</tr>
<tr class="details">
<td>
<?php print $invoice_details->method ?>
</td>
<td>
</td>
</tr>
<tr class="heading">
<td>
Item
</td>
<td>
Price(UGX)
</td>
</tr>
<tr class="item">
<td>
<?php print ucfirst($invoice_details->event) ?> - Summit
</td>
<td>
<?php print number_format($invoice_details->amount) ?>
</td>
</tr>
<tr class="total">
<td></td>
<td>
Total: UGX <?php print number_format($invoice_details->amount) ?>
</td>
</tr>
</table>
</div>
<input type="button" value="Print Div" onclick="PrintElem('#invoice-box')" />
For browsers like IE and Firefox unfortunately this requires user interaction. See this question for more answers: Printing background-color in Firefox and IE
Finally, i got a way around this, here is my solution
<?php
require_once("qrcode.php");
$qr = QRCode::getMinimumQRCode("http:/".$_SERVER['REQUEST_URI']."", QR_ERROR_CORRECT_LEVEL_L);
$img = $qr->createImage(4, 2);
ob_start();
imagegif($img);
imagedestroy($img);
$img = ob_get_clean();
?>
Then output the qrcode as image with this;
<img src="data:image/gif;base64,<?php echo base64_encode($img);?>">
Thanks to #walf for his solution somehow, hope it helps someone else.
I'm trying to create a simple form to show a basic table and a delete query...
I can get the delete query working but when I use an include it does not work, if i run the 'display.php' file alone in browser it runs perfectly and shows the database...
Can anyone advise what would be best way incorporate the display.php file in this html file? Or explain the issue?
<!DOCTYPE html>
<div id="content">
<?php include('display.php')?>
<div id="sidebar"></div>
<div id="footer">
<h5></h5>
</div>
<table>
<tr>
<td>
<form action="delete.php" method="post"></form>
<table border="0" cellpadding="3" cellspacing="1" style="background-color: #536977" width="100%">
<tr>
<td width="150">Enter Username to be deleted</td>
<td width="6">:</td>
<td width="294"><input name="delusername" type="text"></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" value="submit"></td>
</tr>
</table>
</td>
</tr>
</table>
</div>
</div>
<?php require_once('display.php'); ?>
Presuming they are in the same folder.
I am trying to export an HTML existing table to MS Excel with assigned Name. As you know exporting HTML Table to Excel file can be achieved by jQuery BUT as I said I have to add a Name to File as well.
HTML
<button id="myButtonControlID">Export Table to Excel</button>
<div id="divTableDataHolder">
<title>Demo for huge data</title>
<table>
<tbody>
<tr>
<td>abc</td>
<td>12</td>
</tr>
<tr>
<td>abc</td>
<td>12</td>
</tr>
</tbody>
</table>
</div>
So Far I have a jquery which pass data from html to PHP file as:
$( document ).ready(function() {
var naem = "MyExcel";
$("[id$=myButtonControlID]").click(function (e) {
window.open('getMyXSL.php?data=' + encodeURIComponent($('div[id$=divTableDataHolder]').html())+'&name='+name);
e.preventDefault();
});
});
and in getMyXSL.php file I have
<?php
header('Content-Disposition: attachment; filename="'.$_REQUEST['name'].'"');
header('Content-Type: application/vnd.ms-excel');
echo($_REQUEST['data']);
?>
but when I the buton clicked file generates/download a PHP file called getMyXSL.php which looks like this
<title>Demo for huge data</title>
<table>
<tbody>
<tr>
<td>abc</td>
<td>12</td>
</tr>
<tr>
<td>abc</td>
<td>12</td>
</tr>
</tbody>
</table>
Can you please let me know how to fix this and how I can export the table to .xls with a name? Thanks
Extension of output file must be xls, but your script is not generates this correctly. For fix it, you need to change header information:
<?php
$filename = sprintf('%s.xls', rawurlencode(preg_replace('~&#(\d{3,8});~e', '$fixchar(\'$1\')', $_REQUEST['name'])));
header('Content-Disposition: attachment; filename="'.$filename.'";' );
header('Content-Type: application/vnd.ms-excel');
echo($_REQUEST['data']);
?>
<table border="1" id="ReportTable" class="myClass">
<tr bgcolor="#CCC">
<td width="100">Fecha</td>
<td width="700">Seguimiento</td>
<td width="170">Producto</td>
<td width="30"> </td>
</tr>
<tr bgcolor="#FFFFFF">
<td><?php
$date = date_create($row_Recordset3['fecha']);
echo date_format($date, 'd-m-Y');
?></td>
<td><?php echo $row_Recordset3['descripcion']; ?></td>
<td><?php echo $row_Recordset3['producto']; ?></td>
<td><img src="borrar.png" width="14" height="14" class="clickable" nClick="eliminarSeguimiento(<?php echo $row_Recordset3['idSeguimiento']; ?>)" title="borrar"></td>
</tr>
</table>
<input type="hidden" id="datatodisplay" name="datatodisplay">
<input type="submit" value="Export to Excel">
In the onSubmit event of form, I am appending the div with ReportTable table. Using clone() method of jquery, I am making the clone of the ReportTable table that I wanted to export to excel, and in the end I am assigning the html of that cloned table to hidden field available in my form so that when form is posted then the data will be transferred to the exporttoexcel.php web page and in that web page I have all my logic for exporting data to excel in php.
Note: - For this example to run properly, there must be no declaration of inline css in your table or div that you want to export. Whenever you use the style attribute to give inline css, the export to excel functionality will not be performed according to your expectations.
exporttoexcel.php
<?php
header('Content-Type: application/force-download');
header('Content-disposition: attachment; filename=export.xls');
// Fix for crappy IE bug in download.
header("Pragma: ");
header("Cache-Control: ");
echo $_REQUEST['datatodisplay'];
?>
var naem = "MyExcel";
It should be name, that is the error.
I have a HTML code like this:
<div id="invoice">
<div id="invoice-header"> <img alt="Mainlogo_large" class="logo screen" src="http://colosus.com/invoice/images/freeagent_logo.png">
<!-- hCard microformat -->
<div class="vcard" id="company-address">
<div class="fn org"><strong>KIOSBAN.COM</strong></div>
<div class="adr">
<div class="street-address">Gedung Atlantica Lt.4<br>
Jl. Kuningan Barat No. 7, Mampang<br>
</div>
<!-- street-address -->
<div class="locality">Indonesia</div>
<div id="company-postcode"><span class="region">Jakarta Selatan</span> <span class="postal-code">12710</span></div>
</div>
<!-- adr -->
<div class="email">fajar#kiosban.com</div>
<div id="sales-tax-reg-number">Fajar tampan</div>
</div>
<!-- company-address vcard -->
</div>
<!-- #invoice-header -->
<div id="invoice-info">
<h2>Invoice <strong>INV001</strong></h2>
<h3>21 February 2008</h3>
<p id="payment-terms">Payment Terms: 30 days</p>
<p id="payment-due">Payment due by 21 March 2008</p>
<p id="payment-total">£2643.75</p>
</div>
<!-- #invoice-info -->
<div class="vcard" id="client-details">
<div class="fn">John Doe</div>
<div class="org">Client Company</div>
<div class="adr">
<div class="street-address"> Client Street Address<br>
Street Address 2<br>
Street Address 3<br>
</div>
<!-- street-address -->
<div class="locality">LOCALITY</div>
<div id="client-postcode"><span class="region">Region</span> <span class="postal-code">MV2 8SX</span></div>
<div id="your-tax-number">SALES TAX: 193528491</div>
</div>
<!-- adr -->
</div>
<!-- #client-details vcard -->
<table id="invoice-amount">
<thead>
<tr id="header_row">
<th class="quantity_th">Quantity</th>
<th class="left details_th">Details</th>
<th class="unitprice_th">Unit Price (£)</th>
<th class="salestax_th">VAT</th>
<th class="subtotal_th">Net Subtotal (£)</th>
</tr>
</thead>
<tfoot>
<tr id="discount_tr">
<td colspan="2"> </td>
<td colspan="2" class="item_r">10% Discount</td>
<td class="item_r">£250.00</td>
</tr>
<tr id="net_total_tr">
<td colspan="2"> </td>
<td colspan="2" class="item_r">Net Total</td>
<td class="item_r">2250.00</td>
</tr>
<tr id="vat_tr">
<td colspan="2"> </td>
<td colspan="2" class="item_r">VAT</td>
<td class="item_r">393.75</td>
</tr>
<tr id="total_tr">
<td colspan="2"> </td>
<td colspan="2" class="total" id="total_currency"><span class="currency">GBP </span> Total</td>
<td class="total">£2643.75</td>
</tr>
</tfoot>
<tbody>
<tr class="item odd">
<td class="item_l">1 Day</td>
<td class="item_l">Details of project activity to be billed </td>
<td class="item_r">500.00</td>
<td class="item_r">17.5%</td>
<td class="item_r">500.00</td>
</tr>
<tr class="item">
<td class="item_l">2 Days</td>
<td class="item_l">Other Details of project activity to be billed </td>
<td class="item_r">1000.00</td>
<td class="item_r">17.5%</td>
<td class="item_r">1000.00</td>
</tr>
<tr class="item odd">
<td class="item_l">2 Days</td>
<td class="item_l">More d etails of project activity to be billed </td>
<td class="item_r">1500.00</td>
<td class="item_r">17.5%</td>
<td class="item_r">1000.00</td>
</tr>
</tbody>
</table>
<!-- invoice-amount -->
<div id="invoice-other">
<h2>Other Information</h2>
<div id="company-reg-number"><strong>Company Registration Number:</strong> 9273109</div>
<div id="contract-number"><strong>Contract/PO:</strong> PO 87227643</div>
</div>
<!-- invoice-other -->
<div id="payment-details">
<h2>Payment Details</h2>
<div id="bank_name">Bank Name</div>
<div id="sort-code"><strong>Bank/Sort Code:</strong> 32-75-97</div>
<div id="account-number"><strong>Account Number:</strong> 28270761</div>
<div id="iban"><strong>IBAN:</strong> 973547</div>
<div id="bic"><strong>BIC:</strong> 220197</div>
<div id="payment-reference"><strong>Payment Reference:</strong> INV001</div>
</div>
<!-- payment-details -->
<div id="comments">Payment should be made by bank transfer or cheque made payable to John Smith.</div>
<!-- comments -->
</div>
My question is: how to convert that HTML code only element inside <div id='invoice'>bla bla bla</div> or maybe just the specified url of that page (ex: localhost/invoice/index.php) to a downloadable pdf?
I've been searching many pdf class libraries but I can't find a solution.
Can anyone give me a solution, idea or possible approach?
You can use this service http://pdfmyurl.com/ to have PDF downloaded for urls, like having them in this format - http://pdfmyurl.com/?url=http://facebook.com
I would recommend using Zend_Pdf, it can be given a page which is used as the template for the PDF and you can do all sorts of PDF stuff.
Try mPDF it is a great and easy to use tool. And you can take a look on this quick start tutorial smaizys.com
Try TCPDF http://www.tcpdf.org/ . It is a php class for generating pdfs. Read http://www.tcpdf.org/examples/example_006.phps
I suggest to used DOMPDF.
[https://github.com/dompdf/dompdf][1]
it had huge problems with tables. I factored out my large nested tables and it helped (before it was just consuming up to 128M of memory then dying--thats my limit on memory in php.ini) but it makes a complete mess of tables and doesn't seem to get images. The tables were just basic stuff with some border styles to add some lines at various points;