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) »
  • Discussion »
  • Extensions (Moderators: mathieu, totten, kasiawaka) »
  • Extension to export to native Excel (xlsx)
Pages: [1] 2

Author Topic: Extension to export to native Excel (xlsx)  (Read 4834 times)

mathieu

  • Administrator
  • Ask me questions
  • *****
  • Posts: 620
  • Karma: 36
    • Work
  • CiviCRM version: 4.7
  • CMS version: Drupal
  • MySQL version: MariaDB 10
  • PHP version: 7
Extension to export to native Excel (xlsx)
August 25, 2014, 10:36:28 am
Hi,

I have added to the directory a new civicrm extension to export CiviReports directly to native excel (.xlsx) format:
https://civicrm.org/extensions/export-native-excel

You may already be familiar with the excellent "Export to Excel" extension that already existed:
https://civicrm.org/extensions/export-excel

The main differences between these two extensions are:

* "export to excel" exports in a form of HTML that is easier to import in MS Excel, but Excel still displays warnings to the user
* "export to excel" probably performs much better for large exports, since "export to native Excel" uses the PHPExcel library. PHPExcel is a bit slower than directly exporting to text/html format since it needs to generate a few files, compress them to .xlsx, etc..

Testing/feedback/code review much appreciated. I would very much like to qualify the extension for automatic distribution.

Mathieu

Edit: for background, see this thread: http://forum.civicrm.org/index.php/topic,32954.0.html
« Last Edit: August 25, 2014, 10:48:31 am by mathieu »
CiviCamp Montréal, 29 septembre 2017 | Co-founder / consultant / turn-key CiviCRM hosting for Quebec/Canada @ SymbioTIC.coop

SemperFi

  • I post frequently
  • ***
  • Posts: 177
  • Karma: 3
  • CiviCRM version: 4.5.4
  • CMS version: Wordpress 4.0.1
  • MySQL version: MySQL 5.5.16
  • PHP version: PHP 5.3.17
Re: Extension to export to native Excel (xlsx)
August 25, 2014, 10:32:33 pm
Hello Mathieu,

Thanks for this release. I've tested it with the 4.4.6 and 4.5 beta 7 versions of civicrm on Wordpress 9.3.2.

In both case I found out that instead of exporting all the results of a report (I used Constituent Summary), it only exported the first 50 results (the one displayed on the first page of the report). I'm not sure if this is the expected behavior but I'd prefer to be able to export all results once, instead of generating as many xlsx files as my report has pages.

The 50 first results were correctly exported with following columns : Contact Name; Contact Type; Contact ID; Email; Street Address; City; Country; Phone. It went smooth and fast, no warning when opening on Excel and special characters correctly managed (file is encoded in ANSI, not UTF-8).

I don't know how much work it represents but having this "Export" to xlsx function for "Search results" would be great as today csv format is really a pain in the ass + we, French people with special characters in our alphabet, need to change every csv extract from "utf-8 without bom" to "utf-8" using Notepad++ (step not required with your extension -> it saves time).

Hope this helps.
Thanks again for sharing

mathieu

  • Administrator
  • Ask me questions
  • *****
  • Posts: 620
  • Karma: 36
    • Work
  • CiviCRM version: 4.7
  • CMS version: Drupal
  • MySQL version: MariaDB 10
  • PHP version: 7
Re: Extension to export to native Excel (xlsx)
August 26, 2014, 08:01:29 am
Thanks for the feedback! Indeed I had not noticed the issue with the 50 rows limit. I'll check it out when I have a moment.

I also have experimental "Search" export in the git repository (commited yesterday), but it's highly experimental. It also doesn't give the option for CSV or Excel, but always exports Excel (I would prefer to have a file format option, although I guess it's not a strong requirement, since most people probably just want Excel anyway).
CiviCamp Montréal, 29 septembre 2017 | Co-founder / consultant / turn-key CiviCRM hosting for Quebec/Canada @ SymbioTIC.coop

mathieu

  • Administrator
  • Ask me questions
  • *****
  • Posts: 620
  • Karma: 36
    • Work
  • CiviCRM version: 4.7
  • CMS version: Drupal
  • MySQL version: MariaDB 10
  • PHP version: 7
Re: Extension to export to native Excel (xlsx)
August 26, 2014, 10:02:24 am
Aha, found/fixed the 50 row limit issue. I pushed a new release to github, which includes also the experimental code for search export.
CiviCamp Montréal, 29 septembre 2017 | Co-founder / consultant / turn-key CiviCRM hosting for Quebec/Canada @ SymbioTIC.coop

SemperFi

  • I post frequently
  • ***
  • Posts: 177
  • Karma: 3
  • CiviCRM version: 4.5.4
  • CMS version: Wordpress 4.0.1
  • MySQL version: MySQL 5.5.16
  • PHP version: PHP 5.3.17
Re: Extension to export to native Excel (xlsx)
August 26, 2014, 11:12:06 am
Thanks Mathieu for the fix.

I've successfully tested this new release on 4.4.6 and 4.5 beta 7 versions of civicrm for Wordpress 9.3.2.
Ran both a report -> export to XLSX and a search -> export to XLSX and it worked perfectly well in both case for a panel of around 750 contacts, civicrm standard fields+custom fields, up to 81 columns (Civicrm PRIMARY fields mapping).

The only bug I noticed is that some of our custom fields are larger that 255 characters, and they're truncated in the extracted Excel file. This is strange because it was a cell size limitation from the old XLS format, that should not happen with xlsx format as you can manually type more than 255 in a cell of an xslx workbook. Same issue than with the export to HTML extension : http://forum.civicrm.org/index.php/topic,33884.0.html

Could you have a look at that please, if this can be fixed I think will definitely adopt it. This is very good work.
What remains on the "search export" part ?

Thanks

PS : I'd suggest to think about having this extension being embedded in civicrm core code, not as an extension, so that it's the default export format, not the annoying CSV !
« Last Edit: August 26, 2014, 11:22:26 am by SemperFi »

mathieu

  • Administrator
  • Ask me questions
  • *****
  • Posts: 620
  • Karma: 36
    • Work
  • CiviCRM version: 4.7
  • CMS version: Drupal
  • MySQL version: MariaDB 10
  • PHP version: 7
Re: Extension to export to native Excel (xlsx)
August 26, 2014, 01:48:07 pm
Thanks for the exhaustive testing!

Regarding the 255 character limit: I tested with the equivalent of 4 pages of text and opened the exported result in LibreOffice. It made LibreOffice freeze a few times, but the entire text was there. I don't have a copy of MS Excel to test with at the moment.

Does the column have HTML in it?

When searching on forums, there are some mentions that the export library should call "$export->setVersion(8)", however that seems to refer to the BIFF8 format of Excel < 2003 (binary format).
CiviCamp Montréal, 29 septembre 2017 | Co-founder / consultant / turn-key CiviCRM hosting for Quebec/Canada @ SymbioTIC.coop

SemperFi

  • I post frequently
  • ***
  • Posts: 177
  • Karma: 3
  • CiviCRM version: 4.5.4
  • CMS version: Wordpress 4.0.1
  • MySQL version: MySQL 5.5.16
  • PHP version: PHP 5.3.17
Re: Extension to export to native Excel (xlsx)
August 26, 2014, 10:05:31 pm
These fields have the following structure: varchar(530)   - utf8_unicode_ci and none of them contain html, it's only plain text.
Standard varchar format is 255 characters, there is maybe something to look at here to remove limitation that would be based on this default size.

Thanks

SemperFi

  • I post frequently
  • ***
  • Posts: 177
  • Karma: 3
  • CiviCRM version: 4.5.4
  • CMS version: Wordpress 4.0.1
  • MySQL version: MySQL 5.5.16
  • PHP version: PHP 5.3.17
Re: Extension to export to native Excel (xlsx)
August 27, 2014, 07:41:46 am
I've tested to export the "Notes" associated to a contact. One of them was 287 characters length and it has been correcly exported without being truncated at 255. The difference between the "note" (from civicrm_note table) field and my custom field is the type : text (no length specified) vs. varchar (530).

It seems then that the default varchar value (255) is the root cause of the limitation, not the export to Excel tool itself

Two options :
-> can it be bypassed ?
-> can I modified my field type in the custom field table to "text" instead of "varchar" without harming the content already existing ?

EDIT : I've added a new custom field VARCHAR(500). I've put a value for this field with 499 characters and exported it. It worked well when opening the file in Excel. Difference is that I'm using Excel 2013, vs. 2007 so I'm wondering if there is not a Excel version related issue... I'll check and keep you posted.

For both 2007 and 2013, the total number of characters that a cell can contain is: 32,767 characters.
see :
http://office.microsoft.com/en-us/excel-help/excel-specifications-and-limits-HP010073849.aspx
http://office.microsoft.com/en-us/excel-help/excel-specifications-and-limits-HA103980614.aspx

« Last Edit: August 27, 2014, 08:12:00 am by SemperFi »

SemperFi

  • I post frequently
  • ***
  • Posts: 177
  • Karma: 3
  • CiviCRM version: 4.5.4
  • CMS version: Wordpress 4.0.1
  • MySQL version: MySQL 5.5.16
  • PHP version: PHP 5.3.17
Re: Extension to export to native Excel (xlsx)
August 28, 2014, 01:28:48 am
See new test results enclosed. Issue seems to be related to custom_field export, not to Excel version because I tested with 2007 and 2013 versions.

Do you handle differently custom fields when exporting, issue seems to be related to this ?
« Last Edit: August 28, 2014, 03:19:11 am by SemperFi »

mathieu

  • Administrator
  • Ask me questions
  • *****
  • Posts: 620
  • Karma: 36
    • Work
  • CiviCRM version: 4.7
  • CMS version: Drupal
  • MySQL version: MariaDB 10
  • PHP version: 7
Re: Extension to export to native Excel (xlsx)
August 28, 2014, 06:33:03 am
Seems to be a bug in the search->export code, but looking only from the code itself, it seems OK.

In short, the search-export creates a temporary mysql table and guesses mysql column types based on the type of custom field. Ex: a "Textfield" will become a varchar(x), while a "Note" will become a "longtext" column.

CRM/Export/BAO/Export.php :

Code: [Select]
        case CRM_Utils_Type::T_STRING:
          if (isset($query->_fields[$field]['maxlength'])) {
            $sqlColumns[$fieldName] = "$fieldName varchar({$query->_fields[$field]['maxlength']})";
          }
          else {
            $sqlColumns[$fieldName] = "$fieldName varchar(64)";
          }
          break;

However, I would recommend changing your custom field to a "note" field. It can be subject to debate, but a varchar will always reserve the specified amount of space for each row, while a 'text' column will store a pointer to where the data really is. This means that lookups can be slightly slower, but it will use less space on disk, especially if that field is not always used (which can also make disk access faster).

There may be simpler ways of converting a field, but I usually create a new field and use an SQL query to copy the data, ex:

Code: [Select]
update civicrm_my_custom_value_table set newfield = oldfield;

Once that is done, you can disable the old field, do a few tests, and eventually delete the old one.

If I'm really lazy, I might do a "alter table" on the field, and update the corresponding civicrm_custom_field entry, but that is much more risky.
CiviCamp Montréal, 29 septembre 2017 | Co-founder / consultant / turn-key CiviCRM hosting for Quebec/Canada @ SymbioTIC.coop

SemperFi

  • I post frequently
  • ***
  • Posts: 177
  • Karma: 3
  • CiviCRM version: 4.5.4
  • CMS version: Wordpress 4.0.1
  • MySQL version: MySQL 5.5.16
  • PHP version: PHP 5.3.17
Re: Extension to export to native Excel (xlsx)
August 28, 2014, 06:54:02 am
OK understood your point. 2 questions
  • What is the value taken by "x" in varchar(x) ?
  • Why would there be a difference between 4.4.6 and 4.5 ?

Here is an other option : in export.php, why not having both custom fields and notes being exported as "longtext" columns in the temporary table ?

Because if the value of "x" in varchar(x), depends of the value set in "civicrm_custom_field", it may still truncate part of the text contains in the civicrm_custom_field_value table, where type is Text (so not blocking data entry to the x limit).

What do you think ?
Merci Mathieu

mathieu

  • Administrator
  • Ask me questions
  • *****
  • Posts: 620
  • Karma: 36
    • Work
  • CiviCRM version: 4.7
  • CMS version: Drupal
  • MySQL version: MariaDB 10
  • PHP version: 7
Re: Extension to export to native Excel (xlsx)
August 28, 2014, 09:18:42 am
Quote from: SemperFi on August 28, 2014, 06:54:02 am
What is the value taken by "x" in varchar(x) ?

The "x" is the length that was defined for the textfield. So if you put 500, it should be a varchar(500).

Quote
Why would there be a difference between 4.4.6 and 4.5 ?

I managed to reproduce the error in 4.4.6, but not in 4.5.

Edit: Seems like it has been fixed: https://issues.civicrm.org/jira/browse/CRM-15048
« Last Edit: August 28, 2014, 10:00:06 am by mathieu »
CiviCamp Montréal, 29 septembre 2017 | Co-founder / consultant / turn-key CiviCRM hosting for Quebec/Canada @ SymbioTIC.coop

SemperFi

  • I post frequently
  • ***
  • Posts: 177
  • Karma: 3
  • CiviCRM version: 4.5.4
  • CMS version: Wordpress 4.0.1
  • MySQL version: MySQL 5.5.16
  • PHP version: PHP 5.3.17
Re: Extension to export to native Excel (xlsx)
August 28, 2014, 10:28:37 am
OK thanks for the feedback. So bug you may have found is for both version 4.4.6 and 4.5 -> should we post somewhere else in the forum to get the appropriate attention to it ?


mathieu

  • Administrator
  • Ask me questions
  • *****
  • Posts: 620
  • Karma: 36
    • Work
  • CiviCRM version: 4.7
  • CMS version: Drupal
  • MySQL version: MariaDB 10
  • PHP version: 7
Re: Extension to export to native Excel (xlsx)
August 28, 2014, 10:31:48 am
I edited my last comment: it has been fixed in 4.5 already.

It's pretty easy to backport if you don't want to wait:
https://github.com/civicrm/civicrm-core/pull/3790/files
CiviCamp Montréal, 29 septembre 2017 | Co-founder / consultant / turn-key CiviCRM hosting for Quebec/Canada @ SymbioTIC.coop

SemperFi

  • I post frequently
  • ***
  • Posts: 177
  • Karma: 3
  • CiviCRM version: 4.5.4
  • CMS version: Wordpress 4.0.1
  • MySQL version: MySQL 5.5.16
  • PHP version: PHP 5.3.17
Re: Extension to export to native Excel (xlsx)
August 28, 2014, 10:47:28 am
OK understood now ! Thanks - so everything is working well. That's good new.

How far you think you are for a "production" release version with both search and report export capabilities ?
Seems already that you're pretty close, no ?

Pages: [1] 2
  • CiviCRM Community Forums (archive) »
  • Discussion »
  • Extensions (Moderators: mathieu, totten, kasiawaka) »
  • Extension to export to native Excel (xlsx)

This forum was archived on 2017-11-26.