php - 在书中发现了无法阅读的内容
问题描述
每次我制作 xlsx 文件时,我都无法毫无错误地打开它:“在书中找到了无法阅读的内容”。
代码:
function getNameFromNumber($num) {
$numeric = ($num - 1) % 26;
$letter = chr(65 + $numeric);
$num2 = intval(($num - 1) / 26);
if ($num2 > 0) {
return getNameFromNumber($num2) . $letter;
} else {
return $letter;
}
}
require_once $_SERVER['DOCUMENT_ROOT'] . '/local/vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$objPHPExcel = new Spreadsheet();
$date = date("Y-m-d H:i:s");
// Set document properties
$objPHPExcel->getProperties()
->setTitle("Выгрузка заявлений от" . $date)
->setSubject("Выгрузка заявлений от" . $date)
->setDescription("Выгрузка заявлений по фильтру от" . $date);
// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('Выгрузка заявлений по фильтру');
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
$headers = ['ID', 'Фамилия абитуриента', 'Имя абитуриента', 'Отчество абитуриента', 'EMAIL', 'Статус', 'Условния приема', 'Уровень образования', 'Дата отправки заявления'];
if($arParams['GET_DOCS'] == 'Y')
$headers[] = 'Ссылки на файлы';
$activeSheet = $objPHPExcel->setActiveSheetIndex(0);
foreach( $headers as $key => $header ){
$activeSheet->setCellValue(getNameFromNumber($key + 1) . '1' , $header);
}
foreach($arResult["ITEMS"] as $rowIndex => $arItem){
//Here i prepare $row to set values
$c = 0;
foreach($row as $columnIndex => $item ){
$c++;
$activeSheet->setCellValue(getNameFromNumber($c) . (2+$rowIndex) , $item);
}
$c = 0;
foreach($row as $columnIndex => $item ){
$c++;
$activeSheet->getColumnDimension(getNameFromNumber($c))->setAutoSize(true);
}
}
$APPLICATION->RestartBuffer();
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="' . $date . '.xlsx"');
header('Cache-Control: max-age=0');
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($objPHPExcel, 'Xlsx');
$writer->save('php://output');
我试图制作空文件 -
$objPHPExcel = new Spreadsheet();
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="123.xlsx"');
header('Cache-Control: max-age=0');
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($objPHPExcel, 'Xlsx');
$writer->save('php://output');
它没有帮助。错误不会消失
我的 MS Excel 可以正确恢复这个文件,但它不会在 OpenOffice 或其他东西中打开,所以我的客户端无法在 MAC 上打开它。我该如何解决这个问题?
解决方案
mb_internal_encoding('latin1');
这有帮助
推荐阅读
- node.js - Firebase 身份验证与云 sql 混合
- webpack - Webpack 4 Hot Reload:无效的 Host/Origin 标头
- html - 基于 Flexbox 的表格
- jwt - 谷歌登录:使用 google-auth Python 包时“找不到密钥 id xxxx 的证书”
- node.js - 一次调用多次搜索一个集合
- c# - 如何在浏览器关闭 15 分钟后使用 Core 2.2 中的 Identity 保持会话活动?
- java - 在java中创建一个返回数组的toString方法
- c# - 如何通过 VSTO 插件运行 Excel 的菜单项?
- mysql - 为什么 mySQL 查询会给出意想不到的结果?
- javascript - 如何从 HTML 调用缩小的函数?