Browse Source

export table xls as html to avoid accent characters being shown wrongly in excel 2010 refs BT#7300

César Perales 11 years ago
parent
commit
09d1d312e0
2 changed files with 30 additions and 2 deletions
  1. 2 2
      main/inc/ajax/model.ajax.php
  2. 28 0
      main/inc/lib/export.lib.inc.php

+ 2 - 2
main/inc/ajax/model.ajax.php

@@ -1069,9 +1069,9 @@ if (in_array($action, $allowed_actions)) {
                 require_once api_get_path(LIBRARY_PATH).'browser/Browser.php';
                 $browser = new Browser();
                 if ($browser->getPlatform() == Browser::PLATFORM_WINDOWS) {
-                    Export::export_table_xls($array, $file_name, 'ISO-8859-15');
+                    Export::export_table_xls_html($array, $file_name, 'ISO-8859-15');
                 } else {
-                    Export::export_table_xls($array, $file_name);
+                    Export::export_table_xls_html($array, $file_name);
                 }
                 break;
             case 'csv':

+ 28 - 0
main/inc/lib/export.lib.inc.php

@@ -94,6 +94,34 @@ class Export
         DocumentManager::file_send_for_download($file, false, $filename.'.xls');
 	}
 
+    /**
+     * Export tabular data to XLS-file (as html table)
+     * @param array $data
+     * @param string $filename
+     */
+    public static function export_table_xls_html($data, $filename = 'export', $encoding = 'utf-8')
+    {
+        $file = api_get_path(SYS_ARCHIVE_PATH).uniqid('').'.xls';
+        $handle = fopen($file, 'a+');
+        $systemEncoding = api_get_system_encoding();
+        fwrite($handle, '<!DOCTYPE html><html><meta http-equiv="Content-Type" content="text/html" charset="utf-8" /><body><table>');
+        foreach ($data as $id => $row) {
+            foreach ($row as $id2 => $row2) {
+                $data[$id][$id2] = api_htmlentities($row2);
+            }
+        }
+        foreach ($data as $row) {
+            $string = implode("</td><td>", $row);
+            $string = '<tr><td>' . $string . '</td></tr>';
+            if ($encoding != 'utf-8') {
+                $string = api_convert_encoding($string, $encoding, $systemEncoding);
+            }
+            fwrite($handle, $string."\n");
+        }
+        fwrite($handle, '</table></body></html>');
+        fclose($handle);
+        DocumentManager::file_send_for_download($file, false, $filename.'.xls');
+    }
     /**
     * Export tabular data to XML-file
     * @param array  Simple array of data to put in XML