vb.net Excel合并单元格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2825698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:52:26  来源:igfitidea点击:

Merge Cell in Excel

vb.netmergecell

提问by Mark

How to merge cells using Celland not Rangein vb.net

如何在 vb.net 中使用Cell和不合并单元格Range

I'll try this code but it doesn't work

我会试试这个代码,但它不起作用

excelSheet.Cells(1, 10).Merge()

Can anyone help me..

谁能帮我..

回答by goggin13

The cells property of a worksheet refers to a single cell. So you are trying to merge just the cell at A10. As its already one cell, this doesn't do anything. I'm not sure you can do it with just the Cells property, as it will always be one cell only. Why are you avoiding Range?

工作表的单元格属性是指单个单元格。因此,您试图仅合并 A10 处的单元格。由于它已经是一个单元格,因此这没有任何作用。我不确定您是否可以仅使用 Cells 属性来完成此操作,因为它始终只有一个单元格。你为什么要避开 Range?

This uses range, but would still use the cells property to target the range constraints

这使用范围,但仍会使用单元格属性来定位范围约束

excelSheet.Range(excelSheet.Cells(1, 1),excelSheet.Cells(1, 10)).Merge

Also, I think the command is Merge, not Merge(), at least when I run it.

另外,我认为命令是 Merge,而不是 Merge(),至少在我运行它时是这样。

Sorry if that isn't helpful, give us some more details and I'll look harder if that doesn't work for you.

抱歉,如果这没有帮助,请给我们提供更多详细信息,如果这对您不起作用,我会更加努力。

回答by Andrew

Another solution - fix to your values

另一个解决方案 - 修正你的价值观

ActiveCell.Value = Range("G8") & Range("H8")

回答by user3134168

Try this. It can be used for Range only. I don't know how to merge cells.

尝试这个。它只能用于范围。我不知道如何合并单元格。

Dim xlsApp As New Excel.Application
xlsApp .Visible = True
Dim xlsWorkbook As Excel.Workbook = xlsApp.Workbooks.Open("..\TestWorkbook.xls")
Dim xlsWorkSheet As Excel.Worksheet = DirectCast(xlsWorkbook.Worksheets("Sheet1"), Excel.Worksheet)
xlsWorkSheet.Range("A1:D1").MergeCells = True

Remember to import the needed libs.

请记住导入所需的库。

回答by user3277445

easiest way

最简单的方法

ExcelSheet.Range("A1:H1").merge()

ExcelSheet.Range("A1:H1").merge()