mk-j / PHP_XLSXWriter

Lightweight XLSX Excel Spreadsheet Writer in PHP
MIT License
1.84k stars 664 forks source link

Problem with writeSheetHeader and setting column widths #184

Closed RossWHolland closed 1 year ago

RossWHolland commented 6 years ago

Firstly, let me say - Great Tool! So simple to use, and so fast!.

I'm using with PHP to generate a document rather than a listing (not the best use of Excel I know, but it's what the customer wanted), and although Autofit Columns would be great (I know you have discarded the idea, which is a shame) I am trying to set the column widths individually. It looks like this can only be done through writeSheetHeader, however when I try that I get an error in the spreadsheet. Excel tries to recover the doc, then gives me a report saying it repaired or removed the unreadable content "Repaired Records: Cell information from /xl/worksheets/sheet1.xml part". It does however format the column widths correctly :)!

I have based my code on "example.php" which has all the "header('Content-disposition:" etc., in it. which seems to work fine and gives me exactly what I need. So I am assuming that writeSheetHeader is somehow corrupting the header() information?

Any suggestions on how I could do both would be gratefully received.

Cheers

Ross

clubcodigocero commented 6 years ago

It all depends what PHP version you're using.

For PHP Versions under 5.4 you should use the following Array declaration structure to specify the column widths ($col_options) for PHP_XLSwriter:

$widths = array(10,20,30,40,50,60);
$col_options = array('widths'=>$widths);

$writer->writeSheetHeader('Sheet1', $header, $col_options);

For PHP Versions 5.4 or greater:

$writer->writeSheetHeader('Sheet1', $rowdata = array(100,200,300,400,500,600), $col_options = ['widths'=>[10,20,30,40,50,60]] );

michaelcheers commented 6 years ago

What does the $header do in this case? I just want the default XLSXWriter settings except with column widths. How can I do this?