【发布时间】:2012-01-29 09:29:27
【问题描述】:
我有导出到 excel 的代码,并且我有一个列数组。 .
var colheaders = new string[] {"Name", "Age", "Total", "Date"}
现在我有这样的代码来设置标题
excelExport.SetCell("A", 1, "Name");
excelExport.SetCell("B", 1, "Age");
excelExport.SetCell("C", 1, "Total");
excelExport.SetCell("D", 1, "Date");
问题是,如果我有 50 列并且我想在开始时添加一个,我必须去更新每一列“A”、“B”、“C”等中的字母。 .
因为我已经有一个字符串头数组,所以我想要这样的东西:
foreach (string colheader in colheaders)
{
excelExport.SetCell("A", 1, colheader);
}
但在这种情况下我需要动态设置字母“A”。像这样的:
int i = 0;
foreach (string colheader in colheaders)
{
excelExport.SetCell(GetCol(i), 1, colheader);
i++;
}
注意:
另外,在 Z 之后,我需要去 AA,然后是 AB,然后是 AC,等等。 .匹配 Excel 列,因此逻辑必须超过 26 列
【问题讨论】:
-
看看这里stackoverflow.com/questions/1011732/…它可能对你有帮助......
-
我不知道
SetCell函数在做什么,但是关于 Excel 的一个鲜为人知的事实是,您可以使用数字而不是字母来指代列:Cells(1, "AA")是一样的作为Cells(1, 27). -
我找到了一个可行的解决方案 [这里][1]: [1]: stackoverflow.com/questions/837155/…
-
@leora 我建议您将自己的问题作为重复项关闭(而不是让其他人将其标记为关闭)。