PHPExcel导出HTML表格到xlsx

问题描述 投票:0回答:3

我需要尽可能轻松地将数据库中的数据导出 HTML 表格xlsx 文件

我尝试过

PHPExcel
和一些JS插件,但是不成功

有什么新的解决办法吗?

我发现的所有东西都有近 10 年的历史了。

这就是我创建的:

我的 HTML 表单:

<form action="download.php">
    <input type="submit" value="Export" />
</form>

我的 PHPExcel 代码(文件:download.php):

<?php

require_once "../Classes/PHPExcel.php";

$objPHPExcel = new PHPExcel();
$objPHPExcel->getActiveSheet()->setCellValue('A1', 'hello world!');
$objPHPExcel->getActiveSheet()->setTitle('Chesse1');

header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="helloworld.xlsx"');
header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');

?>

当我单击按钮时,它会下载文件,但我无法打开它,因为“格式错误”

我也尝试过这个视频教程: https://www.youtube.com/watch?v=4dc3a4isHNE&t

导出Excel文件有效,但出现另一个问题。

我遇到的问题是错误的字符集。 我需要使用如下字符:ě,š,č,ř,ž,ý,á,í 等,而这些字符完全搞砸了。

php excel export phpexcel xlsx
3个回答
0
投票

更新答案:

<?php  
$conn = new mysqli('localhost', 'root', '');  
mysqli_select_db($conn, 'xlsexport');  
$setSql = "SELECT `ur_Id`,`ur_username`,`ur_password` FROM `tbl_user`";  
$setRec = mysqli_query($conn, $setSql);  
$columnHeader = '';  
$columnHeader = "Sr NO" . "\t" . "User Name" . "\t" . "Password" . "\t";  
$setData = '';  
while ($rec = mysqli_fetch_row($setRec)) {  
    $rowData = '';  
    foreach ($rec as $value) {  
        $value = '"' . $value['id'] . '"' . "\t".'"' . $value['ur_username'] . '"'."\t".'"' . $value['ur_password'] . '"';  
        $rowData .= $value;  
    }  
    $setData .= trim($rowData) . "\n";  
}  
header("Content-type: application/octet-stream");  
header("Content-Disposition: attachment; filename = User_Detail_Reoprt.xlsx");  
header("Pragma: no-cache");  
header("Expires: 0");  
echo ucwords($columnHeader) . "\n" . $setData . "\n";  
?>

0
投票

我已经更改了格式并在您的代码中添加了缓冲区清理。

<?php

require_once "../Classes/PHPExcel.php";

$objPHPExcel = new PHPExcel();

header('Content-Type: application/vnd.ms-excel; charset=utf-8');
header('Content-Disposition: attachment;filename="helloworld.xls"');

$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->getCell('A20')->setValue("TEST PHPEXCEL");
$objPHPExcel->getActiveSheet()->setTitle('Chesse1');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
ob_end_clean();
$objWriter->save('php://output');

?>

有时

Excel2007
会与
Excel5
发生冲突。这取决于 PHPExcel 中可用的库文件。我遇到了同样的问题,下载后文件打不开。于是我就改成
Excel5

已插入样本数据以检查Excelsheet中是否出现数据。您可以将其替换为数据库变量。

此外

ob_end_clean()
有助于避免缓存缓冲。

希望这有帮助。


0
投票
<?php
if (function_exists('mb_internal_encoding')) {
    $oldEncoding=mb_internal_encoding();
    mb_internal_encoding('latin1');
}

require_once 'addons/PHPExcel/PHPExcel/IOFactory.php'; //Update your Path for PHPExcel Class
$objPHPExcel = new PHPExcel();

$sheet = $objPHPExcel->getActiveSheet();
$sheet->setTitle("Template Title");

$sheet->getColumnDimension('A')->setWidth(30);

$sheet->SetCellValue('A1', 'hello world!');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');

if (function_exists('mb_internal_encoding')){
    mb_internal_encoding($oldEncoding); 
}

header('Content-Type: application/vnd.ms-excel');
header("Content-Disposition: attachment;filename=helloworld.xlsx");
header('Cache-Control: max-age=0');

$objWriter->save('php://output');
?>
© www.soinside.com 2019 - 2024. All rights reserved.