
假设我想格式化具有特定格式的列E-G.使用EPPlus,我知道我可以这样做:
wks.Cells("E:G").Style.Numberformat.Format = ... 现在,我想知道,假设我想做同样的事情,但是通过他们的索引号而不是字母表示来引用列 – 理论上看起来像这样:
wks.Columns("5:7").Style.Numberformat.Format = ... 现在,我知道如果我做了这样的事情会有效:
wks.Cells(1,5,wks.Dimension.End.Row,7).Style.Numberformat.Format = ...
但我希望在EPPlus中有更好/更好的方法.有什么想法/建议?
谢谢!!
解决方法 作为我自己的问题的答案,为了帮助任何人,我最终创建了自己的扩展方法列,将列号转换为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 FunctionPrivate 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 nameEnd Function总结
以上是内存溢出为你收集整理的c# – EPPlus – 按索引而不是按字母表示法处理多个列全部内容,希望文章能够帮你解决c# – EPPlus – 按索引而不是按字母表示法处理多个列所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)