8
A
回答
10
我不是VSTO C#的大用户 - 我通常选择VBA。但是,下面这对函数可能对您有用。不确定的,如果他们可以精简一些:
public string RangeAddress(Excel.Range rng)
{
return rng.get_AddressLocal(false, false, Excel.XlReferenceStyle.xlA1,
missing, missing);
}
public string CellAddress(Excel.Worksheet sht, int row, int col)
{
return RangeAddress(sht.Cells[row, col]);
}
的RangeAddress
将采取任何Excel范围和扔你回来的地址。如果您想要绝对样式(带有美元符号,例如$ C $ 3),则必须将get_AddressLocal调用的前两个参数更改为true,true。
要从行/列对获取单元格地址,可以使用CellAddress
。它确实需要一张表来获取地址。但是,如果您不想提供工作表(这可能会或可能不会工作,取决于您在VSTO会话中打开的内容),您可以交换(Excel.Worksheet)ActiveSheet
。
0
你能以某种方式调用Excel的本地公式或重复其功能的函数吗?这将是刚刚
=CELL("address")
-1
public string GetCellAddress(int row, int col) {
return (char)((int)'A' + (row - 1)) + col.ToString();
}
2
这一个(未经测试)也应与列地址的工作,而忽视了超过26:
using System.Text;
public string GetCellAddress(int row, int col) {
StringBuilder sb = new StringBuilder();
col--;
do {
sb.Insert(0, (char)('A' + (col % 26)));
col /= 26;
} while (col > 0);
sb.Append(row);
return sb.ToString();
}
更正:列地址是向后
+3
这将返回col1而不是AA1的col 27。 – 2011-03-15 18:37:40
0
细胞(2,3)。地址
3
只是一个改进,col--是在错误的地方
static string GetCellAddress(int row, int col)
{
StringBuilder sb = new StringBuilder();
do
{
col--;
sb.Insert(0, (char)('A' + (col % 26)));
col /= 26;
} while (col > 0);
sb.Append(row);
return sb.ToString();
}
0
我使用的是从零开始的行和列的索引。所以我不得不将以前答案的代码修改为以下内容。 对于所有可能的地址i.E,这需要一些试验和错误。 A1,Z2,AA2,ZZ10,...
public string GetAddress(int col, int row)
{
col++;
StringBuilder sb = new StringBuilder();
do
{
col--;
sb.Insert(0, (char)('A' + (col % 26)));
col /= 26;
} while (col > 0);
sb.Append(row + 1);
return sb.ToString();
}
相关问题
- 1. 我怎样才能从URL
- 2. 我怎样才能从WP7
- 3. 我怎样才能从TEntity
- 4. 我怎样才能从DataGridView
- 5. 我怎样才能从HBITMAP
- 6. 我怎样才能从Qt
- 7. 我怎样才能在Excel表格
- 8. 我怎样才能
- 9. 我怎样才能
- 10. 我怎样才能
- 11. 我怎样才能
- 12. 我怎样才能节点,我从XML
- 13. 我我怎样才能从cheerio
- 14. 我怎样才能从Excel使用Apache poi更新行数
- 15. 我怎样才能把数据从excel解析成模型?
- 16. 我怎样才能在Ionic2
- 17. 我怎样才能在Zebble
- 18. 我怎样才能在PreferenceScreen
- 19. 我怎样才能在vim
- 20. 我怎样才能绕UIView
- 21. 我怎样才能标签
- 22. 我怎样才能在图
- 23. 我怎样才能在Python
- 24. 我怎样才能event_search_max?
- 25. 我怎样才能在VB.net
- 26. 我怎样才能在DB2
- 27. 我怎样才能在Java
- 28. 我怎样才能在Python
- 29. 我怎样才能在PAM
- 30. 我怎样才能在JSP
另请参见:http://stackoverflow.com/questions/181596/how-to-convert-a-column-number-eg-127-into- an-excel-column-eg-aa – Graham 2010-04-13 13:52:07