PageRenderTime 34ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/export/core/modules/export/export_excel2007.modules.php

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