简体   繁体   中英

PhpOffice\PhpSpreadsheet Cannot set default format for a column or whole spreadsheet

I am stuck trying to set the default formatting of a particular column or whole spreadsheet. I'm using PhpOffice\\PhpSpreadsheet package for creating an Excel spreadsheet. Is there any way to set format of whole sheet or whole column at once?

Below is my code snippet how I have used the library:

require_once 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
// Create new Spreadsheet object
$spreadsheet = new Spreadsheet();
// Set document properties
$spreadsheet->getProperties()->setCreator('Someone')
    ->setLastModifiedBy('Someone Else')
    ->setTitle('XLSX Test Document')
    ->setSubject('Office 2007 XLSX Test Document')
    ->setDescription('PhpOffice')
    ->setKeywords('PhpOffice')
    ->setCategory('PhpOffice');

$header = [
              "Order No",
              "ID",
              "Salutaion",
              "First Name",
              "Last Name",
              "Address"
          ];
$rows = [
            [
                "SDKJFBHIDUF",
                "23123156478978945",
                "Mr.",
                "John",
                "Doe",
                "101, Lorem Ipsum"
            ]
        ];

 $sheet = $spreadsheet->getActiveSheet();
 $sheet->setTitle('Orders');
 $sheet->fromArray($header, null, 'A1');
 $sheet->fromArray($rows, null, 'A2');
 $spreadsheet->setActiveSheetIndex(0);

 header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
 header('Content-Disposition: attachment;filename="samplemultitab-invoice-' . time() . '.xlsx"');
 header('Cache-Control: max-age=0');
 // If you're serving to IE 9, then the following may be needed
 header('Cache-Control: max-age=1');
 // If you're serving to IE over SSL, then the following may be needed
 header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
 header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
 header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
 header('Pragma: public'); // HTTP/1.0
 $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
 $writer->save('php://output');
 exit;

For now I'm modifying /PhpSpreadsheet/Worksheet/Worksheet.php file fromArray method to as below

$this->getCell($currentColumn . $startRow)->setValue($cellValue);

to

$this->getCell($currentColumn . $startRow)->setValueExplicit($cellValue,\PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);

But I need a valid approach to fix this generic option setting.

I am stuck trying to set the default formatting of a particular column or whole spreadsheet. I'm using PhpOffice\\PhpSpreadsheet package for creating an Excel spreadsheet. Is there any way to set format of whole sheet or whole column at once?

Below is my code snippet how I have used the library:

require_once 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
// Create new Spreadsheet object
$spreadsheet = new Spreadsheet();
// Set document properties
$spreadsheet->getProperties()->setCreator('Someone')
    ->setLastModifiedBy('Someone Else')
    ->setTitle('XLSX Test Document')
    ->setSubject('Office 2007 XLSX Test Document')
    ->setDescription('PhpOffice')
    ->setKeywords('PhpOffice')
    ->setCategory('PhpOffice');

$header = [
              "Order No",
              "ID",
              "Salutaion",
              "First Name",
              "Last Name",
              "Address"
          ];
$rows = [
            [
                "SDKJFBHIDUF",
                "23123156478978945",
                "Mr.",
                "John",
                "Doe",
                "101, Lorem Ipsum"
            ]
        ];

 $sheet = $spreadsheet->getActiveSheet();
 $sheet->setTitle('Orders');
 $sheet->fromArray($header, null, 'A1');
 $sheet->fromArray($rows, null, 'A2');
 $spreadsheet->setActiveSheetIndex(0);

 header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
 header('Content-Disposition: attachment;filename="samplemultitab-invoice-' . time() . '.xlsx"');
 header('Cache-Control: max-age=0');
 // If you're serving to IE 9, then the following may be needed
 header('Cache-Control: max-age=1');
 // If you're serving to IE over SSL, then the following may be needed
 header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
 header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
 header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
 header('Pragma: public'); // HTTP/1.0
 $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
 $writer->save('php://output');
 exit;

For now I'm modifying /PhpSpreadsheet/Worksheet/Worksheet.php file fromArray method to as below

$this->getCell($currentColumn . $startRow)->setValue($cellValue);

to

$this->getCell($currentColumn . $startRow)->setValueExplicit($cellValue,\PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);

But I need a valid approach to fix this generic option setting.

I am stuck trying to set the default formatting of a particular column or whole spreadsheet. I'm using PhpOffice\\PhpSpreadsheet package for creating an Excel spreadsheet. Is there any way to set format of whole sheet or whole column at once?

Below is my code snippet how I have used the library:

require_once 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
// Create new Spreadsheet object
$spreadsheet = new Spreadsheet();
// Set document properties
$spreadsheet->getProperties()->setCreator('Someone')
    ->setLastModifiedBy('Someone Else')
    ->setTitle('XLSX Test Document')
    ->setSubject('Office 2007 XLSX Test Document')
    ->setDescription('PhpOffice')
    ->setKeywords('PhpOffice')
    ->setCategory('PhpOffice');

$header = [
              "Order No",
              "ID",
              "Salutaion",
              "First Name",
              "Last Name",
              "Address"
          ];
$rows = [
            [
                "SDKJFBHIDUF",
                "23123156478978945",
                "Mr.",
                "John",
                "Doe",
                "101, Lorem Ipsum"
            ]
        ];

 $sheet = $spreadsheet->getActiveSheet();
 $sheet->setTitle('Orders');
 $sheet->fromArray($header, null, 'A1');
 $sheet->fromArray($rows, null, 'A2');
 $spreadsheet->setActiveSheetIndex(0);

 header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
 header('Content-Disposition: attachment;filename="samplemultitab-invoice-' . time() . '.xlsx"');
 header('Cache-Control: max-age=0');
 // If you're serving to IE 9, then the following may be needed
 header('Cache-Control: max-age=1');
 // If you're serving to IE over SSL, then the following may be needed
 header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
 header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
 header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
 header('Pragma: public'); // HTTP/1.0
 $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
 $writer->save('php://output');
 exit;

For now I'm modifying /PhpSpreadsheet/Worksheet/Worksheet.php file fromArray method to as below

$this->getCell($currentColumn . $startRow)->setValue($cellValue);

to

$this->getCell($currentColumn . $startRow)->setValueExplicit($cellValue,\PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);

But I need a valid approach to fix this generic option setting.

I am stuck trying to set the default formatting of a particular column or whole spreadsheet. I'm using PhpOffice\\PhpSpreadsheet package for creating an Excel spreadsheet. Is there any way to set format of whole sheet or whole column at once?

Below is my code snippet how I have used the library:

require_once 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
// Create new Spreadsheet object
$spreadsheet = new Spreadsheet();
// Set document properties
$spreadsheet->getProperties()->setCreator('Someone')
    ->setLastModifiedBy('Someone Else')
    ->setTitle('XLSX Test Document')
    ->setSubject('Office 2007 XLSX Test Document')
    ->setDescription('PhpOffice')
    ->setKeywords('PhpOffice')
    ->setCategory('PhpOffice');

$header = [
              "Order No",
              "ID",
              "Salutaion",
              "First Name",
              "Last Name",
              "Address"
          ];
$rows = [
            [
                "SDKJFBHIDUF",
                "23123156478978945",
                "Mr.",
                "John",
                "Doe",
                "101, Lorem Ipsum"
            ]
        ];

 $sheet = $spreadsheet->getActiveSheet();
 $sheet->setTitle('Orders');
 $sheet->fromArray($header, null, 'A1');
 $sheet->fromArray($rows, null, 'A2');
 $spreadsheet->setActiveSheetIndex(0);

 header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
 header('Content-Disposition: attachment;filename="samplemultitab-invoice-' . time() . '.xlsx"');
 header('Cache-Control: max-age=0');
 // If you're serving to IE 9, then the following may be needed
 header('Cache-Control: max-age=1');
 // If you're serving to IE over SSL, then the following may be needed
 header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
 header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
 header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
 header('Pragma: public'); // HTTP/1.0
 $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
 $writer->save('php://output');
 exit;

For now I'm modifying /PhpSpreadsheet/Worksheet/Worksheet.php file fromArray method to as below

$this->getCell($currentColumn . $startRow)->setValue($cellValue);

to

$this->getCell($currentColumn . $startRow)->setValueExplicit($cellValue,\PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);

But I need a valid approach to fix this generic option setting.

I am stuck trying to set the default formatting of a particular column or whole spreadsheet. I'm using PhpOffice\\PhpSpreadsheet package for creating an Excel spreadsheet. Is there any way to set format of whole sheet or whole column at once?

Below is my code snippet how I have used the library:

require_once 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
// Create new Spreadsheet object
$spreadsheet = new Spreadsheet();
// Set document properties
$spreadsheet->getProperties()->setCreator('Someone')
    ->setLastModifiedBy('Someone Else')
    ->setTitle('XLSX Test Document')
    ->setSubject('Office 2007 XLSX Test Document')
    ->setDescription('PhpOffice')
    ->setKeywords('PhpOffice')
    ->setCategory('PhpOffice');

$header = [
              "Order No",
              "ID",
              "Salutaion",
              "First Name",
              "Last Name",
              "Address"
          ];
$rows = [
            [
                "SDKJFBHIDUF",
                "23123156478978945",
                "Mr.",
                "John",
                "Doe",
                "101, Lorem Ipsum"
            ]
        ];

 $sheet = $spreadsheet->getActiveSheet();
 $sheet->setTitle('Orders');
 $sheet->fromArray($header, null, 'A1');
 $sheet->fromArray($rows, null, 'A2');
 $spreadsheet->setActiveSheetIndex(0);

 header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
 header('Content-Disposition: attachment;filename="samplemultitab-invoice-' . time() . '.xlsx"');
 header('Cache-Control: max-age=0');
 // If you're serving to IE 9, then the following may be needed
 header('Cache-Control: max-age=1');
 // If you're serving to IE over SSL, then the following may be needed
 header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
 header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
 header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
 header('Pragma: public'); // HTTP/1.0
 $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
 $writer->save('php://output');
 exit;

For now I'm modifying /PhpSpreadsheet/Worksheet/Worksheet.php file fromArray method to as below

$this->getCell($currentColumn . $startRow)->setValue($cellValue);

to

$this->getCell($currentColumn . $startRow)->setValueExplicit($cellValue,\PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);

But I need a valid approach to fix this generic option setting.

this solved my problem with single values, the position was important.

$spreadsheet = new Spreadsheet();
$spreadsheet->getDefaultStyle()
    ->getNumberFormat()
    ->setFormatCode(
        \PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT
    );
$sheet = $spreadsheet->getActiveSheet();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM