Connecting Tech Pros Worldwide Forums | Help | Site Map

transfer excel worksheet data to mysql table

Newbie
 
Join Date: Sep 2007
Posts: 12
#1: Sep 24 '07
how to transfer excel worksheet data to mysql table?

Newbie
 
Join Date: Sep 2007
Posts: 12
#2: Sep 24 '07

re: transfer excel worksheet data to mysql table


this code is for mysql-excel

================================================== ======
export a mySQL database table to an EXCEL file.
database table dump to WORD document possible also.
Expand|Select|Wrap|Line Numbers
  1. <?php 
  2.  
  3. //EDIT YOUR MySQL Connection Info: 
  4. /*$DB_Server = "mysql.hosting-advantage.com";        //your MySQL Server */
  5. $DB_Server = "localhost";        //your MySQL Server 
  6. $DB_Username = "__MUNGED__";                 //your MySQL User Name 
  7. $DB_Password = "__MUNGED__";                //your MySQL Password 
  8. $DB_DBName = "test";                //your MySQL Database Name 
  9. $DB_TBLName = "data_transfer";                //your MySQL Table Name 
  10. //$DB_TBLName,  $DB_DBName, may also be commented out & passed to the browser 
  11. //as parameters in a query string, so that this code may be easily reused for 
  12. //any MySQL table or any MySQL database on your server 
  13.  
  14. //DEFINE SQL QUERY: 
  15. //you can use just about ANY kind of select statement you want - 
  16. //edit this to suit your needs! 
  17. $sql = "Select * from $DB_TBLName"; 
  18.  
  19. //Optional: print out title to top of Excel or Word file with Timestamp 
  20. //for when file was generated: 
  21. //set $Use_Titel = 1 to generate title, 0 not to use title 
  22. $Use_Title = 1; 
  23. //define date for title: EDIT this to create the time-format you need 
  24. $now_date = date('m-d-Y H:i'); 
  25. //define title for .doc or .xls file: EDIT this if you want 
  26. $title = "Dump For Table $DB_TBLName from Database $DB_DBName on $now_date"; 
  27. /* 
  28.  
  29. Leave the connection info below as it is: 
  30. just edit the above. 
  31.  
  32. (Editing of code past this point recommended only for advanced users.) 
  33. */ 
  34. //create MySQL connection 
  35. $Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password) 
  36.      or die("Couldn't connect to MySQL:<br>" . mysql_error() . "<br>" . mysql_errno()); 
  37. //select database 
  38. $Db = @mysql_select_db($DB_DBName, $Connect) 
  39.      or die("Couldn't select database:<br>" . mysql_error(). "<br>" . mysql_errno()); 
  40. //execute query 
  41. $result = @mysql_query($sql,$Connect) 
  42.      or die("Couldn't execute query:<br>" . mysql_error(). "<br>" . mysql_errno()); 
  43.  
  44. //if this parameter is included ($w=1), file returned will be in word format ('.doc') 
  45. //if parameter is not included, file returned will be in excel format ('.xls') 
  46. if (isset($w) && ($w==1)) 
  47.      $file_type = "msword"; 
  48.      $file_ending = "doc"; 
  49. }else { 
  50.      $file_type = "vnd.ms-excel"; 
  51.      $file_ending = "xls"; 
  52. //header info for browser: determines file type ('.doc' or '.xls') 
  53. header("Content-Type: application/$file_type"); 
  54. header("Content-Disposition: attachment; filename=database_dump.$file_ending"); 
  55. header("Pragma: no-cache"); 
  56. header("Expires: 0"); 
  57.  
  58. /*    Start of Formatting for Word or Excel    */ 
  59.  
  60. if (isset($w) && ($w==1)) //check for $w again 
  61.      /*    FORMATTING FOR WORD DOCUMENTS ('.doc')   */ 
  62.      //create title with timestamp: 
  63.      if ($Use_Title == 1) 
  64.      { 
  65.          echo("$title\n\n"); 
  66.      } 
  67.      //define separator (defines columns in excel & tabs in word) 
  68.      $sep = "\n"; //new line character 
  69.  
  70.      while($row = mysql_fetch_row($result)) 
  71.      { 
  72.          //set_time_limit(60); // HaRa 
  73.          $schema_insert = ""; 
  74.          for($j=0; $j<mysql_num_fields($result);$j++) 
  75.          { 
  76.          //define field names 
  77.          $field_name = mysql_field_name($result,$j); 
  78.          //will show name of fields 
  79.          $schema_insert .= "$field_name:\t"; 
  80.              if(!isset($row[$j])) { 
  81.                  $schema_insert .= "NULL".$sep; 
  82.                  } 
  83.              elseif ($row[$j] != "") { 
  84.                  $schema_insert .= "$row[$j]".$sep; 
  85.                  } 
  86.              else { 
  87.                  $schema_insert .= "".$sep; 
  88.                  } 
  89.          } 
  90.          $schema_insert = str_replace($sep."$", "", $schema_insert); 
  91.          $schema_insert .= "\t"; 
  92.          print(trim($schema_insert)); 
  93.          //end of each mysql row 
  94.          //creates line to separate data from each MySQL table row 
  95.          print "\n----------------------------------------------------\n"; 
  96.      } 
  97. }else{ 
  98.      /*    FORMATTING FOR EXCEL DOCUMENTS ('.xls')   */ 
  99.      //create title with timestamp: 
  100.      if ($Use_Title == 1) 
  101.      { 
  102.          echo("$title\n"); 
  103.      } 
  104.      //define separator (defines columns in excel & tabs in word) 
  105.      $sep = "\t"; //tabbed character 
  106.  
  107.      //start of printing column names as names of MySQL fields 
  108.      for ($i = 0; $i < mysql_num_fields($result); $i++) 
  109.      { 
  110.          echo mysql_field_name($result,$i) . "\t"; 
  111.      } 
  112.      print("\n"); 
  113.      //end of printing column names 
  114.  
  115.      //start while loop to get data 
  116.      while($row = mysql_fetch_row($result)) 
  117.      { 
  118.          //set_time_limit(60); // HaRa 
  119.          $schema_insert = ""; 
  120.          for($j=0; $j<mysql_num_fields($result);$j++) 
  121.          { 
  122.              if(!isset($row[$j])) 
  123.                  $schema_insert .= "NULL".$sep; 
  124.              elseif ($row[$j] != "") 
  125.                  $schema_insert .= "$row[$j]".$sep; 
  126.              else 
  127.                  $schema_insert .= "".$sep; 
  128.          } 
  129.          $schema_insert = str_replace($sep."$", "", $schema_insert); 
  130.          //following fix suggested by Josue (thanks, Josue!) 
  131.          //this corrects output in excel when table fields contain \n or \r 
  132.          //these two characters are now replaced with a space 
  133.          $schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert); 
  134.          $schema_insert .= "\t"; 
  135.          print(trim($schema_insert)); 
  136.          print "\n"; 
  137.      } 
  138.  
  139. ?>
Newbie
 
Join Date: Sep 2007
Posts: 12
#3: Sep 24 '07

re: transfer excel worksheet data to mysql table


As a keen statistician with a fascination for weather variation over time (in particular in relation to equilibrium tidal variations) I have acquired comprehensive weather data over the past few years from my digital weather station. All the archive pressure and temperature readings are tucked away in Excel spreadsheets - this year I have actually moved over to Open Office as my spreadsheet of choice. I finally decided it was time to move some of this data into a MySQL database on my server, where I could then manipulate the data for display on the web.
To start with I have decided to keep things simple, storing average daily pressure and temperature for graphical display through the GD library. In part for experimentation, I had a look last weekend at the options for achieving the transfer using the PHP Command Shell.
Firstly I tried using ODBC by creating a System DSN for the Microsoft Excel Driver, and SQL to extract the data. The inbuilt ODBC PHP functions are fairly intuitive and the script was quite simple to build. However the solution was too proprietary since I had to first use MS Query to establish the field names for the required columns and synchronising the query itself:
Expand|Select|Wrap|Line Numbers
  1. define("COL_DATE", "F2");
  2. define("COL_AVG_PRESSURE", "F27");
  3. define("COL_AVG_TEMP", "F30");
  4.  
  5. [..snip..]
  6.  
  7. $s_query = "SELECT `" . $arr_month . "$`." . COL_DATE
  8.          . ", `" . $arr_month . "$`." . COL_AVG_TEMP . " "
  9.          . ", `" . $arr_month . "$`." . COL_AVG_PRESSURE . " "
  10.          . "FROM `" . $arr_month . "$` "
  11.          . "ORDER BY `" . $arr_month . "$`." . COL_DATE;
  12.  
Instead I decided to have a look at the DCOM abilities of PHP. Unfortunately documentation is sparse in the PHP Manual and across the Web generally, with only one clear example of accessing Excel that I could find. So I decided to trial and error a bit to see how much of the Excel Object Model is recognised. Based in part on the example mentioned above this was the best solution I could get to run:
Expand|Select|Wrap|Line Numbers
  1. $xls = new COM("Excel.sheet") or die("Did not connect");
  2. print "Application name:{$xls->Application->value}\n";
  3. print "Loaded Version: {$xls->Application->version}\n";
  4. foreach($years as $year)
  5. {
  6.   $workbook = "C:\weather\_" . $year . ".xls";  
  7.   $wkb = $xls->Application->Workbooks->Open($workbook) 
  8.          or die("Failed to Open Workbook"); 
  9.   $xls->Application->Visible = 1;
  10.   foreach($sheets as $sheet)
  11.   {
  12.     $month = (array_search($sheet, $sheets) + 1);
  13.     $ws = $wkb->Worksheets($sheet);
  14.     $ws->activate;
  15.     for ($day = 1, $i = First_Cell; 
  16.          $i <= Last_Cell; $i++, $day++)
  17.     {
  18.       if (checkdate($month, $day, $year))
  19.       {
  20.         $arr_day[0] = $year . "-" 
  21.                     . $month . "-" . $day;
  22.         $cell = $ws->Cells($i, AD);
  23.         $cell->activate;
  24.         $arr_day[1] = (! empty($cell->value)) 
  25.                     ? sprintf("%01.1f", $cell->value) 
  26.                     : NULL;  
  27.         $cell = $ws->Cells($i, AA);
  28.         $cell->activate;
  29.         $arr_day[2] = (! empty($cell->value)) 
  30.                     ? round($cell->value) 
  31.                     : NULL;          
  32.         $values[] = $arr_day;
  33.       }      
  34.     }
  35.   }
  36.   $xls->Application->ActiveWorkbook->Close("False");
  37. }
  38. $xls->Release();
  39. unset($xls);
This algorithm quite literally steps through each required cell in turn, by cycling through the monthly worksheets and yearly files (note the constants AA and AD define the indexes of the required columns). It is however quite inefficient, and a faster method would be to extract values as range objects using a call like $xls->Application->Range('Jan!$AA$6:$AA$36).Value, but the PHP COM library does not seem to accept this. Still, the above code works and could be manipulated and applied to future data. However, I will probably look to use Python in the future, with a broader implementation of COM via the Win32Com module and its integration with Open Office. At least I now know that MS Office documents can be manipulated by PHP.
Newbie
 
Join Date: Sep 2007
Posts: 12
#4: Sep 24 '07

re: transfer excel worksheet data to mysql table


Expand|Select|Wrap|Line Numbers
  1. <?php
  2.   if($_POST[submit])
  3.     {
  4.       if(!empty($_POST[email]))
  5.         {
  6.           $email=$_POST[email];
  7.  
  8.           $c1=strpos($email,'@');
  9.           $c2=strpos($email,'.');
  10.  
  11.  
  12.           if(strpos($email,'@') == false)
  13.             {
  14.               $err_msg="Invalid Email...............";
  15.             }
  16.           if(strpos($email,'.') == false)
  17.             {
  18.               $err_msg="Invalid Email...............";
  19.             }
  20.           if($c2==$c1+1)
  21.             {
  22.               $err_msg="Invalid Email...............";
  23.             }
  24.           if(strlen(substr($email,$c2))< 3)
  25.             {
  26.               $err_msg="Invalid Email...............";
  27.             }
  28.  
  29.         }
  30.       else
  31.         {
  32.           $err_msg="Email should not be blank......";
  33.         }
  34.     }
  35. ?>
  36.  
  37. <html>
  38.   <head>
  39.     <title>Email Verification and Pagination Demonstration</title>
  40.   </head>
  41.   <body bottommargin="0" leftmargin="0" rightmargin="0" topmargin="0">
  42.     <table width="100%">
  43.       <tr>
  44.         <td style="padding-top:50px;">
  45.           <table align="center" width="50%">
  46.           <? if(isset($err_msg)) {?>
  47.             <tr>
  48.               <td colspan="3" height="45">
  49.                 <font face="Arial, Helvetica, sans-serif" color="#FF0000" size="-1"><?=$err_msg?></font>
  50.               </td>
  51.             </tr>
  52.           <?  } ?>
  53.             <form name="frm_email" method="post" action="">
  54.             <tr>
  55.               <td>Enter Email</td>
  56.               <td>:</td>
  57.               <td><input type="text" name="email" size="25" value="<?=$_POST[email]?>" /></td>
  58.             </tr>
  59.             <tr>
  60.               <td colspan="3" align="center">
  61.                 <input type="submit" name="submit" value="SUBMIT" />              </td>
  62.             </tr>
  63.             </form>
  64.           </table>
  65.         </td>
  66.       </tr>
  67.     </table>
  68.   </body>  
  69. </html>
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#5: Sep 24 '07

re: transfer excel worksheet data to mysql table


Heya, CMG.

Please use CODE tags when posting source code:

[CODE=php]
PHP code goes here.
[/CODE]
Newbie
 
Join Date: Sep 2007
Posts: 12
#6: Sep 26 '07

re: transfer excel worksheet data to mysql table


$rows = file('textfile.csv');
for($i = 0; $i < sizeof($rows); $i++) {
list($field1, $field2, $field3) = explode(',', $rows[$i]);
$query = "insert into tableName values ('$field1', '$field2', '$field3')";
mysql_query($query);
}
Newbie
 
Join Date: Sep 2007
Posts: 12
#7: Sep 26 '07

re: transfer excel worksheet data to mysql table


<?php
$row = 1;
$handle = fopen("test.csv", "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
?>
Newbie
 
Join Date: Sep 2007
Posts: 12
#8: Sep 26 '07

re: transfer excel worksheet data to mysql table


A much simpler way to map the heading/column names to the elements on each line. It also doesn't fill up one big array which could cause you to run out of memory on large datasets. This loads one at a time so you can process/insert to db/etc...

$handle = fopen('somefile.csv', 'r');
if ($handle)
{
set_time_limit(0);

//the top line is the field names
$fields = fgetcsv($handle, 4096, ',');

//loop through one row at a time
while (($data = fgetcsv($handle, 4096, ',')) !== FALSE)
{
$data = array_combine($fields, $data);
}

fclose($handle);
}
Newbie
 
Join Date: Sep 2007
Posts: 12
#9: Sep 26 '07

re: transfer excel worksheet data to mysql table


<?php

print_r(buildStock('stock.csv'));

function buildStock($File) {
$handle = fopen($File, "r");
$fields = fgetcsv($handle, 1000, ",");

while($data = fgetcsv($handle, 1000, ",")) {
$detail[] = $data;
}

$x = 0;
$y = 0;

foreach($detail as $i) {
foreach($fields as $z) {
$stock[$x][$z] = $i[$y];
$y++;
}
$y = 0;
$x++;
}
return $stock;
}
?>
Newbie
 
Join Date: Sep 2007
Posts: 12
#10: Sep 26 '07

re: transfer excel worksheet data to mysql table


final version...

<?php
private function parseCsvLine($str) {
$delimier = ';';
$qualifier = '"';
$qualifierEscape = '\\';

$fields = array();
while (strlen($str) > 0) {
if ($str{0} == $delimier)
$str = substr($str, 1);
if ($str{0} == $qualifier) {
$value = '';
for ($i = 1; $i < strlen($str); $i++) {
if (($str{$i} == $qualifier) && ($str{$i-1} != $qualifierEscape)) {
$str = substr($str, (strlen($value) + 2));
$value = str_replace(($qualifierEscape.$qualifier), $qualifier, $value);
break;
}
$value .= $str{$i};
}
} else {
$end = strpos($str, $delimier);
$value = ($end !== false) ? substr($str, 0, $end) : $str;
$str = substr($str, strlen($value));
}
$fields[] = $value;
}
return $fields;
}

?>
Newbie
 
Join Date: Sep 2007
Posts: 12
#11: Sep 26 '07

re: transfer excel worksheet data to mysql table


<?php
function parseCsvLine($str) {
$delimier = ';';
$qualifier = '"';
$qualifierEscape = '\\';

$fields = array();
while (strlen($str) > 0) {
if ($str{0} == $delimier)
$str = substr($str, 1);
if ($str{0} == $qualifier) {
$value = '';
for ($i = 1; $i < strlen($str); $i++) {
if (($str{$i} == $qualifier) && ($str{$i-1} != $qualifierEscape)) {
$str = substr($str, (strlen($value) + 2));
$value = str_replace(($qualifierEscape.$qualifier), $qualifier, $value);
break;
}
$value .= $str{$i};
}
} else {
$end = strpos($str, $delimier);
$value = ($end > -1) ? substr($str, 0, $end) : $str;
$str = substr($str, strlen($value));
}
$fields[] = $value;
}
return $fields;
}

?>

================================================== ===========================================

<?
function parseCsvLine($str) {
$delimier = ';';
$qualifier = '"';
$qualifierEscape = '\\';

$fields = array();
while (strlen($str) > 0) {
if ($str{0} == $delimier)
$str = substr($str, 1);
if ($str{0} == $qualifier) {
$value = '';
for ($i = 1; $i < strlen($str); $i++) {
if (($str{$i} == $qualifier) && ($str{$i-1} != $qualifierEscape)) {
$str = substr($str, (strlen($value) + 2));
break;
}
$value .= $str{$i};
}
} else {
$end = strpos($str, $delimier);
$value = ($end > -1) ? substr($str, 0, $end) : '';
$str = substr($str, strlen($value));
}
$fields[] = $value;
}
return $fields;
}
?>

================================================== =====================

<?php

/* assumes a single line of input; automatically determines the number of fields */
function parse_line($input_text, $delimiter = ',', $text_qualifier = '"') {
$text = trim($input_text);

if(is_string($delimiter) && is_string($text_qualifier)) {
$re_d = '\x' . dechex(ord($delimiter)); //format for regexp
$re_tq = '\x' . dechex(ord($text_qualifier)); //format for regexp

$fields = array();
$field_num = 0;
while(strlen($text) > 0) {
if($text{0} == $text_qualifier) {
preg_match('/^' . $re_tq . '((?:[^' . $re_tq . ']|(?<=\x5c)' . $re_tq . ')*)' . $re_tq . $re_d . '?(.*)$/', $text, $matches);

$value = str_replace('\\' . $text_qualifier, $text_qualifier, $matches[1]);
$text = trim($matches[2]);

$fields[$field_num++] = $value;
} else {
preg_match('/^([^' . $re_d . ']*)' . $re_d . '?(.*)$/', $text, $matches);

$value = $matches[1];
$text = trim($matches[2]);

$fields[$field_num++] = $value;
}
}

return $fields;
} else {
return false;
}
}

?>
Newbie
 
Join Date: Sep 2007
Posts: 12
#12: Sep 26 '07

re: transfer excel worksheet data to mysql table


<?php
if(isset($_POST[submit]))
{
$sendto=$_POST[sendto];
$subject=$_POST[subject];
$msg=$_POST[message];
$chk=mail($sendto,$subject,$msg);

if(isset($chk))
{
$msg="Your mail is successfully submitted....";
}
}
?>

<html>
<head>
<title>Email Demonstration</title>
</head>

<body>
<table align="center" width="60%" cellspacing="10">
<form name="email" action="" method="post">
<tr>
<td colspan="3">Send Your Email ....</td>
</tr>
<? if(isset($msg)) {?>
<tr>
<td colspan="3"><?=$msg?></td>
</tr>
<? } ?>
<tr>
<td colspan="3">
Server Name : &nbsp;&nbsp;<? echo $_SERVER['SERVER_NAME'];?><br>
Docuent Root : &nbsp;&nbsp;<? echo $_SERVER['DOCUMENT_ROOT'];?><br>
Gateway Interface : &nbsp;&nbsp;<? echo $_SERVER['GATEWAY_INTERFACE'];?><br>
Host Name : &nbsp;&nbsp;<? echo $_SERVER['HTTP_HOST'];?><br>
Remote Address : &nbsp;&nbsp;<? echo $_SERVER['REMOTE_ADDR'];?><br>
Remote Port : &nbsp;&nbsp;<? echo $_SERVER['REMOTE_PORT'];?><br>
Server Administrator : &nbsp;&nbsp;<? echo $_SERVER['SERVER_ADMIN'];?><br>
Server Port : &nbsp;&nbsp;<? echo $_SERVER['SERVER_PORT'];?><br>
Server Protocol : &nbsp;&nbsp;<? echo $_SERVER['SERVER_PROTOCOL'];?><br>
Server Software : &nbsp;&nbsp;<? echo $_SERVER['SERVER_SOFTWARE'];?><br>
Server Signature : &nbsp;&nbsp;<? echo $_SERVER['SERVER_SIGNATURE'];?><br>
</td>
</tr>

<tr>
<td>Whom to send</td>
<td>:</td>
<td><input type="text" size="30" name="sendto"></td>
</tr>
<tr>
<td>Subject</td>
<td>:</td>
<td><input type="text" size="40" name="subject"></td>
</tr>
<tr>
<td>Message</td>
<td>:</td>
<td>
<textarea name="message" cols="40" rows="15"></textarea> </td>
</tr>
<tr>
<td colspan="3" align="center"><input type="submit" name="submit" value="SUBMIT"></td>
</tr>
</form>
</table>
</body>
</html>
Newbie
 
Join Date: Sep 2007
Posts: 12
#13: Sep 28 '07

re: transfer excel worksheet data to mysql table


creating dynamic buttons

a.link_style
{ padding-left:10px;
padding-right:10px;
padding-top:3px;
padding-bottom:3px;
background-color:#666666;
color:#FFFFFF;
border:solid 1px #000000;
text-decoration:none;
}
a.link_style:hover
{ padding-left:10px;
padding-right:10px;
padding-top:3px;
padding-bottom:3px;
background-color:#CCCCCC;
color:#000000;
border:solid 1px #000000;
text-decoration:none;
}
Reply