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?
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 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!
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 can output a PHP table in an excel file using this code:
<?php
require("aacfs.php");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=Cancelled Flight Summary_sortbydate.xls");
header("Pragma: no-cache");
header("Expires: 0");
echo "<center>Ayala Aviation Corporation</center>";
echo "<center><b>Cancelled Flight Summary</b></center>";
echo "<center>For the period ________</center></br></br>";
$result=mysql_query("select * from reservation where status='Cancelled' order by bdate");
echo "<center></br></br><table border=1 class='imagetable'><tr>
<th>Flight Date</th>
<th>Client</th>
<th>Group Code</th>
<th>Itinerary</th>
<th>ETD</th>
<th>Aircraft</th>
<th>ETA</th>
<th>Reservation No.</th>
<th>Last Status</th>
<th>Remarks</th>
<th>Reserved By</th>
</tr>";
if(mysql_num_rows($result)>0)
{
while($row=mysql_fetch_array($result))
{
$rvno=$row['reservno'];
$yr=(string) date("Y");
$rn=$yr."-".$rvno;
echo"<tr><td>".$row['fdate']."</td><td>".$row['cliename']."</td><td>".$row['grpcode']."</td><td>".$row['itinerary']."</td><td>".$row['etd']."</td><td>".$row['acode']."</td><td>".$row['eta']."</td><td>".$rn."</td><td>".$row['lstatus']."</td><td>".$row['remarks']."</td><td>".$row['adminid']."</td></tr>";
}
}
else
{ ?>
<tr><td colspan="11" style="text-align:center; color:#FF0000; font-size:16px;">*No Data Available!*</td></tr>
<?php
}
?><?php
?>
I tried this,
<?php
require("aacfs.php");
header("Content-type: application/pdf");
header("Content-Disposition: attachment; filename=Cancelled Flight Summary_sortbydate.pdf");
header("Pragma: no-cache");
header("Expires: 0");
echo "<center>Ayala Aviation Corporation</center>";
echo "<center><b>Cancelled Flight Summary</b></center>";
echo "<center>For the period ________</center></br></br>";
$result=mysql_query("select * from reservation where status='Cancelled' order by bdate");
echo "<center></br></br><table border=1 class='imagetable'><tr>
<th>Flight Date</th>
<th>Client</th>
<th>Group Code</th>
<th>Itinerary</th>
<th>ETD</th>
<th>Aircraft</th>
<th>ETA</th>
<th>Reservation No.</th>
<th>Last Status</th>
<th>Remarks</th>
<th>Reserved By</th>
</tr>";
if(mysql_num_rows($result)>0)
{
while($row=mysql_fetch_array($result))
{
$rvno=$row['reservno'];
$yr=(string) date("Y");
$rn=$yr."-".$rvno;
echo"<tr><td>".$row['fdate']."</td><td>".$row['cliename']."</td><td>".$row['grpcode']."</td><td>".$row['itinerary']."</td><td>".$row['etd']."</td><td>".$row['acode']."</td><td>".$row['eta']."</td><td>".$rn."</td><td>".$row['lstatus']."</td><td>".$row['remarks']."</td><td>".$row['adminid']."</td></tr>";
}
}
else
{ ?>
<tr><td colspan="11" style="text-align:center; color:#FF0000; font-size:16px;">*No Data Available!*</td></tr>
<?php
}
?><?php
?>
And it does download a PDF file but it can't be opened.
Is it really impossible to output the PHP table in a PDF file the same way it does in the above code? Or I'm just doing something wrong on my 2nd code? This way is the easiest for me so I'm eager to know if it is impossible (if it is, then why?) or possible. And please point me out to an easy alternative to achieve this in case it is not possible, since I'm new to generating external files through PHP. Any help would do. Thanks.
You can't just echo out HTML and hope it will magically make a PDF binary because you sent a PDF header. Look at the PHP PDF library (http://php.net/manual/en/book.pdf.php) or other PHP-based library for creating PDF's.
Similarly, I am shocked that outputting HTML like that with an Excel header will create a usable Excel file. It must be through lucky compatibility with Excel that this would work. You should use a real Excel library (or create a CSV file compatible with Excel) if you want to create Excel files in a more reliable fashion.
I have a products page list and i am exporting it to a word document and also allowing user to print the document window.print... I click ExportToWord button saves as dialog opens and i canceled it... Next when i click print button the print dialog appears for a second and minmizes and the Saves as dialog appears containing the word document...
For word:
else if($btn_value == 'word')
{
$nam = 'StudentSeatAllot';
$this->load->library('table');
$getCollege = $this->adminmodel->getUserCollege();
$data['viewseatdet']=$this->adminmodel->seatallotreport($college);
$this->table->set_heading('Degree','Department','Batch',
'Category','Seatsalloted');
$out = $this->table->generate($data['viewseatdet']);
//header("Content-type: application/x-ms-download");
//header("Content-disposition: attachment; filename=$cur_date.doc");
header("Content-Type: application/vnd.ms-word");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-disposition: attachment; filename=$nam.doc");
$collegeName = $getCollege[2];
$collegeAddr1 = $getCollege[5].', '.$getCollege[6].' - '.$getCollege[7];
$collegeAddr2 = $getCollege[10].', '.$getCollege[8];
echo '<b><center>'.strtoupper($collegeName).'</b>';
echo '<br><b>'.strtoupper($collegeAddr1);
echo '<br><b>'.strtoupper($collegeAddr2).'</center>';
echo '<br><br>';
echo 'S E A T A L L O T - D E T A I L S<br><br>';
print_r($out);
}
For print i am using javascript:
function tablePrint()
{
var display_setting="toolbar=no,location=no,directories=no,menubar=no,";
display_setting+="scrollbars=yes,,status=no,width=750, height=600, left=100, top=25";
var content_innerhtml = document.getElementById("tbl_display").innerHTML;
var document_print=window.open("","",display_setting);
document_print.document.open();
document_print.document.write('<html><head><title>Campus Unified</title><link rel="stylesheet" type="text/css" href="<?=base_url()?>stylesheets/stylesheet.css" /> </head>');
document_print.document.write('<body onLoad="self.print();" >');
document_print.document.write('<center><strong>');
document_print.document.write(content_innerhtml);
document_print.document.write('</strong></center>');
document_print.document.write('</body></html>');
document_print.document.close();
}
![alt text][1]
Second EDIT:
My print button,
<button type="submit" class="print" name="print" value="print" onfocus="this.blur();"
onclick="tablePrint();">
and content_innerhtml
<div id="tbl_display" style="display:none" >
<? if(isset($viewseatdet_print)) { ?>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td colspan="3" align="right" style="padding-right:5px" class="table_label">Date : <?= date('d-m-Y') ?></td>
</tr>
<tr><td colspan="3" class="table_label" align="center">
<? if(isset($_SESSION['empCollege'])) { $userCollege = $_SESSION['empCollege'];
echo strtoupper($userCollege[2]).'<br>'.strtoupper($userCollege[5]);
if(!empty($userCollege[6])){ echo ', '.$userCollege[6]; }
if(!empty($userCollege[7])){ echo ' - '.$userCollege[7]; }
if( (sizeof($userCollege))==11)
{ echo '<br>'.strtoupper($userCollege[9]).', '.strtoupper($userCollege[8]); }
}?>
</td></tr>
</table>
<table ><tr height="10px"><td> </td></tr></table>
<div style="padding-left:4px" class="table_label_header" align="center">
S E A T S - A L L O T M E N T
</div>
<table width="500px" cellpadding="4" cellspacing="0" border="1" >
<tr>
<td style="width:10%" class="grid_header" align="center">Degree</td>
<td style="width:15%" class="grid_header" align="center">Department</td>
<td style="width:15%" class="grid_header" align="center">Category</td>
<td style="width:10%" class="grid_header" align="center">Seatsalloted</td>
</tr>
<? if(empty($viewseatdet_print))
{ echo '<tr><td colspan=2>no record found !</td></tr>'; } ?>
<? foreach($viewseatdet_print as $row) { ?>
<tr>
<td style="width:10%" class="table_label" align="center"><?= $row['dDegreeName']?></td>
<td style="width:15%" class="table_label" align="center"><?= $row['dDepartmentName']?></td>
<td style="width:15%" class="table_label" align="center"><?= $row['dCategory']?></td>
<td style="width:15%" class="table_label" align="center"><?= $row['dSeatsAllot']?></td>
</tr>
<? } ?>
</table>
<? } ?>
</div>
Word HTML:
<button type="submit" class="word" name="word" value="word" onfocus="this.blur();"
onclick="return getButtonType('word');">
I assume there is a form around your posted button that gets called when you click the button. This wouls explain, why the 2 things happen, that you described
<button type="submit" [...] onclick="tablePrint(); return false; ">
The return false disables the submit action, that is attached to the button.