PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/core/modules/export/export_excel.modules.php

https://github.com/asterix14/dolibarr
PHP | 366 lines | 229 code | 45 blank | 92 comment | 13 complexity | 213267fdc771dc9af3dc1e375c7e7baa MD5 | raw file
Possible License(s): LGPL-2.0
  1. <?php
  2. /* Copyright (C) 2006-2011 Laurent Destailleur <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file htdocs/core/modules/export/export_excel.modules.php
  19. * \ingroup export
  20. * \brief File of class to generate export file with Excel format
  21. * \author Laurent Destailleur
  22. */
  23. require_once(DOL_DOCUMENT_ROOT."/core/modules/export/modules_export.php");
  24. /**
  25. * \class ExportExcel
  26. * \brief Class to build export files with Excel format
  27. */
  28. class ExportExcel extends ModeleExports
  29. {
  30. var $id;
  31. var $label;
  32. var $extension;
  33. var $version;
  34. var $label_lib;
  35. var $version_lib;
  36. var $workbook; // Handle fichier
  37. var $worksheet; // Handle onglet
  38. var $row;
  39. var $col;
  40. var $file; // To save filename
  41. /**
  42. * Constructor
  43. *
  44. * @param DoliDB $db Database handler
  45. */
  46. function ExportExcel($db)
  47. {
  48. global $conf;
  49. $this->db = $db;
  50. $this->id='excel'; // Same value then xxx in file name export_xxx.modules.php
  51. $this->label='Excel 95'; // Label of driver
  52. $this->desc='<b>Excel</b> file format (.xls)<br>This is native Excel 95 format (BIFF5).';
  53. $this->extension='xls'; // Extension for generated file by this driver
  54. $this->picto='mime/xls'; // Picto
  55. $this->version='1.30'; // Driver version
  56. // If driver use an external library, put its name here
  57. $this->label_lib='PhpExcel';
  58. $this->version_lib='1.7.2';
  59. $this->row=0;
  60. }
  61. function getDriverId()
  62. {
  63. return $this->id;
  64. }
  65. function getDriverLabel()
  66. {
  67. return $this->label;
  68. }
  69. function getDriverDesc()
  70. {
  71. return $this->desc;
  72. }
  73. function getDriverExtension()
  74. {
  75. return $this->extension;
  76. }
  77. function getDriverVersion()
  78. {
  79. return $this->version;
  80. }
  81. function getLibLabel()
  82. {
  83. return $this->label_lib;
  84. }
  85. function getLibVersion()
  86. {
  87. return $this->version_lib;
  88. }
  89. /**
  90. * Open output file
  91. *
  92. * @param string $file File name to generate
  93. * @param Translate $outputlangs Output language object
  94. * @return int <0 if KO, >=0 if OK
  95. */
  96. function open_file($file,$outputlangs)
  97. {
  98. global $user,$conf,$langs;
  99. if (! empty($conf->global->MAIN_USE_PHP_WRITEEXCEL))
  100. {
  101. $outputlangs->charset_output='ISO-8859-1'; // Because Excel 5 format is ISO
  102. }
  103. dol_syslog("ExportExcel::open_file file=".$file);
  104. $this->file=$file;
  105. $ret=1;
  106. $outputlangs->load("exports");
  107. if (! empty($conf->global->MAIN_USE_PHP_WRITEEXCEL))
  108. {
  109. require_once(PHP_WRITEEXCEL_PATH."class.writeexcel_workbookbig.inc.php");
  110. require_once(PHP_WRITEEXCEL_PATH."class.writeexcel_worksheet.inc.php");
  111. require_once(PHP_WRITEEXCEL_PATH."functions.writeexcel_utility.inc.php");
  112. $this->workbook = new writeexcel_workbookbig($file);
  113. $this->workbook->set_tempdir($conf->export->dir_temp); // Set temporary directory
  114. $this->workbook->set_sheetname($outputlangs->trans("Sheet"));
  115. $this->worksheet = &$this->workbook->addworksheet();
  116. }
  117. else
  118. {
  119. require_once(PHPEXCEL_PATH."PHPExcel.php");
  120. require_once(PHPEXCEL_PATH."PHPExcel/Style/Alignment.php");
  121. $this->workbook = new PHPExcel();
  122. $this->workbook->getProperties()->setCreator($user->getFullName($outputlangs).' - Dolibarr '.DOL_VERSION);
  123. //$this->workbook->getProperties()->setLastModifiedBy('Dolibarr '.DOL_VERSION);
  124. $this->workbook->getProperties()->setTitle($outputlangs->trans("Export").' - '.$file);
  125. $this->workbook->getProperties()->setSubject($outputlangs->trans("Export").' - '.$file);
  126. $this->workbook->getProperties()->setDescription($outputlangs->trans("Export").' - '.$file);
  127. $this->workbook->setActiveSheetIndex(0);
  128. $this->workbook->getActiveSheet()->setTitle($outputlangs->trans("Sheet"));
  129. $this->workbook->getActiveSheet()->getDefaultRowDimension()->setRowHeight(15);
  130. }
  131. return $ret;
  132. }
  133. /**
  134. * Write header
  135. *
  136. * @param Translate $outputlangs Object lang to translate values
  137. * @return int <0 if KO, >0 if OK
  138. */
  139. function write_header($outputlangs)
  140. {
  141. //$outputlangs->charset_output='ISO-8859-1'; // Because Excel 5 format is ISO
  142. return 0;
  143. }
  144. /**
  145. * Output title line into file
  146. *
  147. * @param array $array_export_fields_label Array with list of label of fields
  148. * @param array $array_selected_sorted Array with list of field to export
  149. * @param Translate $outputlangs Object lang to translate values
  150. * @return int <0 if KO, >0 if OK
  151. */
  152. function write_title($array_export_fields_label,$array_selected_sorted,$outputlangs)
  153. {
  154. // Create a format for the column headings
  155. if (! empty($conf->global->MAIN_USE_PHP_WRITEEXCEL))
  156. {
  157. $outputlangs->charset_output='ISO-8859-1'; // Because Excel 5 format is ISO
  158. $formatheader =$this->workbook->addformat();
  159. $formatheader->set_bold();
  160. $formatheader->set_color('blue');
  161. //$formatheader->set_size(12);
  162. //$formatheader->set_font("Courier New");
  163. //$formatheader->set_align('center');
  164. }
  165. else
  166. {
  167. $this->workbook->getActiveSheet()->getStyle('1')->getFont()->setBold(true);
  168. $this->workbook->getActiveSheet()->getStyle('1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  169. }
  170. $this->col=0;
  171. foreach($array_selected_sorted as $code => $value)
  172. {
  173. $alias=$array_export_fields_label[$code];
  174. //print "dd".$alias;
  175. if (empty($alias)) dol_print_error('','Bad value for field with code='.$code.'. Try to redefine export.');
  176. if (! empty($conf->global->MAIN_USE_PHP_WRITEEXCEL))
  177. {
  178. $this->worksheet->write($this->row, $this->col, $outputlangs->transnoentities($alias), $formatheader);
  179. }
  180. else
  181. {
  182. $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($this->col, $this->row+1, $outputlangs->transnoentities($alias));
  183. }
  184. $this->col++;
  185. }
  186. $this->row++;
  187. return 0;
  188. }
  189. /**
  190. * Output record line into file
  191. *
  192. * @param array $array_selected_sorted Array with list of field to export
  193. * @param resource $objp A record from a fetch with all fields from select
  194. * @param Translate $outputlangs Object lang to translate values
  195. * @return int <0 if KO, >0 if OK
  196. */
  197. function write_record($array_selected_sorted,$objp,$outputlangs)
  198. {
  199. // Create a format for the column headings
  200. if (! empty($conf->global->MAIN_USE_PHP_WRITEEXCEL))
  201. {
  202. $outputlangs->charset_output='ISO-8859-1'; // Because Excel 5 format is ISO
  203. }
  204. // Define first row
  205. $this->col=0;
  206. foreach($array_selected_sorted as $code => $value)
  207. {
  208. $alias=str_replace(array('.','-'),'_',$code);
  209. if (empty($alias)) dol_print_error('','Bad value for field with code='.$code.'. Try to redefine export.');
  210. $newvalue=$objp->$alias;
  211. $newvalue=$this->excel_clean($newvalue);
  212. // Traduction newvalue
  213. if (preg_match('/^\((.*)\)$/i',$newvalue,$reg))
  214. {
  215. $newvalue=$outputlangs->transnoentities($reg[1]);
  216. }
  217. else
  218. {
  219. $newvalue=$outputlangs->convToOutputCharset($newvalue);
  220. }
  221. if (preg_match('/^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$/i',$newvalue))
  222. {
  223. if (! empty($conf->global->MAIN_USE_PHP_WRITEEXCEL))
  224. {
  225. $formatdate=$this->workbook->addformat();
  226. $formatdate->set_num_format('yyyy-mm-dd');
  227. //$formatdate->set_num_format(0x0f);
  228. $arrayvalue=preg_split('/[.,]/',xl_parse_date($newvalue));
  229. //print "x".$arrayvalue[0].'.'.strval($arrayvalue[1]).'<br>';
  230. $newvalue=strval($arrayvalue[0]).'.'.strval($arrayvalue[1]); // $newvalue=strval(36892.521); directly does not work because . will be convert into , later
  231. $this->worksheet->write($this->row, $this->col, $newvalue, PHPExcel_Shared_Date::PHPToExcel($formatdate));
  232. }
  233. else
  234. {
  235. $newvalue=dol_stringtotime($newvalue);
  236. $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($this->col, $this->row+1, PHPExcel_Shared_Date::PHPToExcel($newvalue));
  237. $coord=$this->workbook->getActiveSheet()->getCellByColumnAndRow($this->col, $this->row+1)->getCoordinate();
  238. $this->workbook->getActiveSheet()->getStyle($coord)->getNumberFormat()->setFormatCode('yyyy-mm-dd');
  239. }
  240. }
  241. elseif (preg_match('/^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9]$/i',$newvalue))
  242. {
  243. if (! empty($conf->global->MAIN_USE_PHP_WRITEEXCEL))
  244. {
  245. $formatdatehour=$this->workbook->addformat();
  246. $formatdatehour->set_num_format('yyyy-mm-dd hh:mm:ss');
  247. //$formatdatehour->set_num_format(0x0f);
  248. $arrayvalue=preg_split('/[.,]/',xl_parse_date($newvalue));
  249. //print "x".$arrayvalue[0].'.'.strval($arrayvalue[1]).'<br>';
  250. $newvalue=strval($arrayvalue[0]).'.'.strval($arrayvalue[1]); // $newvalue=strval(36892.521); directly does not work because . will be convert into , later
  251. $this->worksheet->write($this->row, $this->col, $newvalue, $formatdatehour);
  252. }
  253. else
  254. {
  255. $newvalue=dol_stringtotime($newvalue);
  256. $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($this->col, $this->row+1, PHPExcel_Shared_Date::PHPToExcel($newvalue));
  257. $coord=$this->workbook->getActiveSheet()->getCellByColumnAndRow($this->col, $this->row+1)->getCoordinate();
  258. $this->workbook->getActiveSheet()->getStyle($coord)->getNumberFormat()->setFormatCode('yyyy-mm-dd h:mm:ss');
  259. }
  260. }
  261. else
  262. {
  263. if (! empty($conf->global->MAIN_USE_PHP_WRITEEXCEL))
  264. {
  265. $this->worksheet->write($this->row, $this->col, $newvalue);
  266. }
  267. else
  268. {
  269. $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($this->col, $this->row+1, $newvalue);
  270. }
  271. }
  272. $this->col++;
  273. }
  274. $this->row++;
  275. return 0;
  276. }
  277. /**
  278. * Write footer
  279. *
  280. * @param Translate $outputlangs Output language object
  281. * @return int <0 if KO, >0 if OK
  282. */
  283. function write_footer($outputlangs)
  284. {
  285. return 0;
  286. }
  287. /**
  288. * Close Excel file
  289. *
  290. * @return int <0 if KO, >0 if OK
  291. */
  292. function close_file()
  293. {
  294. if (! empty($conf->global->MAIN_USE_PHP_WRITEEXCEL))
  295. {
  296. $this->workbook->close();
  297. }
  298. else
  299. {
  300. require_once(PHPEXCEL_PATH."PHPExcel/Writer/Excel5.php");
  301. $objWriter = new PHPExcel_Writer_Excel5($this->workbook);
  302. $objWriter->save($this->file);
  303. $this->workbook->disconnectWorksheets();
  304. unset($this->workbook);
  305. }
  306. return 0;
  307. }
  308. /**
  309. * Clean a cell to respect rules of Excel file cells
  310. *
  311. * @param string $newvalue String to clean
  312. * @return string Value cleaned
  313. */
  314. function excel_clean($newvalue)
  315. {
  316. // Rule Dolibarr: No HTML
  317. $newvalue=dol_string_nohtmltag($newvalue);
  318. return $newvalue;
  319. }
  320. }
  321. ?>