I have a excel with the following data:
Week---Parts
32--------part1
32--------part1
33--------part2
33--------part2
33--------part3
34--------part1
34--------part4
34--------part5
From this, I would like to get somethin glike this in datagridview
Week---Part1---PArt2---Part3----Part4---Part5
32-----2------0--------0-------0--------0
33-----0------2--------1-------0--------0
34-----1------0--------0-------1--------1
This program is importing excel files with this data and it is also not connected to a database.
Using VB.NET code, how can I make the desired results?
Tried this (from Hari Prasad) but no output. NEW in VB.NET
Dim filePath As String = OpenFileDialog1.FileName
Dim extension As String = Path.GetExtension(filePath)
conStr = String.Empty
Select Case extension
Case ".xls"
'Excel 97-03
conStr = String.Format(Excel03ConString, filePath, "YES")
Exit Select
Case ".xlsx"
'Excel 07
conStr = String.Format(Excel07ConString, filePath, "YES")
Exit Select
End Select
'Get the name of the First Sheet.
Using con As New OleDbConnection(conStr)
Using cmd As New OleDbCommand()
cmd.Connection = con
con.Open()
Dim dtExcelSchema As DataTable = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
sheetName = dtExcelSchema.Rows(0)("TABLE_NAME").ToString()
con.Close()
End Using
End Using
'Read Data from the First Sheet.
Using con As New OleDbConnection(conStr)
Dim cmd As OleDbCommand = New OleDbCommand((Convert.ToString("SELECT parts, week From [") & sheetName) + "] ", con)
Dim adapter As New OleDbDataAdapter(cmd)
Dim ds As New DataSet()
adapter.Fill(ds)
Dim contents = ds.Tables(0).AsEnumerable().Select(Function(x) New With { _
Key .week = x.Field(Of Double)("week"), _
Key .parts = x.Field(Of String)("parts") _
})
Dim result = contents.GroupBy(Function(g) g.week).[Select](Function(g) New With { _
Key .week = g.Key, _
Key .groups = g.GroupBy(Function(s) s.parts) _
}).[Select](Function(n) New With { _
n.week, _
Key .PartAndCount = n.groups.[Select](Function(s) New With { _
Key .part = s.Key, _
Key .count = s.Count() _
}) _
})
Dim gview = New DataGridView()
gview.Columns.Add("week", "Week")
For Each c As String In contents.[Select](Function(c) c.parts).Distinct()
gview.Columns.Add(c, c)
Next
For Each r As String In result 'Error: cannot converted to string
Dim rowIndex As Integer = gview.Rows.Add()
Dim row As DataGridViewRow = gview.Rows(rowIndex)
For Each s As String In r.PartAndCount 'Error: is not a member of string
row.Cells(s.part).Value = s.Count
Next
Next
End Using