使用PHPExcel按行和列合并Excel中的单元格 [英] Merging cells in Excel by rows and columns together using PHPExcel

查看:1088
本文介绍了使用PHPExcel按行和列合并Excel中的单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用 PHPExcel按行和按列合并Excel(xlsx)中的单元格.我尝试了以下方法.

I need to merge cells in Excel (xlsx) by rows and again by columns using PHPExcel. I tried the following.

$sheet->mergeCells("G".($row_count+1).":G".($row_count+4));             
$sheet->mergeCells("H".($row_count+1).":H".($row_count+4));             
$sheet->mergeCells("I".($row_count+1).":I".($row_count+4));     

变量$row_count具有一些不可预测的动态值,例如25、50、75等(无规则模式).

Where the variable $row_count has some unpredictable dynamic value like 25, 50, 75 and so on (no regular pattern).

它会合并前面的快照> 中显示的单元格在注释单元格下面.在按行合并这些单元格之后,我尝试按如下所示按列合并它们.

It merges the cells as shown in the preceding snap shot as can be seen immediately below the Note cell. After merging these cells by rows, I'm trying to merge them by columns as follows.

$sheet->mergeCells("G".($row_count+1).":I".($row_count+1));             

但是它不起作用.当我尝试打开excel文件时,它要求您进行确认(带有确认框)

but it doesn't work. When I try to open the excel file, it asks for a confirmation (with a confirmation box)

Excel在"report.xlsx"中发现了不可读的内容.你想要_____吗 恢复此工作簿的内容?如果您相信这个来源 工作簿中,单击是".

Excel found unreadable content in 'report.xlsx'. Do you want to recover the contents of this workbook? If you trust the source of this workbook, click Yes.

然后如何在Excel中按行和列将单元格合并在一起?

How to merge cells by rows and columns together in Excel then?

推荐答案

合并仅需要有效范围的单元格(例如A1:B2),因此您的

Merging simply requires a valid range of cells like A1:B2, so your

$sheet->mergeCells("G".($row_count+1).":I".($row_count+1));

应该可以正常工作.

能否请您通过一个简单的测试用例进行实验,以证明这是导致您遇到问题的原因,而不是脚本中的其他问题

Can you please experiment with a simple test case to prove that this is causing you a problem, and not something else in your script

编辑

重读您的问题后: 您的问题可能是您正在尝试合并已经属于合并范围的单元格,而不是合并每一行,然后尝试按列合并,一次尝试合并整个范围.

After rereading your question: Your problem may be that you're trying to merge cells that are already part of a merge range, rather than merging each row, then trying to merge by column, try merging the full rangein one go.

$sheet->mergeCells("G".($row_count+1).":I".($row_count+4));              

这篇关于使用PHPExcel按行和列合并Excel中的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆