CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using CiviReport (Moderator: Dave Greenberg) »
  • Open report CSV in Excel with correct encoding
Pages: [1]

Author Topic: Open report CSV in Excel with correct encoding  (Read 1336 times)

theitd

  • I’m new here
  • *
  • Posts: 22
  • Karma: 1
    • the ITD
  • CiviCRM version: 3.4.7
  • CMS version: 1.15.26
  • MySQL version: 5.1.66
  • PHP version: 5.3
Open report CSV in Excel with correct encoding
December 01, 2011, 06:59:11 am
I've just been trying to solve an issue with CiviReportMail.php and CSV output - and I thought the solution may be useful for someone else.
When sending (or exporting) a CSV from CiviReport, the resulting file can't be opened in Excel without specifying the encoding used (Data - Import - From Text File etc.....)

Having seen this thread: http://forum.civicrm.org/index.php?topic=21654.0 by Tchiot Galopin, with a couple of minor tweaks, Excel on both a PC and Mac identify the CSV and open without a problem.  As well as the issue of opening, a bigger problem resulted from the GBP locale and using a £ sign (it ended up showing as "£" in Excel)

A solution is to edit /administrator/components/com_civicrm/civicrm/CRM/Report/Utils/Report.php from line 192
Code: (php) [Select]
// Add the headers.
        $csv .= implode(',', $headers) . "\n";

        $displayRows = array();
        $value       = null;
        foreach ( $rows as $row ) {
            foreach ( $columnHeaders as $k => $v ){
                if ( $value = CRM_Utils_Array::value( $v, $row ) ) {
                    // Remove HTML, unencode entities, and escape quotation marks.


                    // _________________________________________________________________Add the following line
                        $value = mb_convert_encoding ($value , "UTF-16LE", "UTF-8");

                    $value =
                        str_replace('"', '""', html_entity_decode(strip_tags($value)));

                    if ( CRM_Utils_Array::value( 'type', $form->_columnHeaders[$v] ) & 4 ) {
                        if ( CRM_Utils_Array::value( 'group_by', $form->_columnHeaders[$v] ) == 'MONTH' ||
                             CRM_Utils_Array::value( 'group_by', $form->_columnHeaders[$v] ) ==  'QUARTER' ) {
                            $value =  CRM_Utils_Date::customFormat( $value, $config->dateformatPartial );
                        } elseif ( CRM_Utils_Array::value( 'group_by', $form->_columnHeaders[$v] ) == 'YEAR' ) {
                            $value =  CRM_Utils_Date::customFormat( $value, $config->dateformatYear );
                        } else {
                            $value =  CRM_Utils_Date::customFormat( $value,'%Y-%m-%d' );
                        }
                    } else if ( CRM_Utils_Array::value( 'type', $form->_columnHeaders[$v] ) == 1024 ) {
                        $value =  CRM_Utils_Money::format( $value );


                        // _________________________________________________________________Add the following line
                        $value = mb_convert_encoding ($value , "UTF-16LE", "UTF-8");

                    }
                    $displayRows[$v] = '"'. $value .'"';
                } else {
                    $displayRows[$v] = " ";
                }
            }
            // Add the data row.
            $csv .= implode(',', $displayRows) . "\n";
        }

Thanks to Tchiot for the simple fix!
Too err is human, to really foul things up needs a computer.

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using CiviReport (Moderator: Dave Greenberg) »
  • Open report CSV in Excel with correct encoding

This forum was archived on 2017-11-26.