php - 使用 PHPSpreadsheet 导出 .xlsx,读取创建 HTML 表的 .php 文件
问题描述
是否可以创建一个.xlsx
使用 PHPSpreadsheet 从创建 HTML 表的 .php 文件中的电子表格?.php 文件从 POST 和 DB 值创建一个 HTML 表。我能够仅使用 php 标头指令成功创建一个 .xls 文件。我通过 PHPSpreadsheet 获得的最接近的方法是将 .php 文件读取为 HTML,它会创建一个 .xlsx 文件,但还会在浏览器中生成有关文件类型的 php 警告,并且 .xlsx 文件除了包含所有 php 代码之外生成的 HTML 表格(表格的静态标题在电子表格中,但不是表格的动态 php 内容)。我在 PHPSpreadsheet 文档中找不到任何关于从 .php 文件中读取的内容。我尝试在 php 文件的开头使用 text/html 标头,但这不会改变 .xlsx 文件的内容。
<?php
require_once("../urlparse.php");
require_once("$passwordfile");
require_once("dbfetchsiteoptions.php");
$fullname = $_SESSION['fullname'];
$userid = $_SESSION['userid'];
$stmt = $PDO->prepare("SELECT * FROM siteoptions WHERE optionname='timesheetround'");
$stmt->execute();
$data = $stmt->fetch(PDO::FETCH_ASSOC);
$optionname = $data['optionname'];
$roundoptions = array($data['optionvalue1'], $data['optionvalue2'], $data['optionvalue3']);
$valuecurrent = $data['optionvaluecurrent'];
$stmt = $PDO->prepare("SELECT * FROM payperiod WHERE id='1'");
$stmt->execute();
$data = $stmt->fetch(PDO::FETCH_ASSOC);
$payperiodstart = new DateTime($data['payperiodstart']);
$payperiodinterval = new DateInterval($data['payperiodinterval']);
while($payperiodstart <= $newdatetime) {
$payperiodstartdate = $payperiodstart->format('m-d-Y');
$payperiodstart->add($payperiodinterval);
$payperiodstart->sub(new DateInterval('P01D'));
$payperiodenddate = $payperiodstart->format('m-d-Y');
$payperiodstart->add(new DateInterval('P01D'));
}
if(isset($payperiodstartdate)) echo '<table><tr><th>From</th><th>'.$payperiodstartdate.'<th>To</th><th>'.$payperiodenddate.'</th></tr>';
$stmt = $PDO->prepare("SELECT * FROM timesheet WHERE userid='$userid' ORDER BY id ASC");
$stmt->execute(); ?>
<table><tr><th>Date</th><th>Time-In</th><th>Time-Out</th><th>Hours</th></tr>
<?php
while($data = $stmt->fetch(PDO::FETCH_ASSOC)) {
$date = new DateTime($data['date']);
$date = $date->format('m-d-Y');
if($date >= $payperiodstartdate AND $date <= $payperiodenddate) {
$hours[] = $data['hours'];
echo '<tr><td>'.$date.'</td><td>'.date("g:i a", strtotime($data['timein'])).'</td>';
if($data['timeout'] == NULL) {
echo '<td>Working...</td>';
}else{
echo '<td>'.date("g:i a", strtotime($data['timeout'])).'</td>';
}
echo '<td>'.$data['hours'].'</td></tr>';
}
} ?>
</table>
<?php
if(empty($hours)){$hours = '00:00';}
$counter = new times_counter($hours);
class times_counter {
private $hou = 0;
private $min = 0;
private $sec = 0;
private $totaltime = '00:00';
public function __construct($times){
if(is_array($times)){
$length = sizeof($times);
for($x=0; $x <= $length; $x++){
$split = explode(":", @$times[$x]);
if(!empty($split[0])) {$this->hou += @$split[0];}
$this->min += @$split[1];
}
$seconds = $this->sec % 60;
$minutes = $this->sec / 60;
$minutes = (integer)$minutes;
$minutes += $this->min;
$hours = $minutes / 60;
$minutes = $minutes % 60;
$hours = (integer)$hours;
$hours += $this->hou;
if($minutes < '10') {$minutes = sprintf("%02d", $minutes);}
if($hours < '10') {$hours = sprintf("%02d", $hours);}
if($hours >= 80) {$othours = $hours - 80; $hours = 80;}
if(isset($othours)) {$this->totaltime = $hours.':00<br><h3>Overtime</h3>'.$othours.':'.$minutes;}else{$this->totaltime = $hours.":".$minutes;}
}
}
public function get_total_time(){
return $this->totaltime;
}
}
?>
<table><tr><th></th><th></th><th></th><th>Total Hours</th></tr><tr><td></td><td></td>
<td></td><td><?php echo $counter->get_total_time(); ?></td></tr>
</table>
解决方案
你有两种方法可以做到这一点:
- 继续从您的 php 脚本生成的 HTML 生成您的 xlsx。
这样,您的 PHP 脚本(例如)在磁盘上生成一个 export.html 文件,然后 phpspreadsheet 代码读取该文件(使用“HTML Reader”)并生成 xslx。
使用 PHPExcel(phpspreadsheet 的先前名称),我们这样做了:
$inputFileType = 'HTML';
$inputFileName = './myHtmlFile.html';
$outputFileType = 'Excel2007';
$outputFileName = './myExcelFile.xlsx';
$objPHPExcelReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objPHPExcelReader->load($inputFileName);
$objPHPExcelWriter = PHPExcel_IOFactory::createWriter($objPHPExcel,$outputFileType);
$objPHPExcel = $objPHPExcelWriter->save($outputFileName);
- 您可以为 PHP 代码生成 CSV,而不是生成 HTML,然后将其发送到 phpspreadsheet,请参阅此处的文档https://phpspreadsheet.readthedocs.io/en/develop/topics/reading-files/
推荐阅读
- java - 我无法使用 Oracle Sql 查询在 java 中设置参数
- xslt - 与 XPath 表达式相比,非流模式下的 XSLT 3 累加器是否提供性能优势?
- python - 使用python的for循环中没有多个名义属性的数据图
- javascript - 如何在同一个请求中退出/退出 Express 中的路线?
- php - Render .glb / .gltf 3d model with PHP
- java - 在输入元素的重量和利润时陷入无限循环
- c++ - 如何将 CSV 文件作为一个大字符串存储在变量中
- ballerina - Ballerina vscode 插件警告ballerina home 设置无效。Windows 10 PC 的正确设置是什么?
- swift - 如何在 Swift 中使用 RealityKit 跟踪多个图像?
- javascript - 为什么这个上下文提供者在 react.js 中向消费者发送 null