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

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

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