How can I export a table created by PHP into an excel file? Is there any way?
如何将PHP创建的表导出到excel文件中?有什么方法吗?
I actually want to do it this way:
我想这样做:
Say I am querying the database is a php file and displaying results in a html and PHP table...
假设我正在查询数据库是一个php文件,并在html和php表中显示结果……
This table will be dynamic depending on what the user is searching for. And the users should be able to save the table as a excel file by clicking a EXPORT button...
这个表将是动态的,这取决于用户正在搜索什么。用户应该能够通过单击EXPORT按钮将该表保存为excel文件……
Is there any option in php to do that?
php中有这样的选项吗?
5 个解决方案
#1
1
PHPExcel is probably the best library to go about doing this with. If you download it, there are a ton of examples that will almost certainly show what you want to do.
PHPExcel可能是最好的库。如果您下载它,有大量的示例几乎肯定会显示您想要做什么。
A quick example would be the following:
一个简单的例子是:
$objPHPExcel = new PHPExcel();
$objWorksheet = $objPHPExcel->getActiveSheet();
$objWorksheet->fromArray(
array(
array('', 'Rainfall (mm)', 'Temperature (°F)', 'Humidity (%)'),
array('Jan', 78, 52, 61),
array('Feb', 64, 54, 62),
array('Mar', 62, 57, 63),
array('Apr', 21, 62, 59),
array('May', 11, 75, 60),
array('Jun', 1, 75, 57),
array('Jul', 1, 79, 56),
array('Aug', 1, 79, 59),
array('Sep', 10, 75, 60),
array('Oct', 40, 68, 63),
array('Nov', 69, 62, 64),
array('Dec', 89, 57, 66),
)
);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('sheet.xlsx');
Alternatively, you could export pretty easily as a CSV, which the user could then open in excel without difficulty (not technically an excel document though - there are limitations).
或者,您可以很容易地导出为CSV,用户可以轻松地在excel中打开(尽管这在技术上不是一个excel文档,但是有一些限制)。
#2
0
store the table in a varaiable
将该表存储在一个可变的容器中
and write into file
并写入文件
ex:
例:
$content = "";//in content you must have table data
$file = fopen("file location", "w");
fwrite($file ,$content);
this should help easy
这应该有助于容易
#3
0
If there is an end-user that will view the file use the answer already given using PHPExcel
如果有终端用户将使用PHPExcel提供的答案查看文件
If your doing it for data-access then use CSV.
如果是数据访问,请使用CSV。
https://php.net/manual/en/function.fputcsv.php
https://php.net/manual/en/function.fputcsv.php
$list = array (
array('aaa', 'bbb', 'ccc', 'dddd'),
array('123', '456', '789'),
array('"aaa"', '"bbb"')
);
$fp = fopen('file.csv', 'w');
foreach ($list as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
#4
0
If you do not need formulas or fancy features, it would also be easy to output a CSV file (which Excel can open). If you had an array of arrays like the one enigma demonstrated, you could just use
如果不需要公式或花哨的特性,也可以很容易地输出CSV文件(Excel可以打开)。如果你有一个像enigma演示的数组,你可以使用
$string = "";
foreach ($array as $row)
$string .= implode(',', $row) . "\n";
file_put_contents("filename.csv", $string);
#5
0
Take a look a this class. It is able to create multi-sheet excel files from HTML tables.
看看这门课。它能够从HTML表创建多表excel文件。
https://github.com/GSTVAC/HtmlExcel
https://github.com/GSTVAC/HtmlExcel
$xls = new HtmlExcel();
$xls->addSheet("Names", $names);
$xls->headers();
echo $xls->buildFile();
#1
1
PHPExcel is probably the best library to go about doing this with. If you download it, there are a ton of examples that will almost certainly show what you want to do.
PHPExcel可能是最好的库。如果您下载它,有大量的示例几乎肯定会显示您想要做什么。
A quick example would be the following:
一个简单的例子是:
$objPHPExcel = new PHPExcel();
$objWorksheet = $objPHPExcel->getActiveSheet();
$objWorksheet->fromArray(
array(
array('', 'Rainfall (mm)', 'Temperature (°F)', 'Humidity (%)'),
array('Jan', 78, 52, 61),
array('Feb', 64, 54, 62),
array('Mar', 62, 57, 63),
array('Apr', 21, 62, 59),
array('May', 11, 75, 60),
array('Jun', 1, 75, 57),
array('Jul', 1, 79, 56),
array('Aug', 1, 79, 59),
array('Sep', 10, 75, 60),
array('Oct', 40, 68, 63),
array('Nov', 69, 62, 64),
array('Dec', 89, 57, 66),
)
);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('sheet.xlsx');
Alternatively, you could export pretty easily as a CSV, which the user could then open in excel without difficulty (not technically an excel document though - there are limitations).
或者,您可以很容易地导出为CSV,用户可以轻松地在excel中打开(尽管这在技术上不是一个excel文档,但是有一些限制)。
#2
0
store the table in a varaiable
将该表存储在一个可变的容器中
and write into file
并写入文件
ex:
例:
$content = "";//in content you must have table data
$file = fopen("file location", "w");
fwrite($file ,$content);
this should help easy
这应该有助于容易
#3
0
If there is an end-user that will view the file use the answer already given using PHPExcel
如果有终端用户将使用PHPExcel提供的答案查看文件
If your doing it for data-access then use CSV.
如果是数据访问,请使用CSV。
https://php.net/manual/en/function.fputcsv.php
https://php.net/manual/en/function.fputcsv.php
$list = array (
array('aaa', 'bbb', 'ccc', 'dddd'),
array('123', '456', '789'),
array('"aaa"', '"bbb"')
);
$fp = fopen('file.csv', 'w');
foreach ($list as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
#4
0
If you do not need formulas or fancy features, it would also be easy to output a CSV file (which Excel can open). If you had an array of arrays like the one enigma demonstrated, you could just use
如果不需要公式或花哨的特性,也可以很容易地输出CSV文件(Excel可以打开)。如果你有一个像enigma演示的数组,你可以使用
$string = "";
foreach ($array as $row)
$string .= implode(',', $row) . "\n";
file_put_contents("filename.csv", $string);
#5
0
Take a look a this class. It is able to create multi-sheet excel files from HTML tables.
看看这门课。它能够从HTML表创建多表excel文件。
https://github.com/GSTVAC/HtmlExcel
https://github.com/GSTVAC/HtmlExcel
$xls = new HtmlExcel();
$xls->addSheet("Names", $names);
$xls->headers();
echo $xls->buildFile();