FIX_all Excel auto width

This commit is contained in:
sanjeev.p 2025-12-15 17:12:33 +05:30
parent dc2a945a06
commit 71b4baea25

View File

@ -3,9 +3,12 @@
namespace App\Controllers; namespace App\Controllers;
use CodeIgniter\RESTful\ResourceController; use CodeIgniter\RESTful\ResourceController;
use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx; use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Style\Border; use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate; // <--- THIS MUST BE HERE
use PhpOffice\PhpSpreadsheet\Style\Alignment;
class ExcelExportController extends ResourceController class ExcelExportController extends ResourceController
{ {
@ -48,8 +51,12 @@ class ExcelExportController extends ResourceController
$sheet = $spreadsheet->getActiveSheet(); $sheet = $spreadsheet->getActiveSheet();
// 1. Title (Merged across the header columns) // 1. Title (Merged across the header columns)
$lastCol = $sheet->getHighestColumn(); // $lastCol = $sheet->getHighestColumn();
$sheet->mergeCells("A1:{$lastCol}1"); $columnCount = count($header);
$lastCol = Coordinate::stringFromColumnIndex($columnCount); // <--- MUST be ONLY 'Coordinate::'
$sheet->mergeCells("A1:{$lastCol}1"); // Merges A1:E1
// $sheet->mergeCells("A1:{$lastCol}1");
$sheet->setCellValue('A1', $title); $sheet->setCellValue('A1', $title);
$sheet->getStyle('A1')->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER); $sheet->getStyle('A1')->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
@ -394,7 +401,7 @@ class ExcelExportController extends ResourceController
$this->streamExcelFile( $this->streamExcelFile(
$header, $header,
$data, $data,
$PartnerName ." Low Premium Agents Policy Details", // Adjusted Title "Partners Low Premium Policy Details",
$fileName, $fileName,
'E', // Total Label Column 'E', // Total Label Column
'F' // Total Value Column 'F' // Total Value Column
@ -461,7 +468,7 @@ class ExcelExportController extends ResourceController
// Renamed helper function for consistency: getAgentMonthlyPoliciesData // Renamed helper function for consistency: getAgentMonthlyPoliciesData
$data = $this->getAgentMonthlyPoliciesData($managerId, $agentId, $month); $data = $this->getAgentMonthlyPoliciesData($managerId, $agentId, $month);
$header = ['Partner Name','Partner Code' ,'Issue Date', 'Policy Number', 'Premium Amount']; $header = ['Partner Name','Partner Code' , 'Policy Number','Issue Date', 'Premium Amount'];
if (empty($data)) { if (empty($data)) {
throw new \RuntimeException('No policies found for this agent for the specified month.', 404); throw new \RuntimeException('No policies found for this agent for the specified month.', 404);
@ -652,7 +659,7 @@ class ExcelExportController extends ResourceController
$this->streamExcelFile( $this->streamExcelFile(
$header, $header,
$data, $data,
"Staff And Product Policies Details for {$monthName}", "Staff And Product Policies Details for {$monthName}",
$fileName, $fileName,
'D', // Total Label Column 'D', // Total Label Column
'E' // Total Value Column 'E' // Total Value Column