Export multiple file PHPExcel - phpexcel

I would like to export several files with PHPExce, the problem is that the code works very well but only exports one file.
Here is my final code after my foreach loop :
header('Content-Type: application/vnd.ms-excel');
$objWriter = IOFactory::createWriter($Xls, 'Excel5');
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="test.xls"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');
Thanks

Related

auto generate attachment file missing in wp_mail()

I am trying to send a mail with an attachment which is generating automatically according to the subscription option. But every time the mail is being sent (in Spam) but the attachment is not. I searched to this topic here but all the solutions are related to the 'uploads' folder.
Here is my code ..
require ( ABSPATH . 'pdfcrowd.php');
try
{
// create an API client instance
$client = new Pdfcrowd("apiname", "apikay");
// convert a web page and store the generated PDF into a $pdf variable
$pdf = $client->convertFile( ABSPATH . 'invoice_html.php');
// set HTTP response headers
header("Content-Type: application/pdf");
header("Cache-Control: max-age=0");
header("Accept-Ranges: none");
header("Content-Disposition: attachment; filename=\"invoice.pdf\"");
//$to = $invoice_email;
$to = "moyen#gmail.com";
$subject = "Invoice for your online package.";
$message = "Message Body Invoice for your online package. Invoice for your online package. Invoice for your online package";
$headers = array('Content-Type: text/html; charset=UTF-8','From: My Site Name <uddin#gmail.com');
$attachments = $pdf;
// send the generated PDF
//echo $attachments;
$wp_mail = wp_mail( $to, $subject, $message, $headers, $attachments );
}
catch(PdfcrowdException $why)
{
echo "Pdfcrowd Error: " . $why;
}
any help?
N.B:Since I managed to output the pdf file in the browser to save but I would like to save this pdf file in a directory and then will send as attachment. What will be the best with this code above ?
Thanks
Finally I am able to send the pdf in email by the following modification code below....
1. First I just save the converted pdf in the uploads folder then
2. I take the pdf for the attachments
require ( ABSPATH . 'pdfcrowd.php');
try
{
// create an API client instance
$client = new Pdfcrowd("apiname", "apikay");
// converted php file and store the generated PDF inside uploads
$fd = fopen( ABSPATH . 'wp-content/uploads/invoice.pdf', 'wb');
$client->convertFile( ABSPATH . 'invoice_html.php', $fd );
fclose($fd);
// set HTTP response headers
header("Content-Type: application/pdf");
header("Cache-Control: max-age=0");
header("Accept-Ranges: none");
//header("Content-Disposition: attachment; filename=\"invoice.pdf\"");
//$to = $invoice_email;
$to = "moyen#gmail.com";
$subject = "Invoice for your online package.";
$message = "Message Body Invoice for your online package. Invoice for your online package. Invoice for your online package";
$headers = array('Content-Type: text/html; charset=UTF-8','From: My Site Name <uddin#gmail.com');
// take the file from the uploads folder
$attachments = array( ABSPATH . '/wp-content/uploads/invoice.pdf' );
// send the generated PDF
//echo $attachments;
$wp_mail = wp_mail( $to, $subject, $message, $headers, $attachments );
}
catch(PdfcrowdException $why)
{
echo "Pdfcrowd Error: " . $why;
}

How to save the generated csv file by my plugin in WordPress?

I am creating a plugin that will generate a CSV file of users, I want to save the generated csv file with unique name.
My current code:-
$output_filename = 'members-' . date('F j, Y') . '.csv';
$output_handle = #fopen( 'php://output', 'w' );
header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
header( 'Content-Description: File Transfer' );
header( 'Content-type: text/csv' );
header( 'Content-Disposition: attachment; filename=' . $output_filename );
header( 'Expires: 0' );
header( 'Pragma: public' );
fputcsv( $output_handle, $header_row );
foreach ( $data_rows as $data_row ) {
fputcsv( $output_handle, $data_row );
}
fclose( $output_handle );
die();
If I understood correctly, you need to make this file downloadable from the browser?
If yes, try with this headers :
$output_filename = 'members-' . date('F j, Y') . '.csv';
// Disable caching
$now = gmdate("D, d M Y H:i:s");
header("Expires: Tue, 03 Jul 2001 06:00:00 GMT");
header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate");
header("Last-Modified: {$now} GMT");
// Force download
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
// Encoding on response body
header("Content-Disposition: attachment;filename={$filename}");
header("Content-Transfer-Encoding: binary");
fputcsv( $output_handle, $header_row );
foreach ( $data_rows as $data_row ) {
fputcsv( $output_handle, $data_row );
}
fclose( $output_handle );
die();

Wordpress - How to use the PHP header() function

I need to use PHP headers like header( 'Location: http://location.com' ); or header("Content-disposition: attachment; filename=$fileName"); from my wordpress plugin but it won't seem to how. I know that they need to be used before the page header is called, so I tried using the init hook:
add_action('init', 'test');
function test() {
header( 'Location: http://location.com' ) ;
}
but it didn't work.
If you want to redirect any page then use wp_redirect() method.. OR if you want to set the specific headers to make the content downloadable.. use below sample..code...
Suppose your url is like.. http://example.com/download/data.csv
add_action('template_redirect','yoursite_template_redirect');
function yoursite_template_redirect() {
if ($_SERVER['REQUEST_URI']=='/downloads/data.csv') {
header("Content-type: application/x-msdownload",true,200);
header("Content-Disposition: attachment; filename=data.csv");
header("Pragma: no-cache");
header("Expires: 0");
echo 'data';
exit();
}
}

How to save files to wordpress

I am running my wordpress on localhost, My code retrieves photos from urls and using file_put_contents add them to the current directory, but I need it to add them to /wordpress/wp-content/uploads where wordpress save the manually uploaded files.
$Address = "www.xxx.com/" . $file;
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_URL, $file);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$url = curl_exec($ch);
curl_close($ch);
$location = "/wordpress/wp-content/uploads/2013/a.jpg";
file_put_contents($location, file_get_contents($url));
if(! function_exists('wp_upload_dir'))
{
define('WP_USE_THEMES',false);
require 'wordpress/wp-load.php';
}
$upload_dir = wp_upload_dir();
echo $upload_dir['path'];
echo $upload_dir['url'];
echo $upload_dir['subdir'];
http://codex.wordpress.org/Function_Reference/wp_upload_bits
// the curl thing from question...results in `$url`
$upload = wp_upload_bits('a.jpg', null, file_get_contents($url));
echo $upload['file'], $upload['url'], $upload['error'];
I think I understand the problem.
This or any equivalent statement should be at the top of the file to load WP:
require_once("path/to/wp-load.php");
The path/to/ is relative and depends on the position of your file. If, for example, your file is located in a folder at wordpress/here, the path should be something like "../wp-load.php", but that's something you have to figure out.
Try displaying errors so you know what's going on, with a code like this one:
ini_set( 'display_errors', TRUE );
error_reporting( E_ALL );
Then, add this code:
$UploadDir = wp_upload_dir();
$UploadURL = $UploadDir['baseurl'];
$location = $UploadURL . "/2013/a.jpg";
Remove:
$location = "/wordpress/wp-content/uploads/2013/a.jpg"; and all code from:
if(! function_exists('wp_upload_dir')) {
down to the last line.
Insert echo statements in the appropriate places to see the results.

Drupal export data from table to excel

I am using the following 2 functions to export data from a table called eco_customers to excel file
but I have a problem with Arabic characters, they show corrupt, how can I modify the encoding to correct the Arabic characters problem in the excel file
function cleanData(&$str) {
$str = preg_replace("/\t/", "\\t", $str);
$str = preg_replace("/\r?\n/", "\\n", $str);
if(strstr($str, '"'))
$str = '"' . str_replace('"', '""', $str) . '"';
}
function customer_export(){
$filename = "cutomers_data_" . date('Ymd') . ".xls";
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: application/vnd.ms-excel, charset=UTF-8; encoding=UTF-8");
$flag = false;
$result = db_query("SELECT name, arabic_name, phones, fax, address, country, city, email, website, registration_no FROM eco_customers ") or die('Query failed!');
while(false !== ($row = db_fetch_array($result))) {
if(!$flag) {
// display field/column names as first row
echo implode("\t", array_keys($row)) . "\r\n";
$flag = true;
}
array_walk($row, 'cleanData');
echo implode("\t", array_values($row)) . "\r\n";
}
}
I used phpExcel library and everything worked fine for me now
function customer_export(){
require_once('Classes/PHPExcel.php');
$objPHPExcel = new PHPExcel();
$objPHPExcel->getActiveSheet()->setTitle('List of Customers');
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'Name')
->setCellValue('B1', 'Arabic Name')
->setCellValue('C1', 'Phone')
->setCellValue('D1', 'Fax')
->setCellValue('E1', 'Address')
->setCellValue('F1', 'Country')
->setCellValue('G1', 'City')
->setCellValue('H1', 'Email')
->setCellValue('I1', 'Website')
->setCellValue('J1', 'Registration Number');
$result = db_query("SELECT name, arabic_name, phones, fax, address, country, city, email, website, registration_no FROM eco_customers ") or die('Query failed!');
// Loop through the result set
$rowNumber = 2;
while ($row = db_fetch_array($result)) {
$col = 'A';
foreach($row as $cell) {
$objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$cell);
$col++;
}
$rowNumber++;
}
// Freeze pane so that the heading line won't scroll
$objPHPExcel->getActiveSheet()->freezePane('A2');
// Save as an Excel BIFF (xls) file
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="customers_data_' . date('Ymd') . '.xls"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');
exit();
}

Resources