2017-07-30 56 views
0

我拼命地嘗試從Excel表中添加多個項目到使用c#的列表視圖。我已經在整個互聯網上尋找一個可行的解決方案,但仍然沒有結果。從excel工作表加載多個項目到列表視圖c#

我想提前問任何人誰知道C#的列表視圖一個伸出援助之手......

感謝

到目前爲止的代碼: -

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 ------------------------- 

回答

0

這是一個簡單的方法。請看看它是否對你有幫助。 1.將Excel文件轉換爲.csv並將其存儲在路徑中 2.將.csv文件中的數據列出。 3.一旦所有數據在列表<>中加載,刪除.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(); 
     } 
閱讀
相關問題