943,148 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 5992
  • VB.NET RSS
Jan 30th, 2010
0

List from OpenXML Excel Column Headers

Expand Post »
I am using VB.net 2008 express edition

I have figured out how to use some of the code snippets for the open xml sdk to create a drop down list of all the sheets in an excel file.

I can also use the code snippet to get the value of an individual column header.

What I want is a drop down list of all column headers.

I could create an array containing the alphabet and loop through each column based on the alphabet letter in the array. I would then just exit the loop on the first column with no value returned for column header...but there must be an easy way to return all headers in one query of the xml.

any examples or suggestions would be apreciated.

Thanks
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
watergeek is offline Offline
3 posts
since Jan 2010
Feb 1st, 2010
0
Re: List from OpenXML Excel Column Headers
Show us your code. I recommend extrememl.
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,525 posts
since Oct 2008
Feb 5th, 2010
0
Re: List from OpenXML Excel Column Headers
here is the code from the snippets:

VB.NET Syntax (Toggle Plain Text)
  1. ' Given a document name, a worksheet name, and a cell name, get the column of the cell and return
  2. ' the content of the first cell in that column.
  3. Public Function XLGetColumnHeader(ByVal docName As String, _
  4. ByVal worksheetName As String, ByVal cellName As String) As String
  5.  
  6. Dim returnValue As String = Nothing
  7.  
  8. ' Open the document as read-only.
  9. Using document As SpreadsheetDocument = SpreadsheetDocument.Open(docName, False)
  10. Dim wbPart As WorkbookPart = document.WorkbookPart
  11.  
  12. ' Given a worksheet name, first find the Sheet that corresponds to the name.
  13. Dim sheet = wbPart.Workbook.Descendants(Of Sheet)(). _
  14. Where(Function(s) s.Name = worksheetName).FirstOrDefault
  15. If sheet Is Nothing Then
  16. ' The specified worksheet does not exist.
  17. Return Nothing
  18. End If
  19.  
  20. ' Given the Sheet,
  21. Dim worksheetPart As WorksheetPart = CType(wbPart.GetPartById(sheet.Id), WorksheetPart)
  22.  
  23. ' Get the column name for the specified cell.
  24. Dim columnName As String = GetColumnName(cellName)
  25.  
  26. ' Get the cells in the specified column and order them by row.
  27. Dim headCell = worksheetPart.Worksheet.Descendants(Of Cell)(). _
  28. Where(Function(c) String.Compare(GetColumnName(c.CellReference.Value), columnName, True) = 0). _
  29. OrderBy(Function(r) GetRowIndex(r.CellReference)).FirstOrDefault()
  30.  
  31. If headCell Is Nothing Then
  32. ' The specified column does not exist.
  33. Return Nothing
  34. End If
  35.  
  36. ' If the content of the first cell is stored as a shared string, get the text of the first cell
  37. ' from the SharedStringTablePart and return it. Otherwise, return the string value of the cell.
  38. If headCell.DataType IsNot Nothing AndAlso headCell.DataType.Value = CellValues.SharedString Then
  39. Dim sharedStringPart As SharedStringTablePart = wbPart.GetPartsOfType(Of SharedStringTablePart)().FirstOrDefault()
  40. If sharedStringPart IsNot Nothing Then
  41. Dim items = sharedStringPart.SharedStringTable.Elements(Of SharedStringItem)()
  42. returnValue = items.ElementAt(Integer.Parse(headCell.CellValue.Text)).InnerText
  43. End If
  44. Else
  45. returnValue = headCell.CellValue.Text
  46. End If
  47. End Using
  48. Return returnValue
  49. End Function
  50.  
  51. ' Given a cell name, parses the specified cell to get the column name.
  52. Private Function GetColumnName(ByVal cellName As String) As String
  53. ' Create a regular expression to match the column name portion of the cell name.
  54. Dim regex As Regex = New Regex("[A-Za-z]+")
  55. Dim match As Match = regex.Match(cellName)
  56. Return match.Value
  57. End Function
  58.  
  59. ' Given a cell name, parses the specified cell to get the row index.
  60. Private Function GetRowIndex(ByVal cellName As String) As UInteger
  61. ' Create a regular expression to match the row index portion the cell name.
  62. Dim regex As Regex = New Regex("\d+")
  63. Dim match As Match = regex.Match(cellName)
  64. Return UInteger.Parse(match.Value)
  65. End Function

here is what I came up with looping trough each column until the first column with no header:

VB.NET Syntax (Toggle Plain Text)
  1. ' get a list of column headers.
  2. Public Function XLColumnList(ByVal fileName As String, ByVal worksheetName As String) As List(Of String)
  3. 'Declare variable TO HOLd list of column headers
  4. Dim Headers As List(Of String) = New List(Of String)
  5.  
  6. 'declare variable to hold the alphabet & a letter number
  7. Dim Alphabet() As String = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", _
  8. "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
  9.  
  10. 'Variable to hold the current index for the alphabet array
  11. Dim AlphabetIndex As Integer = 0
  12.  
  13. 'Declare variable to count which iteration of the alphabet the loop is on
  14. Dim AlphabetCount As Integer = 0
  15.  
  16.  
  17. 'variable to hold last column header name and give it a value so the loop will start
  18. Dim LastHeader As String = "None"
  19.  
  20.  
  21. While Not LastHeader Is Nothing
  22.  
  23. Select Case AlphabetCount
  24. Case 0 To 26
  25. LastHeader = XLGetColumnHeader(fileName, worksheetName, Alphabet(AlphabetIndex) + "1")
  26. Case 27 To 52
  27. LastHeader = XLGetColumnHeader(fileName, worksheetName, "A" + Alphabet(AlphabetIndex) + "1")
  28. Case 52 To 78
  29. LastHeader = XLGetColumnHeader(fileName, worksheetName, "B" + Alphabet(AlphabetIndex) + "1")
  30. Case 79 To 104
  31. LastHeader = XLGetColumnHeader(fileName, worksheetName, "C" + Alphabet(AlphabetIndex) + "1")
  32. Case 105 To 130
  33. LastHeader = XLGetColumnHeader(fileName, worksheetName, "D" + Alphabet(AlphabetIndex) + "1")
  34. Case 131 To 156
  35. LastHeader = XLGetColumnHeader(fileName, worksheetName, "E" + Alphabet(AlphabetIndex) + "1")
  36. Case 157 To 182
  37. LastHeader = XLGetColumnHeader(fileName, worksheetName, "F" + Alphabet(AlphabetIndex) + "1")
  38. End Select
  39.  
  40. If Not LastHeader Is Nothing Then
  41. Headers.Add(LastHeader)
  42. End If
  43. AlphabetCount = AlphabetCount + 1
  44. If AlphabetIndex < 25 Then
  45. AlphabetIndex = AlphabetIndex + 1
  46. Else
  47. AlphabetIndex = 0
  48. End If
  49. If AlphabetCount > 182 Then
  50. Headers.Add("Exceeded 182 column limit")
  51. LastHeader = Nothing
  52. End If
  53. End While
  54. If Headers Is Nothing Then
  55. Headers.Add("No column headers.")
  56. End If
  57. Return Headers
  58. End Function

I was thinking that it would be cleaner if I could do something similar to "select CellValue where row = 1" (after opening the apropriate file and worksheet).

It seems like editing this part of the XLGetColumnHeader function:
VB.NET Syntax (Toggle Plain Text)
  1. Dim headCell = worksheetPart.Worksheet.Descendants(Of Cell)(). _
  2. Where(Function(c) String.Compare(GetColumnName(c.CellReference.Value), columnName, True) = 0). _
  3. OrderBy(Function(r) GetRowIndex(r.CellReference)).FirstOrDefault()
Reputation Points: 10
Solved Threads: 0
Newbie Poster
watergeek is offline Offline
3 posts
since Jan 2010
Feb 5th, 2010
0
Re: List from OpenXML Excel Column Headers
I don't think that there is an easy way to get list of columns with OpenXML API. Of course, look at my post #2, ExtremeML; a new open source library that adds a powerful layer of additional functionality to the OpenXml SDK. Read this article.
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,525 posts
since Oct 2008
Feb 15th, 2010
0
Re: List from OpenXML Excel Column Headers
I assume you are thinking that I would do something similar to the example for itrating over cells?

Are there any example of using extremeML to populate a datagridview with speadsheet content?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
watergeek is offline Offline
3 posts
since Jan 2010
Aug 5th, 2010
0
Re: List from OpenXML Excel Column Headers
for opening xml excel column header, you can use a vb.net excel component. It's named Spire.XLS.

Hope help to you.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
David Sanchez is offline Offline
1 posts
since Aug 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: Worksheet.Unprotect - Office Interop - Difference between 2003 and 2007
Next Thread in VB.NET Forum Timeline: Open/ modify Excel through ADO.net





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC