aVadim483 / fast-excel-writer

Lightweight and very fast XLSX Excel Spreadsheet Writer in PHP
MIT License
142 stars 26 forks source link

Excel unreadable contact #28

Closed SWAT10101 closed 10 months ago

SWAT10101 commented 10 months ago

Hello,

I was using version 2.3 after updating to 4.2 when generating the Excel file and opening it using Microsoft Excel I got the below alert but nothing changes in style and contact, But when open the file using LibreOffice I did not face an alert Microsoft Excel Version 2307 Build 16.0.16626.20110 image

Do I do something wrong below is part of the code for one sheet

                $rowStyle = ['text-align' => 'center', 'border' => 'thin'];
                $excel = Excel::create(['DW', 'In Deal', 'Out OutBy', 'Country', 'Symbols']);
                // DW Deals Sheet ---------------------------------------------------------
                $sheet = $excel->getSheet('DW');
                $sheet->setColWidth(['A', 'B', 'C', 'D', 'G', 'H', 'I'], 'auto');
                $sheet->setFreeze(1);
                $sheet->writeHeader(['Login', 'Amount', 'Time', 'Deal'], $headerStyle);
                $sheet->setColFormat('A', '@integer'); //Login
                $sheet->setColFormat('B', '[BLUE][$$]#,##0.00;[RED]-[$$]#,##0.00'); //Amount
                $sheet->setColFormat('C', 'YYYY-MM-DD HH:MM:SS'); //Time
                $sheet->setColFormat('D', '@integer'); //Deal

              (some query logic)

                $areaTotal = $sheet->makeArea('G4:I7');
                $areaData = $sheet->beginArea('A2');
                foreach ($query->get() as $key => $row) {

                    $numRow = $key + 2;
                    $areaData->setValue('A' . $numRow, $row->Login, $rowStyle);
                    $areaData->setValue('B' . $numRow, $row->Profit, $rowStyle);
                    $areaData->setValue('C' . $numRow, $row->Time, $rowStyle);
                    $areaData->setValue('D' . $numRow, $row->Deal, $rowStyle);

                }
                //Prepare total section 
                $TotalDeposit = $query->get()->where('Profit', '>', 0)->sum('Profit');
                $TotalWithdrawal = $query->get()->where('Profit', '<', 0)->sum('Profit');
                $countDeposit = $query->get()->where('Profit', '>', 0)->count();
                $countWithdrawal = $query->get()->where('Profit', '<', 0)->count();

                $areaTotal->setValue('I4', 'Total Rows', $rowStyle)->applyFontStyleBold();
                $areaTotal->setValue('G5', 'Total Deposit', $rowStyle)->applyFontColor('#3A00FF')->applyFontStyleBold();
                $areaTotal->setValue('G6', 'Total Withdrawal', $rowStyle)->applyFontColor('#FF0000')->applyFontStyleBold();
                $areaTotal->setValue('G7', 'Net', $rowStyle)->applyFontStyleBold();

                $TotalStyle = ['format' => '[BLUE][$$]#,##0.00;[RED]-[$$]#,##0.00', 'text-align' => 'center', 'border' => 'thin', 'font' => ['style' => 'bold']];
                $areaTotal->setValue('H5', $TotalDeposit, $TotalStyle);
                $areaTotal->setValue('H6', $TotalWithdrawal, $TotalStyle);
                $areaTotal->setValue('H7', $TotalDeposit + $TotalWithdrawal, ['format' => '[$$]#,##0.00', 'text-align' => 'center', 'border' => 'thin', 'font' => ['style' => 'bold']]);
                $areaTotal->setValue('I5', $countDeposit, $rowStyle)->applyFontStyleBold();
                $areaTotal->setValue('I6', $countWithdrawal, $rowStyle)->applyFontStyleBold();
                $areaTotal->setValue('I7', $countDeposit + $countWithdrawal, ['format' => '#,##0', 'text-align' => 'center', 'border' => 'thin', 'font' => ['style' => 'bold']]);
//save file
aVadim483 commented 10 months ago

This is a simplified code according to your sample, which works for me without problems. Check if Excel will issue an alert with it?

$headerStyle = [];
$rowStyle = ['text-align' => 'center', 'border' => 'thin'];
$excel = \avadim\FastExcelWriter\Excel::create(['DW', 'In Deal', 'Out OutBy', 'Country', 'Symbols']);
// DW Deals Sheet ---------------------------------------------------------
$sheet = $excel->getSheet('DW');
$sheet->setColWidth(['A', 'B', 'C', 'D', 'G', 'H', 'I'], 'auto');
$sheet->setFreeze(1);
$sheet->writeHeader(['Login', 'Amount', 'Time', 'Deal'], $headerStyle);
$sheet->setColFormat('A', '@integer'); //Login
$sheet->setColFormat('B', '[BLUE][$$]#,##0.00;[RED]-[$$]#,##0.00'); //Amount
$sheet->setColFormat('C', 'YYYY-MM-DD HH:MM:SS'); //Time
$sheet->setColFormat('D', '@integer'); //Deal

//(some query logic)
$areaTotal = $sheet->makeArea('G4:I7');
$areaData = $sheet->beginArea('A2');

$areaData->setValue('A2', 'Login', $rowStyle);
$areaData->setValue('B2', 'Profit', $rowStyle);
$areaData->setValue('C2', 'Time', $rowStyle);
$areaData->setValue('D2', 'Deal', $rowStyle);

//Prepare total section
$TotalDeposit = 111;
$TotalWithdrawal = 222;
$countDeposit = 333;
$countWithdrawal = 444;

$areaTotal->setValue('I4', 'Total Rows', $rowStyle)->applyFontStyleBold();
$areaTotal->setValue('G5', 'Total Deposit', $rowStyle)->applyFontColor('#3A00FF')->applyFontStyleBold();
$areaTotal->setValue('G6', 'Total Withdrawal', $rowStyle)->applyFontColor('#FF0000')->applyFontStyleBold();
$areaTotal->setValue('G7', 'Net', $rowStyle)->applyFontStyleBold();

$TotalStyle = ['format' => '[BLUE][$$]#,##0.00;[RED]-[$$]#,##0.00', 'text-align' => 'center', 'border' => 'thin', 'font' => ['style' => 'bold']];
$areaTotal->setValue('H5', $TotalDeposit, $TotalStyle);
$areaTotal->setValue('H6', $TotalWithdrawal, $TotalStyle);
$areaTotal->setValue('H7', $TotalDeposit + $TotalWithdrawal, ['format' => '[$$]#,##0.00', 'text-align' => 'center', 'border' => 'thin', 'font' => ['style' => 'bold']]);
$areaTotal->setValue('I5', $countDeposit, $rowStyle)->applyFontStyleBold();
$areaTotal->setValue('I6', $countWithdrawal, $rowStyle)->applyFontStyleBold();
$areaTotal->setValue('I7', $countDeposit + $countWithdrawal, ['format' => '#,##0', 'text-align' => 'center', 'border' => 'thin', 'font' => ['style' => 'bold']]);
//save file
SWAT10101 commented 10 months ago

I have tried the code with the same issue

aVadim483 commented 10 months ago

What language is used in your Excel? Perhaps these problems are related to the locale that are used in your Excel. Try to set the locale to "en":

Excel::create(['DW', 'In Deal', 'Out OutBy', 'Country', 'Symbols']);
$excel->setLocale('en');

If that doesn't work, you can try deleting from this code the lines with formatting one by one until the error goes away.

SWAT10101 commented 10 months ago

@aVadim483 Thank you a lot for helping The issue was in other sheets in code missing @ $sheet->setColFormat('C', 'string'); //Type