Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot set culumn width #4102

Open
pippuccio76 opened this issue Jul 19, 2024 · 2 comments
Open

Cannot set culumn width #4102

pippuccio76 opened this issue Jul 19, 2024 · 2 comments

Comments

@pippuccio76
Copy link

pippuccio76 commented Jul 19, 2024

HI , i try to set culumn width , this is my code :

` $mySpreadsheet = new Spreadsheet();

    //se presente cancello l'excel 
    if(is_file((WRITEPATH . 'output.xlsx'))){

        unlink((WRITEPATH . 'output.xlsx'));
    }


    // delete the default active sheet
    $mySpreadsheet->removeSheetByIndex(0);

    // Create "Sheet 1" tab as the first worksheet.
    // https://phpspreadsheet.readthedocs.io/en/latest/topics/worksheets/adding-a-new-worksheet
    $worksheet1 = new Worksheet($mySpreadsheet, "Foglio 1");
    $mySpreadsheet->addSheet($worksheet1, 0);
    $mySpreadsheet->setActiveSheetIndexByName("Foglio 1");
    /*
    // Create "Sheet 2" tab as the second worksheet.
    $worksheet2 = new Worksheet($mySpreadsheet, "Sheet 2");
    $mySpreadsheet->addSheet($worksheet2, 1);
    */


    
    //imposto la larghezza delle colonne
    $worksheet1->getColumnDimension('A')->setAutoSize(false));
    $worksheet1->getColumnDimension('B')->setAutoSize(false);
    $worksheet1->getColumnDimension('C')->setAutoSize(false);
    $worksheet1->getColumnDimension('D')->setAutoSize(false);
    $worksheet1->getColumnDimension('E')->setAutoSize(false);
    $worksheet1->getColumnDimension('F')->setAutoSize(false);
    $worksheet1->getColumnDimension('G')->setAutoSize(false);
    $worksheet1->getColumnDimension('H')->setAutoSize(false);
    
    $worksheet1->getColumnDimension('A')->setWidth(15);
    $worksheet1->getColumnDimension('B')->setWidth('20');
    $worksheet1->getColumnDimension('C')->setWidth('20');
    $worksheet1->getColumnDimension('D')->setWidth('12.57');
    $worksheet1->getColumnDimension('E')->setWidth('6.57');
    $worksheet1->getColumnDimension('F')->setWidth('8.71');
    $worksheet1->getColumnDimension('G')->setWidth('9');
    $worksheet1->getColumnDimension('H')->setWidth('9');


    //imposto la riga 2
    $worksheet1->getRowDimension('1')->setRowHeight(12.75);
    $worksheet1->getRowDimension('2')->setRowHeight(102);
    $worksheet1->getRowDimension('3')->setRowHeight(12.75);
    $worksheet1->getRowDimension('4')->setRowHeight(15);
    $worksheet1->getRowDimension('5')->setRowHeight(15);

    for ($i=6; $i <=15 ; $i++) {

        $worksheet1->getRowDimension($i)->setRowHeight(12.75);

    }

    $worksheet1->getRowDimension('16')->setRowHeight(17.25);
    $worksheet1->getRowDimension('17')->setRowHeight(38.25);

    for ($i = 18; $i <= 55; $i++) {

        $worksheet1->getRowDimension($i)->setRowHeight(12.75);
    }
    
    //unisco le celle 
    $worksheet1->mergeCells('A2:H2');

    $mySpreadsheet->getDefaultStyle()->getFont()->setName('Arial');
    $mySpreadsheet->getDefaultStyle()->getFont()->setSize(20);
    $mySpreadsheet->getDefaultStyle()->getFont()->setBold(true);


    $worksheet1->getStyle('A2:H60')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
    $worksheet1->getStyle('A2:H60')->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);
    $worksheet1->getStyle('A2:H60')->getAlignment()->setWrapText(true);;

    $worksheet1->setCellValue('A2', "xxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxx");

    $worksheet1->mergeCells('E3:F3');
    $worksheet1->mergeCells('G3:H3');

    //IMPOSTO I BORDI

    $styleArray = [
        'borders' => [
            'allBorders' => [
                'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK,
                'color' => ['argb' => '0a0a0a'],
            ],
        ],
    ];

    $worksheet1->getStyle('A3:H3')->applyFromArray($styleArray);

    $worksheet1->getStyle('A3:H3')->getFont()->setName('Arial');
    $worksheet1->getStyle('A3:H3')->getFont()->setSize(10);
    $worksheet1->getStyle('A3:H3')->getFont()->setBold(true);

    $worksheet1->setCellValue('A3', "CLIENTE");
    $worksheet1->setCellValue('B3', "FATT");
    $worksheet1->setCellValue('D3', "DDT");
    $worksheet1->setCellValue('E3', "DATA");
    $worksheet1->setCellValue('E3', "PREAVVISO N°");
    

    // sheet 1 contains the birthdays of famous people.
    /*
    $sheet1Data = [
        ["First Name", "Last Name", "Date of Birth"],
        ['Britney',  "Spears", "02-12-1981"],
        ['Michael',  "Jackson", "29-08-1958"],
        ['Christina',  "Aguilera", "18-12-1980"],

    ];

    $worksheet1->fromArray($sheet1Data);
        */

    // Change the widths of the columns to be appropriately large for the content in them.
    // https://stackoverflow.com/questions/62203260/php-spreadsheet-cant-find-the-function-to-auto-size-column-width
    $worksheets = [$worksheet1];



    // Save to file.
    $writer = new Xlsx($mySpreadsheet);
    $writer->save(WRITEPATH.'output.xlsx');

`

But if i see the culum width f.e. for culumn A hi have this :

image

instead of 15 .

the version is 2.0.1

@oleibman
Copy link
Collaborator

You can set the column width, and, if you look at the xml for the spreadsheet, you'll see that all the columns have the width you've specified. The problem is that Excel then manipulates the value so that you see a different value in the Excel UI.

<cols>
<col min="1" max="1" width="15" customWidth="true" style="0"/>
<col min="2" max="2" width="20" customWidth="true" style="0"/>
<col min="3" max="3" width="20" customWidth="true" style="0"/>
<col min="4" max="4" width="12.57" customWidth="true" style="0"/>
<col min="5" max="5" width="6.57" customWidth="true" style="0"/>
<col min="6" max="6" width="8.71" customWidth="true" style="0"/>
<col min="7" max="7" width="9" customWidth="true" style="0"/>
<col min="8" max="8" width="9" customWidth="true" style="0"/>
</cols>

You can see this behavior in Excel without involving PhpSpreadsheet. Open a spreadsheet, set a cell, change its column width, save the spreadsheet. The value in the UI will not match the value in the xml.

@oleibman
Copy link
Collaborator

What the "width" represents is (taken from http://web.mit.edu/~stevenj/www/ECMA-376-new-merged.pdf):

Column width measured as the number of characters of the maximum digit width of the
numbers 0, 1, 2, ..., 9 as rendered in the normal style's font. There are 4 pixels of margin
padding (two on each side), plus 1 pixel padding for the gridlines.

If you take a look at your spreadsheet, and fill in, say, cell A4, you'll see that you can fit exactly 15 zero characters in it; in B4, you can fit exactly 20 zero characters, in G4, 9 characters, etc. This matches the integer widths you specified. In cell F4, you can fit 8 zero characters, but not 9; this accords with the width of 8.71 that you specified.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants