0

I want to ask something. I have little problem with PHP.

I want to export my database to excel without phpmyadmin but with PHP.

Anyone can help me?

2 Answers2

3

There are number of ways you could do this.

  1. Use PHP to generate a CSV file which you can then open in Excel.

  2. Use a PHP Library that will do it such as http://phpexcel.codeplex.com. Obviously you will need to write the code to connect to your database, pull the records, etc.

Here is a simple "Hello World" example using PHPExcel:

<?php
/** Error reporting */
error_reporting(E_ALL);

/** Include path **/
ini_set('include_path', ini_get('include_path').';../Classes/');

/** PHPExcel */
include 'PHPExcel.php';

/** PHPExcel_Writer_Excel2007 */
include 'PHPExcel/Writer/Excel2007.php';

// Create new PHPExcel object
echo date('H:i:s') . " Create new PHPExcel object\n";
$objPHPExcel = new PHPExcel();

// Set properties
echo date('H:i:s') . " Set properties\n";
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw");
$objPHPExcel->getProperties()->setLastModifiedBy("Maarten Balliauw");
$objPHPExcel->getProperties()->setTitle("Office 2007 XLSX Test Document");
$objPHPExcel->getProperties()->setSubject("Office 2007 XLSX Test Document");
$objPHPExcel->getProperties()->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.");


// Add some data
echo date('H:i:s') . " Add some data\n";
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->SetCellValue('A1', 'Hello');
$objPHPExcel->getActiveSheet()->SetCellValue('B2', 'world!');
$objPHPExcel->getActiveSheet()->SetCellValue('C1', 'Hello');
$objPHPExcel->getActiveSheet()->SetCellValue('D2', 'world!');

// Rename sheet
echo date('H:i:s') . " Rename sheet\n";
$objPHPExcel->getActiveSheet()->setTitle('Simple');


// Save Excel 2007 file
echo date('H:i:s') . " Write to Excel2007 format\n";
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save(str_replace('.php', '.xlsx', __FILE__));

// Echo done
echo date('H:i:s') . " Done writing file.\r\n";

There are other libraries that you can use if you do a quick google search.

ajtrichards
  • 29,723
  • 13
  • 94
  • 101
  • 1
    Read my post - it says you will have to write the code to connect to the database, get the records, etc. This is just a "Hello World" example to show how simple PHPExcel is. – ajtrichards Oct 26 '12 at 10:40
  • You can find an example of how to write from a database to Excel using PHPExcel in this answer http://stackoverflow.com/questions/12611148/how-to-export-data-to-an-excel-file-using-phpexcel – Mark Baker Oct 26 '12 at 11:07
0

you have two options,

  1. Export as CSV and then import into excel
  2. Use some php library as phpexcel

both have pros and cons, in CSV to open it correctly you have to import it if you use Cyrillic letters for example, but the export is pretty straightforward. if you use some library, you might have problems with the export types, for example if ypu want to export 0456 it might come out as 456. etc etc

Nikola
  • 546
  • 1
  • 6
  • 18