Jed
Jed

Reputation: 1043

PhpExcel Workbook

I currently have a .xlsx file with a workbook with multiple sheets with text and images.

I need to import this data into my MySQL database.

Does anyone know of any tutorials or just some code that can help me get all the sheet within the workbook and retrieve text and images, and insert them into the database.

Upvotes: 0

Views: 516

Answers (2)

Mark Baker
Mark Baker

Reputation: 212412

$reader = new PHPExcel_Reader_Excel2007();
 $PHPExcel = $reader->load('test.xlsx');
 $worksheet = $PHPExcel->getActiveSheet();

 // extract images from worksheet and save files:  0.jpeg, 1.jpeg, 2.png, ...
 foreach ($worksheet->getDrawingCollection() as $i => $drawing) {
     $filename = $drawing->getPath();
     $imagesize = getimagesize($filename);

     switch ($imagesize[2]) {

     case 1:
         $image = imagecreatefromgif($filename);
         imagegif($image, "$i.gif");
         break;

     case 2:
         $image = imagecreatefromjpeg($filename);
         imagejpeg($image, "$i.jpeg");
         break;

     case 3:
         $image = imagecreatefrompng($filename);
         imagepng($image, "$i.png");
         break;

     default:
         continue 2;

     }
 }

Upvotes: 0

luttkens
luttkens

Reputation: 1264

Check out PHPExcel. Here's how you would loop through the sheets. I'm not sure how you can get images though.

$reader = new PHPExcel_Reader_Excel2007();
$excel = $reader->load($filename);

foreach ($excel->getWorksheetIterator() as $worksheet){
    // Get the data from current worksheet
    // and store in DB as you like
}

Upvotes: 1

Related Questions