我有以下数据:
周---零件
32 -------- part1的
32 -------- part1的
33 --------第2部分
33 --------第2部分
33 -------- 3部分
34 -------- part1的
34 -------- 4部分
34 -------- PART5
由此,我想在datagridview
中得到一些东西周--- ---第一部分PART2 --- ----第3部分第四部分--- 5部分
32 ----- ------ 2 0 0 -------- ------- -------- 0 0
33 ----- ------ 0 2 -------- 1 ------- -------- 0 0
34 ----- ------ 1 0 0 -------- ------- -------- 1 1
此程序正在使用此数据导入excel文件,但它也未连接到数据库。
使用VB.NET代码,如何获得所需的结果?
试过这个(来自Hari Prasad),但没有输出。 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
答案 0 :(得分:0)
DataGridView只需要一个包含行和列的数据源(例如DataTable),如下所示; 1.每行将具有按周递增的唯一周列顺序 2.每一行还有一列,每列都包含数据中出现的部分编号(根据上述问题中的数据)
将使用什么数据源取决于偏好和要求。例如,如果源数据来自数据库,则网格视图通常使用查询来填充网格,或者是硬编码或无法转换为Collection然后您可以使用DataTable并填充行/列
答案 1 :(得分:0)
例如,你有这个类来存储你的excel数据(你没有在你的问题中指明这个,所以我用我的想象力:)。
struct ExcelResults
{
public string Week;
public string Part;
}
要解决您的任务,您需要dictionary两个键。这是您可以使用的samle代码。这不是一个更好的变体,你可以找到here更多优雅的多维密钥解决方案,但这个变种也是可用的。
var excelResult = new List<ExcelResults>(); // your code for getting results from excel;
var dict = new Dictionary<string, Dictionary<string, int>>();
foreach (var line in excelResult)
{
if (!dict.ContainsKey(line.Week))
dict.Add(line.Week, new Dictionary<string, int>());
if (!dict[line.Week].ContainsKey(line.Part))
dict[line.Week].Add(line.Part, 0);
++dict[line.Week][line.Part];
}
答案 2 :(得分:0)
此代码有助于从excel中读取内容并进行分组以满足您的需求。
string filename = @"c:\myexcel.xlsx"; // your input file.
string connectionstring = @Provider = Microsoft.ACE.OLEDB.12.0;
Data Source = " + filename + @"; Extended properties = 'Excel 12.0; HDR= Yes; ';";
using(var conn = new OleDbConnection(connectionstring))
{
conn.Open();
var comm = new OleDbCommand("SELECT week, parts FROM [Sheet1$]", conn);
var adapter = new OleDbDataAdapter(comm);
var ds = new DataSet();
adapter.Fill(ds);
var contents = ds.Tables[0].AsEnumerable().Select(x = > new {
week = x.Field < double > ("week"),
parts = x.Field < string > ("parts")
});
var result = contents.GroupBy(g = > g.week)
.Select(g = > new {week = g.Key, groups = g.GroupBy(s = > s.parts)
}).Select(n = > new {
n.week, PartAndCount = n.groups.Select(s = >
new
{
part = s.Key, count = s.Count()
})
});
}
将此结果转换为DataGridView
非常简单,可以通过以下几行来实现。
var gview = new DataGridView();
gview.Columns.Add("week", "Week");
foreach(string c in contents.Select(c = > c.parts).Distinct())
{
gview.Columns.Add(c, c);
}
foreach(var r in result)
{
int rowIndex = gview.Rows.Add();
DataGridViewRow row = gview.Rows[rowIndex];
foreach(var s in r.PartAndCount)
{
row.Cells[s.part].Value = s.count;
}
}
希望这有帮助。