vb.net EPPlus - 按索引而不是字母表示处理多列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15368476/
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
EPPlus - Working with multiple columns by index rather than Alphabetical Representation
提问by John Bustos
I am using EPPlus in my .Net project to output some data into an Excel worksheet.
我在我的 .Net 项目中使用 EPPlus 将一些数据输出到 Excel 工作表中。
Suppose I wanted to format Columns E-G with a specific format. Using EPPlus, I know I can do it as follows:
假设我想用特定格式格式化 Columns EG。使用 EPPlus,我知道我可以这样做:
wks.Cells("E:G").Style.Numberformat.Format = ...
Now, I'm wondering, suppose I wanted to do the same, but by referencing the columns by their index numbers rather than their alphabetical representation - In theory something looking like this:
现在,我想知道,假设我想做同样的事情,但是通过按索引号而不是按字母顺序表示的列来引用列 - 理论上看起来像这样:
wks.Columns("5:7").Style.Numberformat.Format = ...
Now, I know that it would work if I did something like this:
现在,我知道如果我做这样的事情它会起作用:
wks.Cells(1,5,wks.Dimension.End.Row,7).Style.Numberformat.Format = ...
But I'm hoping there's a better / nicer way to do this in EPPlus. Any thoughts / suggestions?
但我希望在 EPPlus 中有更好/更好的方法来做到这一点。有什么想法/建议吗?
Thanks!!
谢谢!!
回答by John Bustos
As an answer to my own question for the sake of helping anyone who comes by this, I ended up creating my own Extension Method Columnsthat would convert the column number into a ExcelRangeobject:
作为对我自己的问题的回答,以帮助任何遇到此问题的人,我最终创建了自己的扩展方法Columns,将列号转换为ExcelRange对象:
''' <summary>
''' Allows you to reference a column by its numeric index rather than its alphabetic representation
''' </summary>
''' <param name="colNum">The index of the column to reference on in the worksheet.</param>
<System.Runtime.CompilerServices.Extension()> _
Public Function Columns(ByVal wks As ExcelWorksheet, ByVal colNum As Integer) As ExcelRange
Dim ColName As String = ReturnColumnName(colNum)
Return wks.Cells(ColName & ":" & ColName)
End Function
''' <summary>
''' Allows you to reference a column by its numeric index rather than its alphabetic representation
''' </summary>
''' <param name="StartColNum">The start col num.</param>
''' <param name="EndColNum">The end col num.</param>
<System.Runtime.CompilerServices.Extension()> _
Public Function Columns(ByVal wks As ExcelWorksheet, ByVal StartColNum As Integer, ByVal EndColNum As Integer) As ExcelRange
Dim StartColName As String = ReturnColumnName(StartColNum)
Dim EndColName As String = ReturnColumnName(EndColNum)
Return wks.Cells(StartColName & ":" & EndColName)
End Function
Private Function ReturnColumnName(ByVal colNum As Integer) As String
Dim d As Integer
Dim m As Integer
Dim Name As String
d = colNum
Name = ""
Do While (d > 0)
m = (d - 1) Mod 26
Name = Chr(65 + m) + Name
d = Int((d - m) / 26)
Loop
Return Name
End Function
回答by Red Taz
I re-wrote the answer given by @John Bustos in c# and then realised I wanted to work with the columns themselves rather than a cell range. John's method has been renamed 'GetColumnCells' in the code below.
我在 c# 中重写了@John Bustos 给出的答案,然后意识到我想使用列本身而不是单元格范围。在下面的代码中,John 的方法已重命名为“GetColumnCells”。
public static List<ExcelColumn> GetColumns(ExcelWorksheet wks, int startColNum, int endColNum) {
int d = startColNum;
List<ExcelColumn> cols = new List<ExcelColumn>();
while (d <= endColNum) {
cols.Add(wks.Column(d));
d++;
}
return cols;
}
public static ExcelRange GetColumnCells(ExcelWorksheet wks, int startColNum, int endColNum) {
string startColName = GetColumnName(startColNum);
string endColName = GetColumnName(endColNum);
return wks.Cells[startColName + ":" + endColName];
}
public static string GetColumnName(int colNum) {
int d, m;
string name = String.Empty;
d = colNum;
while (d > 0) {
m = (d - 1) % 26;
name = ((char)(65 + m)) + name;
d = (d - m) / 26;
}
return name;
}

