Export html table contents as xlsx format - php

Iam using PHP+jQuery to export the html table contents. When I click on on export button, the excel file is generated successfully as xls format. I want to generate the file as xlsx format.
$FileName="my-file.xls";
header('Content-Type: application/force-download');
header('Content-disposition: attachment; filename='.$FileName.'');
header("Pragma: ");
header("Cache-Control: ");
echo $_REQUEST['tableData'];
exit();
Adding the jQuery
var clone_val = $(".tblExport").clone();
$("#tData").val($('<span><style> .textFormat { mso-number-format:"\#"; } .numberFormat { mso-number-format:"0\.00" }</style>').append (clone_val).html());
$("#excel-export-form").submit();
The table contents is stored inside a input box having attribute id="tData" name="tableData" using jQuery.
When I changed the filename as my-file.xlsx, a corrupted xlsx file is generated. Also tried Data Table excel export, but it didn't support multiple headers.

You can do this with a php library. I prefer PhpSpreadsheet to use for working with excel.

Related

How to upload excel sheet image into database in php or in Laravel

I wonder how to upload excel sheet image into database in php more accurately in laravel.
For example i have an excel sheet which contain product information such as product name, price, product images etc.
Now how do i upload that image to server.
I know how to upload normal text such as product name, product price etc. but not sure how should i upload the image.
I did googling and check here and there but i didn't get any useful information
Can anyone give me some hint or suggestion or solution?
Thank you
Converting excel into a binary data and you can store those binary data in database.I had tried in mysql using blob data type.
$data = base64_encode(file_get_contents('Book2.xlsx'));
$data_result = base64_decode($data);
//to revert back to excel
header("Content-Type: application/vnd.ms-excel; charset=UTF-8");
header("Content-Disposition: inline; filename= test.xls");
header("Pragma: no-cache");
header("Expires: 0");
echo ($data_result);

PHP Postgtres Generate CSV File for Download

I have a postgresql database table and using PHP for the backend. On the user interface, I provide users with a way of generating reports. What I want to do is that when a user wishes to generate a report, a CSV file should be provided for download.
I already know how to generate a CSV file for results of a query, but now in this case I don't want the file to be saved on disk. Instead, it should be downloaded by the browser.
You simply need to provide a page for download...
#downloadCSV.php?reportID=1
header('Content-Type: text/plain');
header('Content-Disposition: attachment; filename="yourfile.csv"');
// do your postgresql query and put the result into $csv
echo $csv;
In this way you don't have to create a real file on your server
The second header Content-Disposition: attachment; forces the browser to download a file instead to show the content in a page

Download with smartphone generate xls file with PHPExcel

I have a big problem when I want to download on my smart phone a xls file generated by PHPExcel it me download a php file named my page, while on my pc it load properly.
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition:inline;filename=fichie.xls');
$writer->save('php://output');
exit();
There's two problems with your line that sets the content disposition.
It should have spaces between the words, which is a standard requirement for headers.
It should be attachment if you want the file to download, as opposed to display inline.
e.g.
header('Content-Disposition: attachment; filename=fichie.xls');

How to convert PHP CSV output into excel with rich font styles and CSS in PHP

I tried to put font styles and color in EXCEL XLS sheet generation from CSV. Even i explored lots of libraries to get Excelsheet with enriched format and style.
Is there any libraries by which I can get formatted Excel XLS sheet from CSV output in PHP
I usually use phpexcel. It supports xlsx as well as just xls formats.
You can generate a flat web page with a table that has your font styles. Then download the table in excel using the code below.
header("Pragma: public");
$display="attachment";
header('Content-Type: application/vnd.ms-excel');
header("Content-Disposition: $display; filename=$filename");
header("Cache-Control: no-cache");
I'd recommend good old http://pear.php.net/package/Spreadsheet_Excel_Writer/
require_once 'Spreadsheet/Excel/Writer.php';
$table = new Spreadsheet_Excel_Writer();
$table->setTempDir($files_base.'/tmp');
$font= 'HelmetCondensed';
$f_std =& $table->addFormat();
$f_std->setFontFamily($font);
$f_b_c =& $table->addFormat();
$f_b_c->setFontFamily($font);
$f_b_c->setBold();
$f_b_c->setAlign('center');;
$table->write(0, 0, 'normal', $f_std);
$table->write(0, 1, 'bold+centered', $f_b_c);
// send HTTP headers
$table->send('NAME.xls');
// Let's send the file
$table->close();

how to create excel file and read xls using php excel reader?

Create excel file dataexcel.xls using php. dataexcel.xls file has more data.
Then same dataexcel.xls file doesn't read php excel reader. Directly created .xls files are readable. But created excel file is not readable.
I have used http://sourceforge.net/projects/phpexcelreader excel reader class.
header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=dataexcel.xls");
header("Pragma: no-cache");
header("Expires: 0");
Any header content change?

Categories