Jump to content

Trying to Parse an Excel Document


brent123456

Recommended Posts

Hi,

 

I am trying to parse this excel document it is opening the document and printing it fine. What I can't figure out is how to make sure that if there are less then 15 cells (like 10 fill cells then 3 empty cells then 2 full cells) in the row to stop it from adding them in the wrong column. Basically if there are only 12 rows it will insert them under the wrong table heading and just insert "null" and "null" and "null" in the last three columns of the table. I am trying to get it to skip over the columns with no value and just print "null" in them. Thanks

 

$data = array();
  
  function add_computer( $location, $tagnumber, $manufactur, $model_num, $serial_num, $description, $short_def, $oid_1, $product, $serialnum, $processor, $ghz, $memory, $size, $purchase_year ) {
  global $data;
  
  $data []= array(
  'location' => $location,
  'tagnumber' => $tagnumber,
  'manufactur' => $manufactur,
  'model_num' => $model_num,
  'serial_num' => $serial_num,
  'description' => $description,
  'short_def' => $short_def,
  'oid_1' => $oid_1,
  'product' => $product,
  'serialnum' => $serialnum, 
  'processor' => $processor, 
  'ghz' => $ghz,
  'memory' => $memory,
  'size' => $size,
  'purchase_year' => $purchase_year
  );
  }
  
  if ( $_FILES['file']['tmp_name'] )  {
  			
  			$dom = DOMDocument::load($_FILES['file']['tmp_name']);
  			$rows = $dom->getElementsByTagName( 'Row' );
  			$first_row = true;
  					foreach ($rows as $row) {
  							if ( !$first_row ) {
  							$location = "";
  							$tagnumber = "";
  							$manufactur = "";
  							$model_num = "";
  							$serial_num = "";
  							$description = "";
  							$short_def = "";
						$oid_1 = "";
  							$product = "";
  							$serialnum = ""; 
  							$processor = ""; 
  							$ghz = "";
  							$memory = "";
  							$size = "";
  							$purchase_year = "";
  
  							$index = 1;
  							$cells = $row->getElementsByTagName( 'Cell' );
  								foreach( $cells as $cell ) { 
  									$ind = $cell->getAttribute( 'Index' );
  									if ( $ind != null ) $index = $ind;
  
  									if ( $index == 1 ) $location = $cell->nodeValue;
  									if ( $index == 2 ) $tagnumber = $cell->nodeValue;
  									if ( $index == 3 ) $manufactur = $cell->nodeValue;
  									if ( $index == 4 ) $model_num = $cell->nodeValue;
  									if ( $index == 5 ) $serial_num = $cell->nodeValue;
  									if ( $index == 6 ) $description = $cell->nodeValue;
  									if ( $index == 7 ) $short_def = $cell->nodeValue;
  									if ( $index == 8 ) $oid_1 = $cell->nodeValue;
  									if ( $index == 9 ) $product = $cell->nodeValue;
  									if ( $index == 10 ) $serialnum = $cell->nodeValue;
  									if ( $index == 11 ) $processor = $cell->nodeValue;
  									if ( $index == 12 ) $ghz = $cell->nodeValue;
  									if ( $index == 13 ) $memory = $cell->nodeValue;
  									if ( $index == 14 ) $size = $cell->nodeValue;
  									if ( $index == 15 ) $purchase_year = $cell->nodeValue;

  									$index += 1;
  									}
  
  								
  								add_computer( $location, $tagnumber, $manufactur, $model_num, $serial_num, $description, $short_def, 									                                $oid_1, $product, $serialnum, $processor, $ghz, $memory, $size, $purchase_year );
  								}
  						$first_row = false;
  					}
  			}


  ?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.