How to Export XLSX with Cell Formatting as Text #2551
-
Hello, i'm using Laravel 8 with the lastest version of Yajra Datatables and Buttons. How can I format the whole export sheet as Text, without manually set in Office Excel. Thank you. |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 8 replies
-
Just to confirm, you are using excel and not csv button? I mostly encounter this behavior when you export using CSV and then opening it on MS Excel. |
Beta Was this translation helpful? Give feedback.
-
I have same problem too. |
Beta Was this translation helpful? Give feedback.
-
You can use export class https://yajrabox.com/docs/laravel-datatables/master/buttons-laravel-excel and adjust as needed. |
Beta Was this translation helpful? Give feedback.
-
put this in config folder and name it excel.php `<?php use Maatwebsite\Excel\Excel; return [
]; |
Beta Was this translation helpful? Give feedback.
-
I think this is best using the export concern format column from Laravel Excel @see https://docs.laravel-excel.com/3.0/exports/column-formatting.html So In Short you should try use this <?php
namespace App\DataTables;
use Yajra\DataTables\Services\DataTablesExportHandler;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
class AccountsDataTablesExportHandler extends DataTablesExportHandler
implements WithColumnFormatting, ShouldAutoSize
{
/**
* @return array
*/
public function columnFormats(): array
{
return [
// The Assumption is this column is the number column
'C' => NumberFormat::FORMAT_TEXT,
];
}
} EDIT : 2nd, if there are intelligent value binder, then YOU NEED TO OVERWRITEit! @see https://docs.laravel-excel.com/3.1/exports/column-formatting.html#value-binders <?php
namespace App\DataTables;
use Yajra\DataTables\Services\DataTablesExportHandler;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
// For Custom Value Binder
use Maatwebsite\Excel\Concerns\WithCustomValueBinder;
use Maatwebsite\Excel\DefaultValueBinder;
use PhpOffice\PhpSpreadsheet\Cell\Cell;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
class AccountsDataTablesExportHandler extends DataTablesExportHandler
implements WithColumnFormatting, ShouldAutoSize
{
/**
* THIS IS THE REAL WAY
* To format the value from number to string, FORECFULLY
* @param Cell $cell
* @param mixed $value
* @return bool
* @throws Exception
*/
public function bindValue(Cell $cell, $value) {
// The column is expected to be Column F for...
// The Phone Number
if ($cell->getColumn() == "F") {
$cell->setValueExplicit($value, DataType::TYPE_STRING2);
return true;
}
// Duct tape, sadly, @see https://github.com/SpartnerNL/Laravel-Excel/blob/3.1/src/DefaultValueBinder.php
$defaultValueBinder = new DefaultValueBinder();
// else return default behavior
return $defaultValueBinder->bindValue($cell, $value);
}
} The put on your DataTables Class namespace App\DataTables;
use Yajra\DataTables\Services\DataTable;
class AccountsDataTable extends DataTable
{
/**
* Export class handler.
*
* @var class-string
*/
protected string $exportClass = AccountsDataTablesExportHandler::class;
} This inspired by https://laracasts.com/discuss/channels/laravel/formatted-exported-excel-file-on-laravel-excel-based-on-yajra-datatable And the answer before. I think this already documented thoo in the docs |
Beta Was this translation helpful? Give feedback.
put this in config folder and name it excel.php
`<?php
use Maatwebsite\Excel\Excel;
return [
'exports' => [