PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

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

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