[英]Load Multiple items from an excel sheet into a listview c#
I am desperately trying to add multiple items from an excel sheet into a listview using c#. 我拼命试图使用C#将Excel表中的多个项目添加到列表视图中。 I have looked all over the Internet for a working solution but still no result.
我已经在整个互联网上寻找了一个可行的解决方案,但仍然没有结果。
I would like to ask anybody who knows about c#'s listview for an helping hand... 我想向任何了解c#listview的人伸出援手...
Thanks in advance 提前致谢
code so far:- 到目前为止的代码:
public void InitializeListView(string path) {
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook sheet = excel.Workbooks.Open(path);
Microsoft.Office.Interop.Excel.Worksheet wx = excel.ActiveSheet as Microsoft.Office.Interop.Excel.Worksheet;
int count = 0;
int row = 0;
int col = 0;
Excel.Range userrange = wx.UsedRange;
count = userrange.Rows.Count;
statusBar1.Panels[1].Text = "Amount: " + count;
for (row = 1; row <= count; row++) {
for (col = 1; col <= 4; col++) {
listView1.Items.Add(wx.Cells[row, col].Value2);
listView1.Items.Add(wx.Cells[row, col].Value2);
listView1.Items.Add(wx.Cells[row, col].Value2);
listView1.Items.Add(wx.Cells[row, col].Value2);
}
}
sheet.Close(true, Type.Missing, Type.Missing);
excel.Quit();
}//------------------ end of InitializeListView -------------------------
This is a simple method. 这是一个简单的方法。 Please look if it helps you.
请查看它是否对您有帮助。 1. Convert the Excel file in to .csv and Store it in the a Path 2. Take the data from .csv file to list.
1.将Excel文件转换为.csv并将其存储在路径中。2.将.csv文件中的数据提取到列表中。 3. Delete the .csv file once all data is loaded in List<>.
3.将所有数据加载到List <>中后,删除.csv文件。
To Read from .csv 从.csv读取
string filepath = "D:\\sample.csv";
var lineCount = File.ReadAllLines(@"D:\\sample.csv").Length;
int TotalLines = Int32.Parse(lineCount.ToString());
StreamReader sr = new StreamReader(filepath);
string line;
List<string> lstSample = new List<string>();
while ((line = sr.ReadLine()) != null)
{
lstSample = line.Split(',').ToList();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.