更新时间:2022-08-25 09:58:29 来源:赢咖4 浏览545次
Excel中的合并单元格是指将两个或多个相邻单元格合并为一个跨越多行或多列的大单元格。这对于创建需要在单元格范围内居中的标题或标签很有用。
首先,您需要将 Spire.Xls.jar 文件作为依赖项添加到您的 Java 程序中。可以从此链接下载 JAR 文件。如果您使用 Maven,您可以通过将以下代码添加到项目的 pom.xml 文件中轻松地在应用程序中导入 JAR 文件。
<font style="vertical-align: inherit;"><font style="vertical-align: inherit;"><存储库></font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
<存储库></font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
<id>com.e-iceblue</id></font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
<name>e-iceblue</name></font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url></font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
</repository></font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
</存储库></font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
<依赖项></font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
<依赖></font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
<groupId>e-iceblue</groupId></font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
<artifactId>spire.xls</artifactId></font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
<版本>5.4.3</版本></font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
</依赖></font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
</依赖></font></font>
详细步骤如下。
创建一个工作簿实例。
使用Workbook.loadFromFile()方法加载示例 Excel 文档。
使用Workbook.getWorksheets().get()方法获取指定的工作表。
使用Worksheet.getRange().get()方法获取指定范围。
使用XlsRange.merge()方法合并指定范围内的单元格。
使用XlsRange.getCellStyle().setHorizontalAlignment()方法将合并单元格的水平对齐方式设置为 Center 。
使用XlsRange.getCellStyle().setVerticalAlignment()方法将合并单元格的垂直对齐方式设置为 Center 。
使用Workbook.saveToFile()方法保存结果文档。
import com.spire.xls.*;<font></font>
<font></font>
public class MergeCells {<font></font>
public static void main(String[] args){<font></font>
<font></font>
//Create a Workbook instance<font></font>
Workbook workbook = new Workbook();<font></font>
<font></font>
//Load a sample Excel document<font></font>
workbook.loadFromFile("input.xlsx");<font></font>
<font></font>
//Get the first worksheet<font></font>
Worksheet sheet = workbook.getWorksheets().get(0);<font></font>
<font></font>
//Merge cells by range<font></font>
sheet.getRange().get("A2:A4").merge();<font></font>
sheet.getRange().get("A5:A7").merge();<font></font>
<font></font>
//Set the horizontal alignment of merged cells to Center<font></font>
sheet.getRange().get("A2").getCellStyle().setHorizontalAlignment(HorizontalAlignType.Center);<font></font>
sheet.getRange().get("A5").getCellStyle().setHorizontalAlignment(HorizontalAlignType.Center);<font></font>
<font></font>
//Set the vertical alignment of merged cells to Center<font></font>
sheet.getRange().get("A2").getCellStyle().setVerticalAlignment(VerticalAlignType.Center);<font></font>
sheet.getRange().get("A5").getCellStyle().setVerticalAlignment(VerticalAlignType.Center);<font></font>
<font></font>
<font></font>
//Save the result document<font></font>
workbook.saveToFile("MergeCells.xlsx", FileFormat.Version2013);<font></font>
}<font></font>
}
0基础 0学费 15天面授
Java就业班有基础 直达就业
业余时间 高薪转行
Java在职加薪班工作1~3年,加薪神器
工作3~5年,晋升架构
提交申请后,顾问老师会电话与您沟通安排学习