使用范围时,Excel引发HRESULT:0x800A03EC异常 [英] Excel throws HRESULT: 0x800A03EC exception when using Range
问题描述
大家好!我真的是Visual Basic和Excel的新手,所以我不知道是否有更好的方法来做我想做的事情,但这并不重要.
Hello guys! I'm really new to Visual Basic and Excel so i don't know if there's a better way to do what i'm trying to do, but this is not important.
我正在尝试设置用于设置具有某些边框和类似内容的excel单元格格式的范围.我不知道如何找到最后用过的电池,所以我想到了这个.
I'm trying to set the range for formatting my excel cells with some borders and stuff like that. I don't know how to find the last used cell so i came up with this.
range = "A1:AD1"
For Each dataRow In dataTable.Rows
rowIndex += 1
Next
我从我的数据表中创建了excel工作表,因此它们具有相同的列和行.我的 range 是较早计算出来的,它取决于我的excel工作表中有多少列(数据表/excel列可能会根据用户输入而改变)
I created the excel sheet from my data table so they have the same columns and rows. My range was calculated earlier and it depends of how many columns i have in my excel sheet (data table/excel columns could change depending on user input)
计算完数据表中的每一行后,我这样做了:
after counting every rows in my data table i did this:
range = range.Substring(0, range.Length() - 1) & Convert.ToString(rowIndex)
最后一行的结果是"A1:AD413" .我在excel上对其进行了检查,最后一次使用的单元格正是 AD413
The result of last line is "A1:AD413". I checked it on my excel and last used cell is exactly AD413
现在,当我尝试将样式应用于我的范围时,excel会抛出 HRESULT:0x800A03EC
exception,我知道这类似于超出范围异常" .为什么Excel会抛出它?我该如何解决?范围没有错!
Now, when i try to apply my style on my range, excel throws HRESULT: 0x800A03EC
exception and I know that is something like "Out of range exception". Why excel is throwing it? how can I fix it? The range isn't wrong!
excel引发异常的行
wSheet.Cells(range).VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter
我的代码
rowIndex = 0
For Each dr In dt.Rows
rowIndex += 1
Next
range = range.Substring(0, range.Length() - 1) & Convert.ToString(rowIndex + 1)
wSheet.Cells(range).VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter
wSheet.Cells(range).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft
wSheet.Cells(range).Borders.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous
推荐答案
Cells()对于行和列采用两个整数参数,对于单个单元格地址采用字符串参数.如果要定位范围,则需要使用Worksheet.Range而不是Worksheet.Cells.Worksheet.Range可以接受开始和结束的单元格地址,例如:
Cells() takes two integer parameters for row and column, or a string parameter for a single cell address. If you want to target a range, you need to use Worksheet.Range instead of Worksheet.Cells. Worksheet.Range can accept a starting and ending cell address, such as:
wSheet.Range("A1", "AD413").Borders.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous
在您的情况下,由于您已经在名为range的变量中包含了字符串"A1:AD413",因此您只需将单元格格式行更改为:
In your case, since you already have the string "A1:AD413" in a variable called range, you could just change the cell-formatting lines to:
wSheet.Range(range.Split(":")(0), range.Split(":")(1)).Borders.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous
这篇关于使用范围时,Excel引发HRESULT:0x800A03EC异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!