Export Grid Data and Generate Excel File

Discussion about developing the ProcessMaker application and suggestions for improving it

Moderator: amosbatto

salman1538
Posts: 6
Joined: Thu Mar 09, 2017 3:10 am
Country: Saudi Arabia
Location: Riyadh

Export Grid Data and Generate Excel File

Unread postby salman1538 » Thu Nov 16, 2017 7:41 am

Hello,

I have Grid data and I need to generate excel sheet and send this sheet as email attachment using PMFSendMessage?

amosbatto
Posts: 5564
Joined: Mon Jun 22, 2009 10:28 am
Country: Bolivia
Location: La Paz

Re: Export Grid Data and Generate Excel File

Unread postby amosbatto » Thu Nov 16, 2017 10:59 pm

You can create a CSV file which Excel can import. Take this code and adapt it:
viewtopic.php?f=41&t=710430&p=790694#p790694

Here would be the adapted code:

Code: Select all

if (isset(@=productsList) and is_array(@=productsList) and count(@=productsList)) {
   
$aCsv = array();
   
//set the first row in the CSV file to the IDs of the grid fields
   
$aColumnHeaders array_keys(@=productsList[1]);
   
$line '';
   foreach (
$aColumnHeaders as $fieldId) {
      
$line .= (empty($line) ? '' ';') . $fieldId;
   }
   
$aCsv[] = $line;
   
   
//set each data row in CSV file:
   
foreach (@=productsList as $aRow) {
      
$line '';
           foreach (
$aColumnHeaders as $fieldId) {
         
$val $aRow[$fieldId];
         if (
is_array($val) or is_object($val)) {
           
$val json_encode($val);
         }
         
//sanitize CSV data to handle double quotation marks, commas, semicolons,
         //spaces and new lines
         
$val str_replace('"''""'$val);
           if (
preg_match('/[,;"\n\r]/'$val) or trim($val) != $val) {
              
$val '"' $val '"';
           }
         
$line .= (empty($line) ? '' ';') . $val;
      }
      
$aCsv[] = $line;
   }
   
   
//create a temporary output file:
   
$path tempnam(sys_get_temp_dir(), "products_") . ".csv";
   
file_put_contents($pathimplode("\n"$aCsv));
                
   
$aAttachFiles = array(
      
"products.csv" => $path
   
);
   
$usr userInfo(@@USER_LOGGED);
   
PMFSendMessage(@@APPLICATION'[email protected]'$usr['mail'], '''',
      
'Company Information''companyInfoTemplate.html', array(), $aAttachFiles); 
                
   
unlink($filename); //delete temporary CSV file
}   
 
Amos B. Batto ♦ ProcessMaker Technical Documentation Writer & Forum Manager ♦ Cofounder of ILLA (http://www.illa-a.org)


Return to “Development of the ProcessMaker application”

Who is online

Users browsing this forum: No registered users and 1 guest