Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • P PHP_XLSXWriter
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 101
    • Issues 101
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 24
    • Merge requests 24
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • mk-j
  • PHP_XLSXWriter
  • Issues
  • #317
Closed
Open
Issue created Dec 30, 2021 by adiakeka@adiakeka

PHP_XLSXWriter and Code Igniter Corrupted Output Excel File

I'm trying to integrate [PHP_XLSXWriter] (https://github.com/mk-j/PHP_XLSXWriter) with Code Igniter

Here's my controller source code

`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();   

}`

The Excel file are generated and downloaded successfully, but when I try to open it using Ms Excel, it says that the file was corrupted. The problem is, it turned out that there's an empty single line at first row of the generated Excel file

When I delete that empty line, it can be opened without any problem

And also, if I copy that controller code to single php file (without Code Igniter involved), the script and generated Excel file worked like a charm

How do I get rid of that first empty line?

Many thanks for the help

Assignee
Assign to
Time tracking