2011-04-21 197 views
1

我刚刚开始使用SpreadsheetGear的项目。 CopyFromDataTable用于从数据库获取所有数据。有很多领域可以使用;我们格式化他们,隐藏在一定范围,如果他们是空的,等等。作为一个例子:SpreadsheetGear按名称获取列?

SpreadsheetGear.IRange cells = worksheet.Cells;  
cells["G1"].EntireColumn.NumberFormat = "m/d/yyyy"; 

因此,如果列被重新排列或一个被删除,看来我不得不回去调整所有硬编码值以反映单元格移位。有没有什么办法通过它的第一个单元格的名称来引用列,以便可以使事物更加模块化?

我发现this,但它仍然需要对列进行硬编码。

回答

0

一种可能是搜索标题/ FIELDNAMES

Sub test() 
Dim strFieldName As String 
Dim rngHeading As Range 

With Sheet1 

    strFieldName = "Heading 3" 'find Heading Name 
    With .Rows("1:1") 'search row 1 headings 

     Set rngHeading = .Find(What:=strFieldName, LookIn:=xlValues, lookAt:=xlWhole, _ 
     SearchOrder:=xlByColumns, SearchDirection:=xlNext, _ 
     MatchCase:=False, SearchFormat:=False) 

     'check if found 
     If Not rngHeading Is Nothing Then 

     MsgBox rngHeading.Address 
     MsgBox rngHeading.Row 
     MsgBox rngHeading.Column 
     rngHeading.EntireColumn.NumberFormat = "m/d/yyyy" 

     End If 

    End With 

End With 

End Sub 
+0

感谢您的答复 - 没有忘记这件事,但还没有时间来测试这个问题,最终得到它并接受答案,如果它。 – 2011-05-03 19:10:22

1

为的SpreadsheetGear一个伟大的选择是http://officehelper.codeplex.com。使用这个库你可以创建一个模板xlsx,你可以在其中完成所有的格式化,然后你可以使用这个“模板”文档中的部分来创建你的最终的,被报告的xlsx文件,而不需要编程设置数字格式,比如在你的例子中。