PHPexcel合并列和行
PHPexcel merging columns and rows
所以我试图将多个行和列合并在一起,但是当我打开 Excel 文件时出现错误。
这就是我的 excel 现在的样子:
我要做的是让它看起来像这样:
我用下面的代码做了这个:
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
//Columns merging
$objPHPExcel->getActiveSheet()->mergeCells('A13:A16');
$objPHPExcel->getActiveSheet()->mergeCells('B13:B16');
$objPHPExcel->getActiveSheet()->mergeCells('C13:C16');
$objPHPExcel->getActiveSheet()->mergeCells('D13:D15');
$objPHPExcel->getActiveSheet()->mergeCells('E13:E15');
$objPHPExcel->getActiveSheet()->mergeCells('F13:F15');
$objPHPExcel->getActiveSheet()->mergeCells('G13:G15');
$objPHPExcel->getActiveSheet()->mergeCells('H13:H15');
//Adding cell text
$objPHPExcel->getActiveSheet()->SetCellValue('A13', 'Sr. No.');
$objPHPExcel->getActiveSheet()->SetCellValue('B13', 'Component name');
$objPHPExcel->getActiveSheet()->SetCellValue('C13', 'Features');
$objPHPExcel->getActiveSheet()->SetCellValue('D13', 'Title');
$objPHPExcel->getActiveSheet()->SetCellValue('D16', 'HW Details');
$objPHPExcel->getActiveSheet()->SetCellValue('E16', 'Test Case ID/ Test Steps');
$objPHPExcel->getActiveSheet()->SetCellValue('F16', 'Expected');
$objPHPExcel->getActiveSheet()->SetCellValue('G16', 'Result');
$objPHPExcel->getActiveSheet()->SetCellValue('H16', 'Remark');
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save('some_excel_file2.xlsx');
我想我可以先合并列,然后再合并行,以获得所需的输出,方法是在合并列后执行以下操作:
$objPHPExcel->getActiveSheet()->mergeCells('D13:H13');
$objPHPExcel->getActiveSheet()->mergeCells('D14:H14');
$objPHPExcel->getActiveSheet()->mergeCells('D15:H15');
但这会产生错误。
我应该如何合并这些 rows/columns 才能得到最后一张图片?
所以我的大脑就是不工作,现在我有了解决方案,所以要合并一个框,我只需要使用 D13:H15
标记一个区域,如下图所示:
因此代码将如下所示:
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
//Columns merging
$objPHPExcel->getActiveSheet()->mergeCells('A13:A16');
$objPHPExcel->getActiveSheet()->mergeCells('B13:B16');
$objPHPExcel->getActiveSheet()->mergeCells('C13:C16');
//SOLUTION HERE
$objPHPExcel->getActiveSheet()->mergeCells('D13:H15');
//Adding cell text
$objPHPExcel->getActiveSheet()->SetCellValue('A13', 'Sr. No.');
$objPHPExcel->getActiveSheet()->SetCellValue('B13', 'Component name');
$objPHPExcel->getActiveSheet()->SetCellValue('C13', 'Features');
$objPHPExcel->getActiveSheet()->SetCellValue('D13', 'Title');
$objPHPExcel->getActiveSheet()->SetCellValue('D16', 'HW Details');
$objPHPExcel->getActiveSheet()->SetCellValue('E16', 'Test Case ID/ Test Steps');
$objPHPExcel->getActiveSheet()->SetCellValue('F16', 'Expected');
$objPHPExcel->getActiveSheet()->SetCellValue('G16', 'Result');
$objPHPExcel->getActiveSheet()->SetCellValue('H16', 'Remark');
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save('some_excel_file2.xlsx');
所以我试图将多个行和列合并在一起,但是当我打开 Excel 文件时出现错误。
这就是我的 excel 现在的样子:
我要做的是让它看起来像这样:
我用下面的代码做了这个:
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
//Columns merging
$objPHPExcel->getActiveSheet()->mergeCells('A13:A16');
$objPHPExcel->getActiveSheet()->mergeCells('B13:B16');
$objPHPExcel->getActiveSheet()->mergeCells('C13:C16');
$objPHPExcel->getActiveSheet()->mergeCells('D13:D15');
$objPHPExcel->getActiveSheet()->mergeCells('E13:E15');
$objPHPExcel->getActiveSheet()->mergeCells('F13:F15');
$objPHPExcel->getActiveSheet()->mergeCells('G13:G15');
$objPHPExcel->getActiveSheet()->mergeCells('H13:H15');
//Adding cell text
$objPHPExcel->getActiveSheet()->SetCellValue('A13', 'Sr. No.');
$objPHPExcel->getActiveSheet()->SetCellValue('B13', 'Component name');
$objPHPExcel->getActiveSheet()->SetCellValue('C13', 'Features');
$objPHPExcel->getActiveSheet()->SetCellValue('D13', 'Title');
$objPHPExcel->getActiveSheet()->SetCellValue('D16', 'HW Details');
$objPHPExcel->getActiveSheet()->SetCellValue('E16', 'Test Case ID/ Test Steps');
$objPHPExcel->getActiveSheet()->SetCellValue('F16', 'Expected');
$objPHPExcel->getActiveSheet()->SetCellValue('G16', 'Result');
$objPHPExcel->getActiveSheet()->SetCellValue('H16', 'Remark');
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save('some_excel_file2.xlsx');
我想我可以先合并列,然后再合并行,以获得所需的输出,方法是在合并列后执行以下操作:
$objPHPExcel->getActiveSheet()->mergeCells('D13:H13');
$objPHPExcel->getActiveSheet()->mergeCells('D14:H14');
$objPHPExcel->getActiveSheet()->mergeCells('D15:H15');
但这会产生错误。
我应该如何合并这些 rows/columns 才能得到最后一张图片?
所以我的大脑就是不工作,现在我有了解决方案,所以要合并一个框,我只需要使用 D13:H15
标记一个区域,如下图所示:
因此代码将如下所示:
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
//Columns merging
$objPHPExcel->getActiveSheet()->mergeCells('A13:A16');
$objPHPExcel->getActiveSheet()->mergeCells('B13:B16');
$objPHPExcel->getActiveSheet()->mergeCells('C13:C16');
//SOLUTION HERE
$objPHPExcel->getActiveSheet()->mergeCells('D13:H15');
//Adding cell text
$objPHPExcel->getActiveSheet()->SetCellValue('A13', 'Sr. No.');
$objPHPExcel->getActiveSheet()->SetCellValue('B13', 'Component name');
$objPHPExcel->getActiveSheet()->SetCellValue('C13', 'Features');
$objPHPExcel->getActiveSheet()->SetCellValue('D13', 'Title');
$objPHPExcel->getActiveSheet()->SetCellValue('D16', 'HW Details');
$objPHPExcel->getActiveSheet()->SetCellValue('E16', 'Test Case ID/ Test Steps');
$objPHPExcel->getActiveSheet()->SetCellValue('F16', 'Expected');
$objPHPExcel->getActiveSheet()->SetCellValue('G16', 'Result');
$objPHPExcel->getActiveSheet()->SetCellValue('H16', 'Remark');
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save('some_excel_file2.xlsx');