简体   繁体   English

使用PHP将数据导出到Excel

[英]Export data to Excel with PHP

I want to ask something. 我想问一些事情。 I have little problem with PHP. 我对PHP没什么问题。

I want to export my database to excel without phpmyadmin but with PHP. 我想将我的数据库导出到不带phpmyadmin但带PHP的excel中。

Anyone can help me? 有人可以帮助我吗?

There are number of ways you could do this. 您可以通过多种方法来执行此操作。

  1. Use PHP to generate a CSV file which you can then open in Excel. 使用PHP生成CSV文件,然后可以在Excel中打开。

  2. Use a PHP Library that will do it such as http://phpexcel.codeplex.com . 使用可以做到这一点的PHP库,例如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: 这是一个使用PHPExcel的简单“ Hello World”示例:

<?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. 如果您进行快速的Google搜索,则可以使用其他库。

you have two options, 您有两种选择,

  1. Export as CSV and then import into excel 导出为CSV,然后导入到Excel中
  2. Use some php library as phpexcel 使用一些php库作为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. 两者都有其优缺点,例如要使用西里尔字母,要正确打开CSV文件,必须先将其导入,但是导出非常简单。 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 如果使用某些库,则导出类型可能会出现问题,例如,如果ypu要导出0456,则可能会导出为456。等等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM