Cannot set TD width when viewing multiple HTML tables in Excel - php

Hello I write PHP header for downloading excel.
If I have 1 table I can set the width of <td> successfully by use width attribute
but if I have two table it's not working. How could I do? because I need multiple table in my excel page file
<?php
header("Content-Type: application/vnd.ms-excel; charset=TIS-620");
header('Content-Disposition: attachment; filename="report_schedule_teacher.xls"');#ชื่อไฟล์
?>
<html>
<head>
<meta http-equiv="content-type" content="application/xhtml+xml; charset=TIS-620" />
</head>
<body>
<table width="100%" style="border-collapse: collapse;overflow:wrap; font-size:9pt;">
<thead>
<tr>
<td width="300">Hello1</td>
<td width="400">Hello2</td>
</tr>
</thead>
<tbody>
<tr>
<td>World1</td>
<td>World2</td>
</tr>
</tbody>
</table>
<table width="100%" style="border-collapse: collapse;overflow:wrap; font-size:9pt;">
<thead>
<tr>
<td width="300">Why I cannot set width if I have multiple table</td>
<td width="400">Noooo</td>
</tr>
</thead>
<tbody>
<tr>
<td>kub</td>
<td>pom</td>
</tr>
</tbody>
</table>
</body>
</html>

Excel, when reading HTML files, really only has one HTML table. If you look at the Excel window you'll see that physically it's just a single table. If you insist on separating the data (which makes no difference to Excel) you can use multiple table bodies:
<html>
<head>
<meta http-equiv="content-type" content="application/xhtml+xml; charset=TIS-620" />
</head>
<body>
<table width="100%" style="border-collapse: collapse;overflow:wrap; font-size:9pt;">
<thead>
<tr>
<td width="300">Hello1</td>
<td width="400">Hello2</td>
</tr>
</thead>
<tbody>
<tr>
<td>World1</td>
<td>World2</td>
</tr>
</tbody>
<thead>
<tr>
<td width="300">Why I cannot set width if I have multiple table</td>
<td width="400">Noooo</td>
</tr>
</thead>
<tbody>
<tr>
<td>kub</td>
<td>pom</td>
</tr>
</tbody>
</table>
</body>
</html>
Output:
By the way, you are abusing MIME types terribly here! This document is not application/vnd.ms-excel or application/xhtml+xml, it's text/html and should not be saved with an XLS extension. Excel can read HTML files, but that doesn't make HTML files Excel files!

Related

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.

Prevent table resizing in PDF

I'm using mpdf to generate PDF from a form. In form I have an option to adding new rows to table. The problem is when count of rows is too big for generated PDF page. Then the table is resizing (it's smaller) instead of going to the next page.
This is mpdf code:
$mpdf=new mPDF('UTF-8','A4','','',20,15,48,25,10,10);
$mpdf->WriteHTML(generatePDF());
$mpdf->Output();
exit;
This is html table code:
function getHTMLStyle(){
$html ='<table class="items" width="100%" style="font-size: 9pt; border-collapse: collapse;" cellpadding="8">
<tr>
<td width="5%">A</td>
<td width="95%"><b>'.$a.'</b><br /><br /> '.$_POST['title'].'</td>
</tr>
<tr>
<td >B</td>
<td ><b>'.$b.'</b><br /><br /> '.$_POST['organizationName'].'</td>
</tr>
<tr>
<td >C</td>
<td></td>
<table class="items2" width="100%" page-break-before="always" >
<tr>
<td ><b>'.$c.'</b></td>'.addTableC().'
</tr>
</table>
</tr>
This is image with property view:
And this is an amage with wrong view:
How can I make a break in table and continue to another side?
Because you're incorrectly nesting tables -
<tr>
<td >C</td>
<td></td>
<table class="items2" width="100%" page-break-before="always" >
<tr>
<td ><b>'.$c.'</b></td>'.addTableC().'
</tr>
</table>
</tr>
The table should be inside the <td> tag, like so:
<tr>
<td>C</td>
<td>
<table class="items2" width="100%" page-break-before="always" >
<tr>
<td ><b>'.$c.'</b></td>'.addTableC().'
</tr>
</table>
</td>
</tr>

Scraping Webpage with DOMdocument and DOMXpath

I'm very new to this. I would like to extract a table from a page using PHP and return it's HTML after modifying the HREF values of all anchors.
Here is the table:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1255">
<link rel="stylesheet" type="text/css" href="../CssGraduateE.css">
<title></title>
</head>
<body>
<div>
<br>
<table class="main" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<br><span class="MainHeader">Subjects in Faculty - Electrical Engineering</span><br><br>
<table cellpadding="2" cellspacing="0" border="1" width="100%">
<tbody>
<tr>
<td><span class="SecondHeader"> Subject Number</span></td>
<td><span class="SecondHeader">Subject Name</span></td>
<td><span class="SecondHeader">Points</span></td>
<td><span class="SecondHeader">Semesters</span></td>
<td>Subject Site</td>
</tr>
<tr>
<td>46001 </td>
<td nowrap="">Engineering of Distributed Software Sys</td>
<td>3</td>
<td><br></td>
<td><a target="_newtab" href="http://www.thislinkisok.com/courses/046001">www</a></td>
</tr>
<tr>
<td>46002 </td>
<td nowrap="">Design and Analysis of Algorithms</td>
<td>3</td>
<td>B<br></td>
<td> <br></td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
<br>
<table border="0">
<tbody>
<tr>
<td>Last Update on :</td>
<td>Wednesday ,9 April 2014</td>
<td></td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
I know how to grab the table I want:
$query = $xpath->query('//table[#class="main"]//table[1]');
but how do I loop through all the links that begin with "../xxx" and modify them to something like this: "www.mynewlink.com/xxx" ?
At the end I would like to return the extracted table as HTML. How do I do this with native DOMDocument and DOMXpath?
Thanks All!
If $html is your string with HTML you get from the external website, you can do something like this:
$dom = new DOMDocument();
#$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
foreach($xpath->query('//table[#class="main"]//a[starts-with(#href, "../")]') as $link) {
$link->setAttribute('href', preg_replace('#^..#', 'http://www.mynewlink.com', $link->getAttribute('href')));
}
$container = new DOMDocument();
$container->appendChild($container->importNode($xpath->query('//table[#class="main"]')->item(0), true));
echo $container->saveHTML();

Send HTML email message in PHP

I'm trying to send an HTML formatted invoice but it is sending the message as plain text rather than formatted HTML.
The code is:
$this->load->library('email',$config);
$this->email->set_newline("\r\n");
$this->email->from('sample#email.com', 'Sample');
$this->email->to('sample2#email.com');
$this->email->cc('sample3#email.com');
$this->email->subject('Sample Test');
$this->email->message($message);
$this->email->send();
echo $this->email->print_debugger();
and the content:
$message ='
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<style>
<![CDATA[
body, td { color:#a6a7a3; font-size: 14px; font-family:Arial; font-weight:normal; text-decoration:none; }
a { color:#a6a7a3; font-weight:normal; text-decoration:none; }
table td { border-collapse: collapse;}
]]>
</style>
</head>
<body>
<table>
<tr>
<td>
<table>
<tr>
<td>
<!-- // Begin Template Header \ -->
<table>
<tr>
<td><!--IMAGE--></td>
</tr>
</table><!-- // End Template Header \ -->
</td>
</tr>
<tr>
<td>
<!-- // Begin Template Body \ -->
<table>
<tr>
<td>
<!-- // Begin Module: Standard Content \ -->
<table>
<tr>
<td>
<div>
<strong style="font-size:14px;font-family:Arial;">Dear Sample,</strong><br />
<br />
Thank you for being with us. <!-- Start Transaction Information -->
<table>
<tr>
<td></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td><strong>Sample</strong></td>
</tr>
</table>
<table>
<tr>
<td></td>
</tr>
<tr>
<td>Item Name</td>
<td>Quantity</td>
<td>Item Price</td>
<td>Item Code</td>
<td>Shipping</td>
</tr>
<tr>
<td>'.$item.'</td>
<td>'.$quantity.'</td>
<td>'.$price.'</td>
<td>'.$code.'</td>
<td>'.$shipping.'</td>
</tr>
</table><!-- End Transaction Information -->
</div>
</td>
</tr>
</table><!-- // End Module: Standard Content \ -->
</td>
</tr>
</table><!-- // End Template Body \ -->
</td>
</tr>
</table><br />
</td>
</tr>
</table><br />
</body>
</html>';
$this->email->set_mailtype("html");
Here you might also find this useful.. I was bored. You'll get much better device compatibility with this code..
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<meta content="telephone=no" name="format-detection" /><!-- ios -->
<meta content="on" http-equiv="cleartype" /><!-- Internet explorer -->
<style>
<![CDATA[
html,body,.ReadMsgBody, .ExternalClass,.ExternalClass * { background-color:#ffffff;width:100%!important;line-height:100%;margin:0;-webkit-text-size-adjust:100%; }
table,table td { border-collapse:collapse;mso-table-lspace:0pt;mso-table-rspace:0pt; }
img { display:block;border:0 none;outline:none;height:auto;line-height:100%;margin:0;padding:0;text-decoration:none;-ms-interpolation-mode:bicubic; }
.container { width:600px; }
#outlook a { padding:0; }
.yshortcuts,.yshortcuts a,.yshortcuts a:link,.yshortcuts a:visited,.yshortcuts a:hover,.yshortcuts a span { color:#a6a7a3;text-decoration:none!important;border-bottom:none!important;background:none!important;}
]]>
</style>
<title></title>
</head>
<body bgcolor="#FFFFFF" style="min-width:100%;margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;padding-top:0;padding-bottom:0;padding-left:0;padding-right:0;color:#a6a7a3;font-family:Arial,sans-serif;">
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<!-- // Begin Template Header \ -->
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td><!--IMAGE (style-display:block;!!)--></td>
</tr>
</table><!-- // End Template Header \ -->
</td>
</tr>
<tr>
<td>
<!-- // Begin Template Body \ -->
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<!-- // Begin Module: Standard Content \ -->
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td style="color:#a6a7a3;font-size:14px;font-family:Arial,sans-serif;text-align:left;">
<b>Dear Sample,</b><br />
</td>
</tr>
<tr>
<td style="color:#a6a7a3;font-size:14px;font-family:Arial,sans-serif;text-align:left;">
Thank you for being with us. <!-- Start Transaction Information -->
</td>
</tr>
<tr>
<td style="color:#a6a7a3;font-size:14px;font-family:Arial,sans-serif;text-align:left;">
<b>Sample</b>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table border="0" cellpadding="3" cellspacing="0"><tr><td height="3"><table border="0" cellpadding="0" cellspacing="0"><tr><td></td></tr></table></td></tr></table>
</td>
</tr>
<tr>
<td>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td style="text-align:left;font-family:Arial,sans-serif;font-size:14px;color:#a6a7a3;">Item Name</td>
<td style="text-align:left;font-family:Arial,sans-serif;font-size:14px;color:#a6a7a3;">Quantity</td>
<td style="text-align:left;font-family:Arial,sans-serif;font-size:14px;color:#a6a7a3;">Item Price</td>
<td style="text-align:left;font-family:Arial,sans-serif;font-size:14px;color:#a6a7a3;">Item Code</td>
<td style="text-align:left;font-family:Arial,sans-serif;font-size:14px;color:#a6a7a3;">Shipping</td>
</tr>
<tr>
<td style="text-align:left;font-family:Arial,sans-serif;font-size:14px;color:#a6a7a3;">'.$item.'</td>
<td style="text-align:left;font-family:Arial,sans-serif;font-size:14px;color:#a6a7a3;">'.$quantity.'</td>
<td style="text-align:left;font-family:Arial,sans-serif;font-size:14px;color:#a6a7a3;">'.$price.'</td>
<td style="text-align:left;font-family:Arial,sans-serif;font-size:14px;color:#a6a7a3;">'.$code.'</td>
<td style="text-align:left;font-family:Arial,sans-serif;font-size:14px;color:#a6a7a3;">'.$shipping.'</td>
</tr>
</table><!-- End Transaction Information -->
</td>
</tr>
</table><!-- // End Module: Standard Content \ -->
</td>
</tr>
</table><!-- // End Template Body \ -->
</td>
</tr>
</table>
</body>
</html>

Categories