/inc/Excel2MySQL/excel2mysql.php

https://github.com/sangmadesu/tcake · PHP · 286 lines · 171 code · 89 blank · 26 comment · 14 complexity · f05f2cf42373ccc9cc9ebba535ba5033 MD5 · raw file

  1. <?php
  2. require_once 'reader.php';
  3. class Excel2MySQL
  4. {
  5. /**
  6. *
  7. * purpose : convert excel file to mysql data
  8. * author : anghuda
  9. * email : anghuda@yahoo.com
  10. * website : http://blog.lentera.web.id
  11. * cpyrite : anghuda
  12. * lisence : http://www.php.net/license/3_0.txt PHP License 3.0
  13. *
  14. * require : Spreadsheet_Excel_Reader by Vadim Tkachenko <vt@phpapache.com>
  15. *
  16. * version : 0.0.1 07 July 2008
  17. * ....... : 0.0.2 18 July 2008
  18. **/
  19. private $row_start;
  20. private $col_start;
  21. private $row_end;
  22. private $col_end;
  23. private $col_names;
  24. private $col_tb_names;
  25. private $col_mapping;
  26. private $row_empty_end;
  27. private $col_empty_end;
  28. private $row_head;
  29. private $db_fields;
  30. private $tbl_name;
  31. private $db_name;
  32. private $excel_file;
  33. function __construct( $file )
  34. {
  35. $this->row_start = 1;
  36. $this->col_start = 1;
  37. $this->row_empty_end = 5;
  38. $this->col_empty_end = 5;
  39. $this->row_end = 0;
  40. $this->col_end = 0;
  41. $this->row_head = false;
  42. $this->col_names = array();
  43. $this->col_tb_names = array();
  44. $this->excel_fle = $file;
  45. }
  46. public function setConvertArea( $area )
  47. {
  48. $num_area = $this->convertAreatoNumber( $area );
  49. $this->col_start = $num_area[1];
  50. $this->row_start = $num_area[2];
  51. $this->col_end = $num_area[3];
  52. $this->row_end = $num_area[4];
  53. }
  54. public function setFirstRowHead()
  55. {
  56. $this->row_head = true;
  57. }
  58. public function setRowStart( $start )
  59. {
  60. $this->row_start = $start;
  61. }
  62. public function setRowEnd( $end )
  63. {
  64. $this->row_end = $end;
  65. }
  66. public function setColStart( $str )
  67. {
  68. $this->col_start = $this->strColtoNumber( $str );
  69. }
  70. public function setColEnd( $str )
  71. {
  72. $this->col_end = $this->strColtoNumber( $str );
  73. }
  74. public function setColMapping( $mapping )
  75. {
  76. $this->col_mapping = $mapping;
  77. }
  78. /* convert excel file to array */
  79. public function parse2Array()
  80. {
  81. $sheet = new Spreadsheet_Excel_Reader();
  82. $sheet->setOutputEncoding('UTF-8');
  83. $sheet->read( $this->excel_fle );
  84. $row_end = $this->row_end;
  85. $col_end = $this->col_end;
  86. if(empty($row_end)) $row_end = $sheet->sheets[0]['numRows'];
  87. if(empty($col_end)) $col_end = $sheet->sheets[0]['numCols'];
  88. for ( $i = $this->row_start; $i <= $row_end; $i++ )
  89. {
  90. for ($j = $this->col_start; $j <= $col_end; $j++)
  91. {
  92. if ( empty( $sheet->sheets[0]['cells'][$i][$j]))
  93. {
  94. /* do nothing if cell is empty */
  95. } else {
  96. if($i==$this->row_start)
  97. {
  98. /* grab column name */
  99. $this->col_names[$j] = $sheet->sheets[0]['cells'][$i][$j];
  100. /* map column name */
  101. if(in_array( $this->col_names[$j], array_keys($this->col_mapping )))
  102. $this->col_tb_names[$j] = $this->col_mapping[ $this->col_names[$j] ];
  103. } else {
  104. /* grab cell data */
  105. $contents[$i][ $this->col_tb_names[$j] ] = $sheet->sheets[0]['cells'][$i][$j];
  106. }
  107. }
  108. }
  109. }
  110. return $contents;
  111. }
  112. public function connectDB ($host,$user,$passwd,$db_name,$db_table) {
  113. $this->db_conn = mysql_connect ($host,$user,$passwd);
  114. $this->db_name = $db_name;
  115. $this->db_table = $db_table;
  116. }
  117. function evalDB() {
  118. $tbl_name = $this->db_table;
  119. $field_names = implode(",", $this->col_tb_names);
  120. $query = "SELECT $field_names FROM $tbl_name";
  121. $result = mysql_db_query($this->db_name, $query, $this->db_conn);
  122. $count = mysql_num_fields($result);
  123. for ($j=0; $j < $count; $j++) {
  124. $type = mysql_field_type($result, $j);
  125. $name = mysql_field_name($result, $j);
  126. $this->db_fields[$name]['type'] = $type;
  127. }
  128. }
  129. public function injectData() {
  130. $arr_data = $this->parse2Array();
  131. $this->evalDB();
  132. $query = "INSERT INTO $this->db_table ( " . implode(',', $this->col_tb_names) ." ) VALUES ";
  133. $val_query = "";
  134. /* next row for data */
  135. $this->row_start++;
  136. for( $i=$this->row_start; $i < $this->row_start+count($arr_data); $i++ ){
  137. $o_query = "(";
  138. reset($this->col_tb_names);
  139. $v_query = "";
  140. while (list($key, $val) = each($this->col_tb_names)) {
  141. $type = $this->db_fields[ $val ]['type'];
  142. switch( $type ) {
  143. case 'int' :
  144. $v_query .= $arr_data[$i][$val] . ",";
  145. break;
  146. case 'date' :
  147. $v_query .= "'" . $this->xl2timestamp( $arr_data[$i][$val] )."',";
  148. break;
  149. default :
  150. $v_query .= "'".$arr_data[$i][$val]."',";
  151. }
  152. }
  153. $v_query = rtrim($v_query,',');
  154. $val_query .= $o_query . $v_query . "),";
  155. }
  156. $val_query = rtrim($val_query,',') . ";";
  157. /* inject into database */
  158. mysql_query( $query . $val_query);
  159. }
  160. /* convert column characters to numbers */
  161. function strColtoNumber( $str )
  162. {
  163. if( strlen($str) > 1)
  164. {
  165. $arr_str = str_split($str);
  166. $num_1 = ord( $arr_str[0] ) - 64 + ( (ord( $arr_str[0] ) - 64) * 25);
  167. $num_2 = ord( $arr_str[1] ) - 64;
  168. return $num_1 + $num_2;
  169. } else {
  170. $num = ord( $str ) - 64;
  171. return $num;
  172. }
  173. }
  174. /* convert input area string to associative numbers */
  175. function convertAreatoNumber( $area )
  176. {
  177. /* purify input */
  178. $area = str_replace(':','',$area);
  179. $area = ltrim($area,'$');
  180. /* parse column & row number */
  181. list($col_start, $row_start, $col_end, $row_end) = explode('$', $area);
  182. $num_area[1] = $this->strColtoNumber( $col_start );
  183. $num_area[2] = $row_start;
  184. $num_area[3] = $this->strColtoNumber( $col_end );
  185. $num_area[4] = $row_end;
  186. return $num_area;
  187. }
  188. /* convert excel date to mysql date format */
  189. function xl2timestamp( $xl_date )
  190. {
  191. $excel_timestamp = $xl_date - 25568;
  192. $php_timestamp = mktime(0,0,0,1,$excel_timestamp,1970);
  193. $mysql_timestamp = date('Y-m-d', $php_timestamp);
  194. return $mysql_timestamp;
  195. }
  196. }
  197. ?>