【问题标题】:PHP_XLSXWriter and Code Igniter Corrupted Output Excel FilePHP_XLSXWriter 和 Code Igniter 损坏的输出 Excel 文件
【发布时间】:2022-06-14 15:00:50
【问题描述】:

我正在尝试将 [PHP_XLSXWriter] (https://github.com/mk-j/PHP_XLSXWriter) 与 Code Igniter 集成

这是我的控制器源代码

public function ToExcel(){
    include_once APPPATH.'/third_party/xlsxwriter.class.php';
    $filename = "report-".date('d-m-Y-H-i-s').".xlsx";
    header('Content-disposition: attachment; filename="'.XLSXWriter::sanitize_filename($filename).'"');
    header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    header('Content-Transfer-Encoding: binary');
    header('Cache-Control: must-revalidate');
    header('Pragma: public'); 

    $styles = array('widths'=>[3,20,30,40], 'font'=>'Arial','font-size'=>10,'font-style'=>'bold', 'fill'=>'#eee', 'halign'=>'center', 'border'=>'left,right,top,bottom');
    $styles2 = array( ['font'=>'Arial','font-size'=>10,'font-style'=>'bold', 'fill'=>'#eee', 'halign'=>'left', 'border'=>'left,right,top,bottom','fill'=>'#ffc'],['fill'=>'#fcf'],['fill'=>'#ccf'],['fill'=>'#cff'],);

    $header = array(
    'No 1'=>'string',
    'No 2'=>'string',
    'No 3'=>'string',
    'No 4'=>'string',
    );

    $writer = new XLSXWriter();
    $writer->setAuthor('Human');
    $writer->writeSheetHeader('Sheet1', $header, $styles);
    for($no=1;$no<=10;$no++){
        $writer->writeSheetRow('Sheet1', [$no, $no, $no, $no], $styles2);
    }
    $writer->writeToStdOut();   
}

Excel 文件生成并成功下载,但是当我尝试使用 Ms Excel 打开它时,它说文件已损坏。问题是,原来生成的 Excel 文件的源有空单行

当我删除那个空行时,它可以毫无问题地打开

而且,如果我将该控制器代码复制到单个 php 文件(不涉及 Code Igniter),脚本和生成的 Excel 文件就像一个魅力

如何去掉第一个空行?

非常感谢您的帮助

【问题讨论】:

    标签: codeigniter phpxlsxwriter


    【解决方案1】:

    $writer->writeToStdOut($filename);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-06
      • 2020-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-19
      相关资源
      最近更新 更多